我想创建一个 SP 来使用 SQL 中的游标为所有表生成元数据。以下是我尝试过的代码。但它成为一个永无止境的循环,并且重复相同的数据。提前致谢。
--SELECT * FROM information_schema.columns
ALTER PROCEDURE p1
AS
SET NOCOUNT ON;
DECLARE @id INT
, @tablename VARCHAR(100)
, @columnname VARCHAR(100)
, @datatype VARCHAR(100)
, @isnullable VARCHAR(100)
BEGIN
DECLARE CURSOR_1 CURSOR FOR
SELECT TABLE_NAME
, COLUMN_NAME
, DATA_TYPE
, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Employee' -- group BY table_name
OPEN CURSOR_1
FETCH NEXT FROM CURSOR_1 INTO
@tablename,
@columnname,
@datatype,
@isnullable
WHILE @@fetch_status = 0
BEGIN
INSERT INTO table_schema_detail (TABLE_NAME, COLUMN_NAME, DATA_TYPE, isnullable)
VALUES (@tablename, @columnname, @datatype, @isnullable)
END
FETCH NEXT FROM CURSOR_1 INTO
@tablename,
@columnname,
@datatype,
@isnullable
CLOSE CURSOR_1
DEALLOCATE CURSOR_1
SET NOCOUNT OFF;
END
GO