大家好,
我创建了一个存储过程,它将表的列作为参数并插入到另一个表中,过程如下所示:
ALTER PROCEDURE cagroup (
@DataID INT ,
@days INT ,
@GName VARCHAR(50) ,
@T_ID INT ,
@Act BIT ,
@Key VARBINARY(16)
)
AS
BEGIN
DECLARE @SQL NVARCHAR(MAX)
DECLARE @SchemaName SYSNAME
DECLARE @TableName SYSNAME
DECLARE @DatabaseName SYSNAME
DECLARE @BR CHAR(2)
SET @BR = CHAR(13) + CHAR(10)
SELECT @SchemaName = Source_Schema ,
@TableName = Source_Table ,
@DatabaseName = Source_Database
FROM Source
WHERE ID = @DataID
SET @SQL = 'INSERT INTO ' + @DatabaseName + '.' + @SchemaName + '.'
+ @TableName + ' (' + @BR + 'GrName,' + @BR
+ '[To_ID],' + @BR + '[DataSoID],' + @BR
+ '[Active],' + @BR + 'S_R_ID'
+ @BR + ')' + @BR
SET @SQL = @SQL + 'SELECT ' + @GName + ' AS GrName ,' + @BR
+ cast(@T_ID as NVARCHAR(10)) + ' AS To_ID ,' + @BR
+ cast(@DataID as NVARCHAR(10)) + ' AS DataSoID ,' + @BR
+ cast(@Act as NVARCHAR(10)) + ' AS Active ,' + @BR
+ Key + ' AS key' + @BR
+ 'R_ID AS S_R_ID' + @BR
+ 'FROM' + @DatabaseName + '.'
+ @SchemaName + '.'
+ @TableName + ' t' + @BR
+ 'LEFT OUTER JOIN Gro g ON g.GName = '
+ @GName + @BR + 'AND g.Data_ID] ='
+cast(@DataID as NVARCHAR(10)) + @BR
+ ' WHERE t.[I_DATE] > GETDATE() -'
+ cast(@days as NVARCHAR(10)) + @BR
+ ' AND g.GName IS NULL
AND ' + @GName + ' IS NOT NULL
AND t.[Act] = 1' + @BR
EXEC (@SQL)
END
现在我必须创建更多这样的过程,但问题是我将表的列作为参数传递,因此其他表在表中有50列,我不想有50个参数。那么有更好的方法吗?