question

David 2 1 avatar image
David 2 1 asked

How To Build Dynamic Query Based On Multi Value Parameter?

Hi there, I'm in a bit of a pickle trying to solve this question. How can I dyanimcally build up a sql query based on a varying multi value parameter list? Basically when I run the stored procedure say as below: EXEC test_address 'Smyth', 'A,B,C' I need the query in the stored procedure to be dynamically built such as: SELECT name,address,postcode FROM address_file WHERE name = 'Smyth' AND (postcode LIKE 'A%' OR postcode LIKE 'B%' OR postcode LIKE 'C%') However the issue is that there may not always be 3 postcodes passed in. There could be any number such like: EXEC test_address 'Smyth', 'A,B,C,D,E,F,G' Or even: EXEC test_address 'Smyth', 'Z' How can I dynamically build the 'OR postcode LIKE...' in the WHERE clause to be based on the multi value postcode parameter? TIA
sql-server-2000stored-procedurestsqlparametersdynamic
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

0 Answers

·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.