用于插入,删除和更新Information_Schema的evey单表的存储过程

时间:2015-07-02 10:46:57

标签: sql-server stored-procedures information-schema

我想编写一个脚本来制作存储过程,用于插入,删除和更新每一个信息_Schema的表,我想出了这个代码,但我在程序输入参数和插入,删除和更新的方式上有问题他们,有人可以帮我吗?

DECLARE AutoProc CURSOR GLOBAL
FOR
    SELECT TABLE_SCHEMA,TABLE_NAME
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_TYPE='BASE TABLE'
OPEN AutoProc

DECLARE @Schema NVARCHAR(100)
DECLARE @Table NVARCHAR(100)
DECLARE @TotalField NVARCHAR(600)=''
DECLARE @Parameters NVARCHAR(600)

FETCH NEXT
FROM AutoProc
INTO @Schema,@Table

WHILE @@FETCH_STATUS = 0

BEGIN

DECLARE AutoFields CURSOR GLOBAL
FOR
    SELECT COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA=@Schema AND
    TABLE_NAME=@Table AND
    TABLE_NAME
    IN
    (
    SELECT TABLE_NAME
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_TYPE= 'BASE TABLE'
    )
OPEN AutoFields

DECLARE @FieldName NVARCHAR(100)
DECLARE @DataType NVARCHAR(50)
DECLARE @Size NVARCHAR(10)

FETCH NEXT
FROM AutoFields
INTO @FieldName,@DataType,@Size

WHILE @@FETCH_STATUS = 0

BEGIN

IF  @DataType='NVARCHAR' OR @DataType='nvarchar'
BEGIN
SET @TotalField=@TotalField+'@'+@FieldName+' '+@DataType+'('+@Size+'),'
END
ELSE
BEGIN
SET @TotalField=@TotalField+'@'+@FieldName+' '+@DataType+','
END

FETCH NEXT
FROM AutoFields
INTO @FieldName,@DataType,@Size
END

CLOSE AutoFields
DEALLOCATE AutoFields

SET @Parameters =SUBSTRING(@TotalField,1,LEN(@TotalField)-1)

EXECUTE('
CREATE PROC USP_INS_'+@Schema+'_'+@Table+'
@parameters NVARCHAR(600)='+@Parameters+'
AS
BEGIN 

INSERT '+@Schema+'.'+@Table+'
VALUES(@parameters)

END')

EXECUTE('
CREATE PROC USP_UPD_'+@Schema+'_'+@Table+'
@parameters NVARCHAR(600)='+@Parameters+'
AS
BEGIN

UPDATE '+@Schema+'.'+@Table+'
SET 

END')

EXECUTE('
CREATE PROC USP_DEL_'+@Schema+'_'+@Table+'
parameters NVARCHAR(600)='+@Parameters+'
AS
BEGIN

DELETE '+@Schema+'.'+@Table+'
WHERE 

END')

SET @TotalField=''

FETCH NEXT
FROM AutoProc
INTO @Schema,@Table
END

CLOSE AutoProc
DEALLOCATE AutoProc

0 个答案:

没有答案