Home > Enterprise >  Send multi values ​to an OpenQuery
Send multi values ​to an OpenQuery

Time:07-05

I have the following query in sql server

Example:

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

But it shows me an error since it does not receive dynamic parameters.

CodePudding user response:

Does this help?

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

Reference

CodePudding user response:

I made my own solution. (it worked for me)

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

The result returns: 'AA'',''NF'

I share it in case someone needs it.

  • Related