如何识别表是否有标识列 [英] How to identify whether the table has identity column
本文介绍了如何识别表是否有标识列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道该表是否有标识列.我不知道表.表的结构我没做过.使用查询?
I want to find out whether the table has an identity column or not. Table is unknown to me. I have not done the structure of the table. Using Query?
我使用的是 Sql Server 精简版.
I am using Sql Server Compact Edition.
推荐答案
此查询返回表的标识列名称:
This query returns a table's identity column name:
CREATE PROCEDURE dbo.usp_GetIdentity
@schemaname nvarchar(128) = 'dbo'
,@tablename nvarchar(128)
AS
BEGIN
SELECT OBJECT_NAME(OBJECT_ID) AS TABLENAME,
NAME AS COLUMNNAME,
SEED_VALUE,
INCREMENT_VALUE,
LAST_VALUE,
IS_NOT_FOR_REPLICATION
FROM SYS.IDENTITY_COLUMNS
WHERE OBJECT_NAME(OBJECT_ID) = @tablename
AND OBJECT_SCHEMA_NAME(object_id) = @schemaname
END
然后形成代码端.
使用 datareader 角色调用此存储过程,然后检查 datareader.hasrows()
.如果条件值为真 (1
),则表具有标识列(如果已设置).如果不是,则它没有标识列.
Call this stored procedure using the datareader role, then check datareader.hasrows()
. If the condition value is true (1
), then the table has identity column if set. If not then it doesn't have an identity column.
这篇关于如何识别表是否有标识列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文