输入表名生成插入、更新存储过程和调用该存储过程的字符串
Code
IF OBJECT_ID('F_GET_PROC') IS NOT NULL DROP FUNCTION F_GET_PROC
GO
CREATE FUNCTION F_GET_PROC(@TABLE VARCHAR(200))
RETURNS VARCHAR(MAX)
BEGIN
DECLARE @S VARCHAR(MAX)
SET @S=ISNULL(@S,'')+'IF OBJECT_ID(N''P_'+@TABLE+''',N''P'') IS NOT NULL DROP PROC P_'+@TABLE+CHAR(13)
SET @S=@S+'GO'+CHAR(13)
SET @S=@S+'Create PROC P_'+@TABLE+CHAR(13)
--定义存储过程参数输出
Select @S=@S+' @'+COLUMN_NAME+' VARCHAR(8000) = NULL,'+CHAR(13)
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=@TABLE
SET @S=@S+' @Identity INT OUTPUT'+CHAR(13) --去尾逗号
SET @S=@S+'AS'+CHAR(13)
SET @S=@S+' SET NOCOUNT ON'+CHAR(13)
SET @S=@S+' SET XACT_ABORT ON'+CHAR(13)
SET @S=@S+'BEGIN'+CHAR(13)
--将时间类型字段默认值设置为当前时间
SELECT @S=@S+' IF @'+COLUMN_NAME+' IS NULL OR LTRIM(RTRIM(@'+COLUMN_NAME+'))='''' SET @'+COLUMN_NAME+'=GETDATE()'+CHAR(13)
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=@TABLE AND (DATA_TYPE='DATETIME' OR DATA_TYPE='SMALLDATETIME')
--取出自增ID列名
DECLARE @IDENTITY_COLUMN VARCHAR(50)
Select @IDENTITY_COLUMN=COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME=@TABLE AND ORDINAL_POSITION=
(Select MIN(ORDINAL_POSITION) FROM INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME=@TABLE)
--如果没有输入标识ID值,则认为增加一条记录
SET @S=@S+' IF @'+@IDENTITY_COLUMN+' IS NULL OR LTRIM(RTRIM(@'+@IDENTITY_COLUMN+'))='''''+CHAR(13)
SET @S=@S+' BEGIN'+CHAR(13)
SET @S=@S+' INSERT INTO '+@TABLE+' values('
SELECT @S=@S+'@'+COLUMN_NAME+',' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=@TABLE AND COLUMN_NAME<>@IDENTITY_COLUMN
SELECT @S=STUFF(@S,LEN(@S),1,')')
SET @S=@S+CHAR(13)+' SET @Identity=IDENT_CURRENT('''+@TABLE+''')'+CHAR(13)
SET @S=@S+' END'+CHAR(13)
--否则认为是更新一条记录
SET @S=@S+' ELSE'+CHAR(13)
SET @S=@S+' BEGIN'+CHAR(13)
SET @S=@S+' UPDATE '+@TABLE+' SET '
SELECT @S=@S+COLUMN_NAME+' = @'+COLUMN_NAME+',' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=@TABLE AND COLUMN_NAME<>@IDENTITY_COLUMN
SELECT @S=STUFF(@S,LEN(@S),1,'')
SET @S=@S+' WHERE '+@IDENTITY_COLUMN+' = @'+@IDENTITY_COLUMN+CHAR(13)
SET @S=@S+' SET @Identity=@'+@IDENTITY_COLUMN+CHAR(13)
SET @S=@S+' END'+CHAR(13)
SET @S=@S+'END'
RETURN @S
END
--使用方法
PRINT DBO.F_GET_PROC('T_NEW_BUSI')
IF OBJECT_ID('F_GET_CALL_P_STR') IS NOT NULL DROP FUNCTION F_GET_CALL_P_STR
GO
--输入表名,生成调用存储过程字符串
--说明:如果表字段比较少则直接调用Print dbo.F_GET_PROC('表名')
-- 如果预计超出个字符则用程序输出(因在查询分析器中Print字符有限)
CREATE FUNCTION F_GET_CALL_P_STR(
@TABLE VARCHAR(200)--表名
)
RETURNS VARCHAR(MAX)
AS
BEGIN
IF @TABLE<>''
BEGIN
DECLARE @S VARCHAR(MAX)
SET @S=ISNULL(@S,'')+'Dim Identity'+CHAR(13)
SET @S=@S+'On Error Resume Next'+CHAR(13)
SET @S=@S+'CreateCmd'+CHAR(13)
SET @S=@S+'With Cmd'+CHAR(13)
SET @S=@S+' .CommandText = "P_'+@TABLE+'"'+CHAR(13)
SET @S=@S+' .CommandType = 4'+CHAR(13)
SET @S=@S+' .CommandTimeout = 0'+CHAR(13)
SET @S=@S+' .ActiveConnection = Conn'+CHAR(13)
SET @S=@S+' .Prepared = true'+CHAR(13)
SET @S=@S+' ''如果有返回值'+CHAR(13)
SET @S=@S+' ''.Parameters.Append .CreateParameter("RETURN",2,4)'+CHAR(13)
SELECT @S=@S+' .Parameters.Append .CreateParameter("@'+A.COLUMN_NAME+'",200,1,'+LTRIM(B.LENGTH)+','+A.COLUMN_NAME+')'+CHAR(13)
FROM information_schema.columns A INNER JOIN SYSTYPES B ON table_name = @TABLE AND A.DATA_TYPE=B.NAME
SET @S=@S+' .Parameters.Append .CreateParameter("@Identity",3,2)'+CHAR(13)
SET @S=@S+' .EXECUTE'+CHAR(13)
SET @S=@S+'Identity=.Parameters("@Identity")'+CHAR(13)
SET @S=@S+'End With'+CHAR(13)
SET @S=@S+'CloseCmd'+CHAR(13)
END
RETURN @S
END
GO
--使用方法
PRINT DBO.F_GET_CALL_P_STR('T_NEW_BUSI')
IF OBJECT_ID('F_GET_PROC') IS NOT NULL DROP FUNCTION F_GET_PROC
GO
CREATE FUNCTION F_GET_PROC(@TABLE VARCHAR(200))
RETURNS VARCHAR(MAX)
BEGIN
DECLARE @S VARCHAR(MAX)
SET @S=ISNULL(@S,'')+'IF OBJECT_ID(N''P_'+@TABLE+''',N''P'') IS NOT NULL DROP PROC P_'+@TABLE+CHAR(13)
SET @S=@S+'GO'+CHAR(13)
SET @S=@S+'Create PROC P_'+@TABLE+CHAR(13)
--定义存储过程参数输出
Select @S=@S+' @'+COLUMN_NAME+' VARCHAR(8000) = NULL,'+CHAR(13)
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=@TABLE
SET @S=@S+' @Identity INT OUTPUT'+CHAR(13) --去尾逗号
SET @S=@S+'AS'+CHAR(13)
SET @S=@S+' SET NOCOUNT ON'+CHAR(13)
SET @S=@S+' SET XACT_ABORT ON'+CHAR(13)
SET @S=@S+'BEGIN'+CHAR(13)
--将时间类型字段默认值设置为当前时间
SELECT @S=@S+' IF @'+COLUMN_NAME+' IS NULL OR LTRIM(RTRIM(@'+COLUMN_NAME+'))='''' SET @'+COLUMN_NAME+'=GETDATE()'+CHAR(13)
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=@TABLE AND (DATA_TYPE='DATETIME' OR DATA_TYPE='SMALLDATETIME')
--取出自增ID列名
DECLARE @IDENTITY_COLUMN VARCHAR(50)
Select @IDENTITY_COLUMN=COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME=@TABLE AND ORDINAL_POSITION=
(Select MIN(ORDINAL_POSITION) FROM INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME=@TABLE)
--如果没有输入标识ID值,则认为增加一条记录
SET @S=@S+' IF @'+@IDENTITY_COLUMN+' IS NULL OR LTRIM(RTRIM(@'+@IDENTITY_COLUMN+'))='''''+CHAR(13)
SET @S=@S+' BEGIN'+CHAR(13)
SET @S=@S+' INSERT INTO '+@TABLE+' values('
SELECT @S=@S+'@'+COLUMN_NAME+',' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=@TABLE AND COLUMN_NAME<>@IDENTITY_COLUMN
SELECT @S=STUFF(@S,LEN(@S),1,')')
SET @S=@S+CHAR(13)+' SET @Identity=IDENT_CURRENT('''+@TABLE+''')'+CHAR(13)
SET @S=@S+' END'+CHAR(13)
--否则认为是更新一条记录
SET @S=@S+' ELSE'+CHAR(13)
SET @S=@S+' BEGIN'+CHAR(13)
SET @S=@S+' UPDATE '+@TABLE+' SET '
SELECT @S=@S+COLUMN_NAME+' = @'+COLUMN_NAME+',' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=@TABLE AND COLUMN_NAME<>@IDENTITY_COLUMN
SELECT @S=STUFF(@S,LEN(@S),1,'')
SET @S=@S+' WHERE '+@IDENTITY_COLUMN+' = @'+@IDENTITY_COLUMN+CHAR(13)
SET @S=@S+' SET @Identity=@'+@IDENTITY_COLUMN+CHAR(13)
SET @S=@S+' END'+CHAR(13)
SET @S=@S+'END'
RETURN @S
END
--使用方法
PRINT DBO.F_GET_PROC('T_NEW_BUSI')
IF OBJECT_ID('F_GET_CALL_P_STR') IS NOT NULL DROP FUNCTION F_GET_CALL_P_STR
GO
--输入表名,生成调用存储过程字符串
--说明:如果表字段比较少则直接调用Print dbo.F_GET_PROC('表名')
-- 如果预计超出个字符则用程序输出(因在查询分析器中Print字符有限)
CREATE FUNCTION F_GET_CALL_P_STR(
@TABLE VARCHAR(200)--表名
)
RETURNS VARCHAR(MAX)
AS
BEGIN
IF @TABLE<>''
BEGIN
DECLARE @S VARCHAR(MAX)
SET @S=ISNULL(@S,'')+'Dim Identity'+CHAR(13)
SET @S=@S+'On Error Resume Next'+CHAR(13)
SET @S=@S+'CreateCmd'+CHAR(13)
SET @S=@S+'With Cmd'+CHAR(13)
SET @S=@S+' .CommandText = "P_'+@TABLE+'"'+CHAR(13)
SET @S=@S+' .CommandType = 4'+CHAR(13)
SET @S=@S+' .CommandTimeout = 0'+CHAR(13)
SET @S=@S+' .ActiveConnection = Conn'+CHAR(13)
SET @S=@S+' .Prepared = true'+CHAR(13)
SET @S=@S+' ''如果有返回值'+CHAR(13)
SET @S=@S+' ''.Parameters.Append .CreateParameter("RETURN",2,4)'+CHAR(13)
SELECT @S=@S+' .Parameters.Append .CreateParameter("@'+A.COLUMN_NAME+'",200,1,'+LTRIM(B.LENGTH)+','+A.COLUMN_NAME+')'+CHAR(13)
FROM information_schema.columns A INNER JOIN SYSTYPES B ON table_name = @TABLE AND A.DATA_TYPE=B.NAME
SET @S=@S+' .Parameters.Append .CreateParameter("@Identity",3,2)'+CHAR(13)
SET @S=@S+' .EXECUTE'+CHAR(13)
SET @S=@S+'Identity=.Parameters("@Identity")'+CHAR(13)
SET @S=@S+'End With'+CHAR(13)
SET @S=@S+'CloseCmd'+CHAR(13)
END
RETURN @S
END
GO
--使用方法
PRINT DBO.F_GET_CALL_P_STR('T_NEW_BUSI')