使用表值参数获取sp中的标量变量error

时间:2013-10-21 17:50:21

标签: sql-server table-valued-parameters

我创建了表值参数,如下所示:

CREATE TYPE dbo.ss AS TABLE(ss1 NVARCHAR(5));

然后我写了这样的存储过程:

ALTER PROCEDURE [dbo].[T_TransactionSummary] 
 @locations dbo.ss readonly
as
begin
...............
.............
AND (Location_tbl.Locid IN (@locations))

执行此操作时出现错误:

  

必须声明标量变量“@locations”。

我的存储过程有什么问题

1 个答案:

答案 0 :(得分:5)

真的? I showed you here您无法使用IN (@TVP)但必须使用其他技术,例如WHERE EXISTS

WHERE EXISTS (SELECT 1 FROM @locations WHERE ss1 = Location_tbl.Locid)

你也可以说:

WHERE Location_tbl.Locid IN (SELECT ss1 FROM @locations)

或者:

INNER JOIN @locations AS x
ON x.ss1 = Location_tbl.Locid

原因是@locations现在基本上是一个表,而不是变量或文字值。你不能说WHERE EmployeeID IN (dbo.Employees),对吗?你说WHERE EmployeeID IN (SELECT EmployeeID FROM dbo.Employees)