IF NOTEXISTS ( SELECT*FROM sysobjects WHERE id = OBJECT_ID(N'user_table') AND OBJECTPROPERTY(id, N'IsUserTable') =1 ) BEGIN SELECT*INTO sqlTest.dbo.user_table FROM master.dbo.user_table go
增加数据
追加字段
1 2 3 4 5 6
ALTERTABLE user_table ADD cID INTIDENTITY (1,1), telNumber varchar(11) # 查询结果 SELECT TOP (10) * FROM [sqlTest].[dbo].[user_table]
SELECT cname FROM col WHERE tname='ZW_YINGYEZ' SELECT column_name FROM user_tab_columns WHERE table_name='ZW_YINGYEZ'
查询表字段数
1
SELECTcount(column_name) FROM user_tab_columns WHERE table_name='表名';
SQLServer中获取表结构
1
SELECT a.COLUMN_NAME,a.DATA_TYPE,a.CHARACTER_MAXIMUM_LENGTH,b.value FROM InformatiON_schema.COLUMNS AS a LEFTJOIN sys.extended_properties AS b ON a.TABLE_NAME=OBJECT_NAME(b.major_id) AND a.ORDINAL_POSITION=b.minor_id WHERE a.TABLE_NAME='user_table'
SQLServer语句查询某表中所有字段的名称、类型、长度等属性信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
SELECT 字段名=a.name, 标识=cAseWHEN COLUMNPROPERTY(a.id,a.name,'IsIdentity')=1THEN'√'ELSE''end, 主键=cAseWHENEXISTS(SELECT1FROM sysobjects WHERE xtype='PK'AND name IN (SELECT name FROM sysIndexes WHERE Indid IN(SELECT Indid FROM sysIndexkeys WHERE id = a.id AND colid=a.colid))) THEN'√'ELSE''end, 类型=b.name, 占用字节数=a.length, 长度=COLUMNPROPERTY(a.id,a.name,'PRECISION'), 小数位数=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0), 允许空=cAseWHEN a.isnullable=1THEN'√'ELSE''end, 默认值=isnull(e.text,''), 字段说明=isnull(g.[value],'') FROM syscolumns a LEFTJOIN systypes b ON a.xtype=b.xusertype INNERjoIN sysobjects d ON a.id=d.id AND d.xtype='U'AND d.name<>'dtproperties' LEFTJOIN syscomments e ON a.cdefault=e.id LEFTJOIN sys.extended_properties g ON a.id=g.major_id AND a.colid=g.minor_id LEFTJOIN sys.extended_properties f ON d.id=f.major_id AND f.minor_id =0 WHERE d.name='user_table' ORDERBY a.name