在将表类型用作存储过程中的参数时遇到麻烦。
This is my table type:
CREATE TYPE [dbo].[ TableTypeMyValue] AS TABLE(
[MyValue] [nvarchar](50) NULL
)
GO
我需要从c#传递@MyValue:
@BCID nvarchar(20),
@MyValue TableTypeMyValue READONLY
AS
BEGIN
SET NOCOUNT ON
SELECT IIF (EXISTS (SELECT bcc.BCID, ocv.CAID, ocv.CAValue
FROM BCTWO AS ocv
INNER JOIN BCONE AS bcc ON bcc.MyValue = ocv.MyValue
WHERE ocv.MyValue = @MyValue AND bcc.[Part of the key] = 1 AND bcc.BCID = @BCID), 1, NULL)
END
这是我得到的错误:
`Must declare the table variable @MyValue`.
我认为表类型参数有些问题。我没有正确使用它。有人可以帮忙吗?
谢谢!
答案 0 :(得分:1)
You need to treat @CharacteristicValue
as a table, not the same as a scalar variable. You should probably join to it and have ocv.CharacteristicValue = @CharacteristicValue
instead be an ON
condition that references the CharacteristicValue
column within the table.
答案 1 :(得分:1)
您正在尝试使用表类型参数,例如标量参数,特别是:
ocv.CharacteristicValue = @CharacteristicValue
您需要使用JOIN
或EXISTS
子句。例如:
WHERE EXISTS (SELECT 1
FROM @CharacteristicValue CV
WHERE CV.CharacteristicValue = ocv.CharacteristicValue)