SQL server 获得 表的主键,自增键
主键:
@tableName --表名
@id ---表对应的id
SELECT SYSCOLUMNS.name FROM SYSCOLUMNS,SYSOBJECTS,SYSINDEXES,SYSINDEXKEYS WHERE SYSCOLUMNS.id = object_id(@tableName) AND SYSOBJECTS.xtype = 'PK' AND SYSOBJECTS.parent_obj = SYSCOLUMNS.id AND SYSINDEXES.id = SYSCOLUMNS.id AND SYSOBJECTS.name = SYSINDEXES.name AND SYSINDEXKEYS.id = SYSCOLUMNS.id AND SYSINDEXKEYS.indid = SYSINDEXES.indid AND SYSCOLUMNS.colid = SYSINDEXKEYS.colid
自增键:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME=@tableName AND COLUMNPROPERTY(OBJECT_ID(@tableName),COLUMN_NAME,'IsIdentity')=1
列名说明字段:
select name,id,xtype from dbo.sysobjects where name in (‘@tableName’)
select a.name,value from sys.extended_properties b, dbo.syscolumns a where a.colid=b.minor_id and a.id=b.major_id and a.id =@id and a.iscomputed=0
所有列名:
select columnName,typeName from (select a.*,b.name as typeName,b.length as typeLength,colid as columnId from (select id as objectId,name as columnName,xtype,length as columnLength,colid from dbo.syscolumns where id = @id ) a left join dbo.systypes b on a.xtype=b.xtype) t where typeName <> 'sysname' order by columnId
所有可编辑列名: 去掉主键列、自增键列、表内计算列
select columnName,typeName from (select a.*,b.name as typeName,b.length as typeLength,colid as columnId from (select id as objectId,name as columnName,xtype,length as columnLength,colid from dbo.syscolumns where id = @id and iscomputed=0 and name not in(SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME=@tableName AND COLUMNPROPERTY(OBJECT_ID(@tableName),COLUMN_NAME,'IsIdentity')=1)) a left join dbo.systypes b on a.xtype=b.xtype) t where typeName <> 'sysname' order by columnId
下一篇: 同事在朋友圈发消息让大家帮忙介绍女朋友
推荐阅读
-
Oracle 实现类似SQL Server中自增字段的一个办法
-
Oracle创建主键自增表(sql语句实现)及触发器应用
-
Oracle 实现类似SQL Server中自增字段的一个办法
-
Oracle创建主键自增表(sql语句实现)及触发器应用
-
oracle建表设置主键自增的方法
-
Oracle数据库下给表设置自增的逻辑主键的方法
-
ORACLE数据库创建表、自增主键、外键相关语法讲解
-
牛客SQL练习-46-在audit表上创建外键约束,其emp_no对应employees_test表的主键id
-
SQL实战46.在audit表上创建外键约束,其emp_no对应employees_test表的主键id
-
Oracle如何创建表的自增主键ID—SEQ序列和Mybatis.xml的selectKey代码