獲取Table的建立語句

2021-09-26 04:07:56 字數 4107 閱讀 1737

-------------------------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;

varsl: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

語法 create table database name.owner owner.table name 指定列名稱,型別 collate 指定排序規則 default constant expression 指定預設值 identit seed,increment not for replicat...

動態建立Table

定義表頭 tablerow trhead new tablerow 定義列 tablecell tchead new tablecell 設定列名 tchead.text 列名1 追加乙個列 trhead.cells.add tchead tchead new tablecell 設定列名 tche...

獲取table的行,響應onclick事件

jquery方式 table tr click table find tr click table find tr length table tr length 原生js document.getelementbyid table getelementsbytagname tr length 注意要...