将值传递给存储过程中的参数

时间:2017-08-08 06:19:32

标签: sql sql-server-2016

我有一个超过500的值列表,每次我必须将这些值传递给下面的存储过程。是否可以动态调用这些值?

RDBMS: SQL Server 2014

下面的一小部分@value示例。

    declare @valuetable table
(
value varchar(50)
)

insert @valuetable

select video union
select audio union
select hayward union
select abott union
select gsk

代码:

DECLARE @value VARCHAR(24) SET @value = 'video'     
DECLARE @DAYS INT SET @DAYS = -30               


SELECT * INTO #XTP1 FROM (                  
SELECT DISTINCT 'START' AS DTT, DATEADD(D,@DAYS,DATEACTIONED) AS DT FROM NEWREPORTS
WHERE value = @value                        
UNION                           
SELECT DISTINCT 'CHANGE' AS DTT, DATEACTIONED AS DT FROM NEWREPORTS
WHERE value = @value                        
)r

由于

1 个答案:

答案 0 :(得分:0)

所以你需要做的是在#XTP1中添加一列来存储值名称。然后你可以这样做:

declare @valuetable table
(
value varchar(50)
)

insert @valuetable

select video union
select audio union
select hayward union
select abott union
select gsk

SELECT * INTO #XTP1 FROM (                  
SELECT DISTINCT v.value, 'START' AS DTT, DATEADD(D,@DAYS,DATEACTIONED) AS DT
FROM NEWREPORTS n INNER JOIN @valuetable v ON n.value = v.value                        
UNION                           
SELECT DISTINCT v.value, 'CHANGE' AS DTT, DATEACTIONED AS DT 
FROM NEWREPORTS n INNER JOIN @valuetable v ON n.value = v.value
)r

现在,您可以一次性从#XTP1中检索所有内容。您可能希望包含" value"在你的ORDER BY。