-1

我创建了这样的表值参数:

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”。

我的存储过程有什么问题

4

1 回答 1

5

严重地?我在这里向您展示了您不能使用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)

于 2013-10-21T18:06:00.540 回答