动态生成存储过程






3.29/5 (5投票s)
2007年9月9日
2分钟阅读

65423

548
创建存储过程以将命令插入任何表。
此压缩文件包含一个存储过程 .SQL 文件。
引言
本文介绍如何构建存储过程,以动态生成包含针对数据库中任何表的插入命令的存储过程。
创建存储过程
创建存储过程以自动生成另一个包含针对任何表的插入命令的存储过程。
CREATE PROCEDURE Create_procedure_To_insert
参数
您必须添加两个参数:
1- 表名:用于在其上生成插入命令。
2- 开发者姓名:用于在注释中设置开发者姓名。
// // CREATE PROCEDURE create_procedure @table varchar(200), @DeveloperName varchar(200), @Createtable varchar(20) //
声明变量
您需要在存储过程中声明许多变量,请参阅下面的代码了解详情。
declare @testTable varchar(8000) declare @testTable2 varchar(8000) declare @testTable3 varchar(8000) declare @opration varchar(8000) declare @final varchar(8000) declare @OP varchar(100)
1- @testTable:用于从表中设置所有列。
2- @testTable2:用于从表中设置所有数据类型。
3- @testTable3:用于从表中设置所有参数。
4- @opration:用于设置插入命令。
5- @final:用于设置自动生成的存储过程。
6- @OP:用于设置新存储过程的名称。
初始化变量
您需要在下面的变量中设置空值。
set @testTable='' set @testTable2='' set @final='' set @testTable3='' set @opration='' declare @Datetime varchar(50) set @Datetime=getdate()
重要代码
您需要创建三个 SELECT 语句
1- 第一个 SELECT 语句用于从 information_schema.columns 获取所有列,当表等于参数中的表名时,您必须获取 'isidentity' = zero,因为如果 'isidentity' = one,则无法在标识列上插入数据。
2- 第二个 SELECT 语句用于从 information_schema.columns 获取所有数据类型,当表等于参数中的表名时,您必须获取 'isidentity' = zero,因为如果 'isidentity' = one,则无法在标识列上插入数据。
3- 第三个 SELECT 语句用于获取列名并在参数中设置 @columns 名称
之后,您必须生成新的存储过程,因此您可以以字符串数据类型的形式创建过程结构,并将字符串设置为执行函数以执行存储过程“Exec(@final)”
select @testTable=@testTable+ ', '+column_name from information_schema.columns where table_name=@table and (COLUMNPROPERTY(OBJECT_ID(@table), column_name, 'isidentity') = 0) AND (column_default IS NULL) select @testTable2=@testTable2+ ', @'+column_name+' '+data_type+'(' + cast(character_maximum_length as varchar(10)) +')' + case is_nullable when 'no' then ' ' when 'yes' then '=null' end from information_schema.columns where table_name=@table and (COLUMNPROPERTY(OBJECT_ID(@table), column_name, 'isidentity') = 0)and character_maximum_length<>null AND (column_default IS NULL)and data_type<>'text' select @testTable2=@testTable2+ ', @'+column_name+' '+data_type from information_schema.columns where table_name=@table and (COLUMNPROPERTY(OBJECT_ID(@table), column_name, 'isidentity') = 0)and (character_maximum_length=null or data_type='text' ) AND (column_default IS NULL) select @testTable3=@testTable3+ ', @'+column_name from information_schema.columns where table_name=@table and (COLUMNPROPERTY(OBJECT_ID(@table), column_name, 'isidentity') = 0) AND (column_default IS NULL) set @testTable=SUBSTRING(@testTable,2,len(@testTable)) set @testTable2=SUBSTRING(@testTable2,4,len(@testTable2)) set @testTable3=SUBSTRING(@testTable3,2,len(@testTable3)) set @opration=' insert into [' +@table+'] ( '+@testTable+' ) values ( '+ @testTable3 +' )' set @OP='InsertNew'+@table set @final='/* ---------------------------------------------------------------------------------------- Store Procedure Name : SP__'+@OP +' ---------------------------------------------------------------------------------------- 1- Creation Date :'+convert (varchar,getdate(),103) +' 2- Last Update :'+convert (varchar,getdate(),103)+' 3- Parametars No:6 4- Creation By :'+@DeveloperName+' 5- Last Update By :'+@DeveloperName+' 6- Return Value : Dataset --------------------------------------------------------------------------------------- */ Create PROCEDURE SP__'+@OP+' ( '+ @testTable2 + ' ) AS set nocount on ' + @opration + ' Select * from [' +@table +']' exec (@final)
摘要
创建此存储过程后,您可以立即创建用于插入命令的存储过程,只需设置表名参数和您的姓名即可。