有人可以解释为什么下面给我“必须声明标量变量@facilities”。但是如果我使用 VARCHAR 而不是我创建的类型,效果会很好吗?我怎样才能解决这个问题?
TIA
--CREATE TYPE integer_list AS TABLE (n int NOT NULL PRIMARY KEY)
--DROP PROCEDURE spTestTVP
CREATE PROCEDURE spTestTVP (
@facilities integer_list READONLY
--@facilities varchar(100)
)
AS
DECLARE @sql nvarchar(4000)
SET @sql = 'SELECT * FROM TestTable'
SET @sql = @sql + ' WHERE 1=1 '
IF @facilities IS NOT NULL
SET @sql = @sql + ' AND (FacilityNo IN (' + @facilities + ') OR FacilityNo IS NULL)'
EXEC sp_executesql @sql