how to pass my value in query string

RAVI 1,076 Reputation points
2023-07-31T06:57:13.6766667+00:00

Hello

My value will be like this A1,A2,A6

how to pass this in select * from table1 where Field1 storeprocedure so that this can pull all A1 & A2 & A6 data from table

Thanking You

Developer technologies | ASP.NET | Other
SQL Server | Other
{count} votes

Answer accepted by question author
  1. QiYou-MSFT 4,341 Reputation points Microsoft External Staff
    2023-07-31T10:11:55.77+00:00

    Hi @RAVI

    Here is my test table:

    Picture31

    Query String:

    CREATE proc FieldExample @ID1 nvarchar(MAX),@ID2 nvarchar(MAX),@ID3 nvarchar(MAX)
    AS
    BEGIN
    SELECT*FROM dbo.Test2 WHERE ID in(@ID1,@ID2,@ID3)
    END
    
    
    

    Execute the statement:

    exec FieldExample 'A1','A2','A6'
    

    OutPut:

    T

    Best regards,
    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Erland Sommarskog 128.7K Reputation points MVP Volunteer Moderator
    2023-07-31T08:26:06.35+00:00

    One way is to pass the comma-separated list to SQL Server and then use a function to split it into table format. I have a shorter article on my web site for techniques to do this: Arrays and Lists in SQL Server

    But a better option in my opinion is to pass a table-valued parameter and split the list already in the client. I have an article about this on my web site as well: Using Table-Valued Parameters in SQL Server and .NET

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.