我创建了表值参数,如下所示:
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”。
我的存储过程有什么问题
答案 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)
。