how to pass my value in query string

RAVI 1,056 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

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,507 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,992 questions
{count} votes

Accepted answer
  1. QiYou-MSFT 4,321 Reputation points Microsoft Vendor
    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 112.7K Reputation points MVP
    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 Answers by the question author, which helps users to know the answer solved the author's problem.