我正在尝试在我的select语句中传递一个或多个输入变量
我尝试使用IN语句
DECLARE @LOANKIND VARCHAR(100)
SET @LOANKIND = 'Payables - Loan,Payables - Lines of Credit'
SELECT * from Table
WHERE LOANKIND.KIND_DESC IN (@LOANKIND)
我所有可能的值是:
应付账款-贷款,应付账款-信贷额度,应付账款-可收回赠款,应付账款-赠款,应付账款-债券,应付账款-担保,应收款-贷款,应收款-信贷额度,应收款-可收回赠款,应收款-担保>
答案 0 :(得分:1)
您可以为此使用动态查询。如下所示:
DECLARE @sql nvarchar(max)
DECLARE @LOANKIND VARCHAR(100)
SET @LOANKIND = '''Payables - Loan'', ''Payables - Lines of Credit'''
SET @sql = 'SELECT * from Table WHERE LOANKIND.KIND_DESC IN (' + @LOANKIND + ')'
SP_EXECEUTESQL @sql
答案 1 :(得分:0)
另一种方式。
如果您使用的是SQL 2016+,则可以使用STRING_SPLIT
。否则,您可以下载DelimitedSplit8K。
-- Sample Data
DECLARE @Table TABLE (KIND_DESC VARCHAR(100));
INSERT @Table(KIND_DESC) VALUES ('Payables - Pizza'),('Payables - Loan'),('Payables - Lines of Credit');
-- Solution
DECLARE @LOANKIND VARCHAR(100) = 'Payables - Loan,Payables - Lines of Credit'
SELECT *
FROM @Table AS LOANKIND
JOIN STRING_SPLIT(@LOANKIND,',') AS s
ON LOANKIND.KIND_DESC = s.[value];