其他分享
首页 > 其他分享> > 利用存储过程提取主键名称,再利用游标判断主键个数

利用存储过程提取主键名称,再利用游标判断主键个数

作者:互联网

ALTER procedure A
@TableName nvarchar(max)
as
begin
	create table Table_key
(
  Tablename varchar(50),
  T_key  varchar(50)
  
)
	DECLARE  @into_table   nvarchar(max)
	
	select   @into_table  = 'insert into Table_key  SELECT TABLE_NAME ,COLUMN_NAME  FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE   
	WHERE TABLE_NAME= '+''''+ @TableName+''''+''
	
	Exec ( @into_table)
END

DECLARE @id_name varchar(100),@i int
set @i=0
declare count_cursor cursor
for
	select * FROM Table_key
	
for read only
     
	open count_cursor
	FETCH NEXT FROM count_cursor INTO @tablename,@id_name --抓取下一行游标数据
	
WHILE @@FETCH_STATUS = 0
  BEGIN

    PRINT '表名:'+@tablename+'      '+'主键:'+@id_name
	select @i=@i+1
	 FETCH NEXT FROM count_cursor INTO @tablename,@id_name

 END
 PRINT @i
CLOSE count_cursor --关闭游标
DEALLOCATE count_cursor --释放游标

标签:count,into,游标,id,cursor,利用,table,主键
来源: https://www.cnblogs.com/1241187306qq/p/16528373.html