迫切需要一个sp来传递单个变量的多个值,例如@list
是一个变量,而我为它添加的值是“1,3,4,5,66
”,我需要表中的信息相关到这些id:
SELECT T.C.value('.', 'NVARCHAR(100)') AS [id]
INTO #tblPersons
FROM (SELECT CAST ('<Name>' + REPLACE(@empid, ',', '</Name><Name>') + '</Name>' AS XML) AS [Names]) AS A
cross APPLY Names.nodes('/Name') as T(C)
我在我的sp中使用它,但是我很难将这个值传递给多个值。
答案 0 :(得分:1)
您是否考虑过使用表变量将数据传递给存储过程。例如:
<强>设置强>
CREATE TYPE EmpIdValuesType AS TABLE
(
[EmpID] [int]NOT NULL
)
CREATE TABLE Employee
(
EmpID INT,
Name varchar(20)
)
INSERT INTO Employee
Values
(1, 'test1'),
(2, 'test2'),
(3, 'test3'),
(4, 'test4'),
(5, 'test5'),
(6, 'test6'),
(7, 'test7');
Create Procedure usp_GetEmployees
(
@TableVariable EmpIdValuesType READONLY
)
AS
SELECT *
FROM Employee
INNER JOIN @TableVAriable TV
ON Employee.EmpId = TV.EmpId
现在,您可以将行插入到EmpIdValuesType类型的临时表变量中,并将该变量传递给存储过程:
调用存储过程
DECLARE @empIds AS EmpIdValuesType
INSERT INTO @empIds(EmpID) Values (1), (2), (5)
Exec usp_GetEmployees @empIds;
以下是结果: