一尘不染

向 OpenQuery 发送多个值

sql

我在 sql server 中有以下查询

例子:

sql Declare @typeL varchar(max) SET @typeL = 'AA,NF' OPENQUERY(ORACLEPD, 'SELECT * FROM Ledger where typeLedger in (''+@typeL+'')')

但它向我显示了一个错误,因为它没有接收动态参数。


阅读 89

收藏
2022-07-22

共1个答案

一尘不染

我制定了自己的解决方案。(它对我有用)

DECLARE @var VARCHAR(MAX)  
DECLARE @var2 VARCHAR(MAX)  
DECLARE @tb as TABLE(       
       VALORES VARCHAR(MAX) )

    SET @var = 'AA,NF' 
    SET @var2 = ''

    INSERT INTO @tb 
    SELECT value FROM string_split(@var,',')

    WHILE EXISTS(SELECT *  FROM @tb) 
            BEGIN
            IF (SELECT COUNT(*)FROM @tb)>1  
               BEGIN    
               SET @var2 = @var2 + (''''+(SELECT TOP 1 * FROM @tb)+'''')+''','''     
               DELETE TOP(1) FROM @tb   
               END      
                  ELSE          
                      BEGIN             
                          SET @var2 = @var2 + (''''+(SELECT TOP 1 * FROM @tb)+'''')                       
                          DELETE TOP(1) FROM @tb            END     END


    SELECT @var2

开放查询看起来像这样:

OPENQUERY(ORACLEPD, 'SELECT * FROM Ledger where typeLedger in (''+@var2+'')')

结果@var2 返回:‘AA’‘,’‘NF’

我分享它以防有人需要它。

2022-07-22