-------------------------sql脚本---------------------------
SELECT (CASE WHEN a.colorder=1 THEN d.name ELSE ‘‘‘‘ END) N‘表名‘, a.colorder N‘字段序号‘, a.name N‘字段名‘,
(CASE WHEN COLUMNPROPERTY( a.id,a.name,‘IsIdentity‘)=1 THEN ‘√‘ELSE ‘‘ END) N‘标识‘,
(CASE WHEN (SELECT COUNT(*) FROM sysobjects WHERE (name in
(SELECT name FROM sysindexes WHERE (id = a.id) AND (indid in
(SELECT indid FROM sysindexkeys WHERE (id = a.id) AND (colid in
(SELECT colid FROM syscolumns WHERE (id = a.id) AND (name = a.name))))))) AND
(xtype = ‘PK‘))>0 THEN ‘√‘ ELSE ‘‘ END) N‘主键‘, b.name N‘类型‘, a.length N‘占用字节数‘,
COLUMNPROPERTY(a.id,a.name,‘PRECISION‘) AS N‘长度‘, ISNULL(COLUMNPROPERTY(a.id,a.name,‘Scale‘),0) AS N‘小数位数‘,
(CASE WHEN a.isnullable=1 THEN ‘√‘ELSE ‘‘ END) N‘允许空‘, ISNULL(e.text,‘‘) N‘默认值‘,
ISNULL(g.[value],‘‘) AS N‘字段说明‘ FROM syscolumns a LEFT JOIN systypes b on a.xtype=b.xusertype
INNER JOIN sysobjects d on a.id=d.id AND d.xtype=‘U‘ AND d.name<>‘dtproperties‘
LEFT JOIN syscomments e on a.cdefault=e.id
LEFT JOIN sys.extended_properties g on a.id=g.major_id AND a.colid=g.minor_id
WHERE d.name = ‘Company‘ --要查询的表
ORDER BY object_name(a.id), a.colorder
-----------------------------------------------------------
//--------------------获取Table的创建语句(Delphi方法)---------------------------
function TFrm_main.GetSQLString(TbName:string):string;
var
sl:TStringList;
sqlstr,sqlstr2:string;
colname,coltype,collen,rhtpotlen,isnull,isidentify,IsFlag:string;
begin
sqlstr2:=‘SELECT (CASE WHEN a.colorder=1 THEN d.name ELSE ‘‘‘‘ END) N‘‘表名‘‘, a.colorder N‘‘字段序号‘‘, a.name N‘‘字段名‘‘, ‘
+‘(CASE WHEN COLUMNPROPERTY( a.id,a.name,‘‘IsIdentity‘‘)=1 THEN ‘‘√‘‘ELSE ‘‘‘‘ END) N‘‘标识‘‘, ‘
+‘(CASE WHEN (SELECT COUNT(*) FROM sysobjects WHERE (name in ‘
+‘(SELECT name FROM sysindexes WHERE (id = a.id) AND (indid in ‘
+‘(SELECT indid FROM sysindexkeys WHERE (id = a.id) AND (colid in ‘
+‘(SELECT colid FROM syscolumns WHERE (id = a.id) AND (name = a.name))))))) AND ‘
+‘(xtype = ‘‘PK‘‘))>0 THEN ‘‘√‘‘ ELSE ‘‘‘‘ END) N‘‘主键‘‘, b.name N‘‘类型‘‘, a.length N‘‘占用字节数‘‘,‘
+‘COLUMNPROPERTY(a.id,a.name,‘‘PRECISION‘‘) AS N‘‘长度‘‘, ISNULL(COLUMNPROPERTY(a.id,a.name,‘‘Scale‘‘),0) AS N‘‘小数位数‘‘, ‘
+‘(CASE WHEN a.isnullable=1 THEN ‘‘√‘‘ELSE ‘‘‘‘ END) N‘‘允许空‘‘, ISNULL(e.text,‘‘‘‘) N‘‘默认值‘‘,‘
+‘ISNULL(g.[value],‘‘‘‘) AS N‘‘字段说明‘‘ FROM syscolumns a LEFT JOIN systypes b on a.xtype=b.xusertype ‘
+‘INNER JOIN sysobjects d on a.id=d.id AND d.xtype=‘‘U‘‘ AND d.name<>‘‘dtproperties‘‘‘
+‘LEFT JOIN syscomments e on a.cdefault=e.id ‘
+‘LEFT JOIN sys.extended_properties g on a.id=g.major_id AND a.colid=g.minor_id ‘
+‘WHERE d.name = ‘‘‘+TbName+‘‘‘‘// --要查询的表
+‘ORDER BY object_name(a.id), a.colorder ‘;
with qrytable do
begin
Close;
SQl.Text:=sqlstr2;
Open;
sqlstr:=‘Create Table ‘+TbName+‘(‘;
while not Eof do
begin
if sqlstr<>‘Create Table ‘+TbName+‘(‘ then sqlstr:=sqlstr+‘,‘;
colname:=FieldByName(‘字段名‘).AsString;
coltype:=FieldByName(‘类型‘).AsString;
collen:=FieldByName(‘长度‘).AsString;
rhtpotlen:=FieldByName(‘小数位数‘).AsString;
isnull:=FieldByName(‘允许空‘).AsString;
isidentify:=FieldByName(‘主键‘).AsString;
IsFlag:=FieldByName(‘标识‘).AsString;
sqlstr:=sqlstr+colname+‘ ‘+coltype;
coltype:=LowerCase(coltype); //数据类型
if coltype=‘decimal‘ then
sqlstr:=sqlstr+‘(‘+collen+‘,‘+rhtpotlen+‘)‘;
if (coltype=‘binary‘) or (coltype=‘varbinary‘) or (coltype=‘float‘) or (coltype=‘char‘)
or (coltype=‘nchar‘) or (coltype=‘varchar‘) or (coltype=‘nvarchar‘) then
begin
sqlstr:=sqlstr+‘(‘+collen+‘)‘;
end;
if isnull=‘‘ then sqlstr:=sqlstr+‘ not null‘;
if isidentify<>‘‘ then sqlstr:=sqlstr+‘ Primary Key‘;
if IsFlag<>‘‘ then sqlstr:=sqlstr+‘ identity(1,1)‘;
Next;
end;
sqlstr:=sqlstr+‘)‘;
end;
Result:=sqlstr;
end;
获取Table的创建语句