0

it's possible to pass a Where statement through declared variable in SQL Server 2008?, How can I do something like this?

declare @foo as nvarchar(max) = '9510,9580,5030'
SELECT T1.[ItemCode]
FROM tbl1 T1
WHERE T1.ItemCode in (@foo)

When I run this code my foo variable can hold at least one item or 100 items, that depends on another result

1

2 Answers 2

1

You can do this by using exec.

declare @foo as nvarchar(max) = '9510,9580,5030'

declare @SQLString as nvarchar(max)
set @SQLString  = 'SELECT T1.[ItemCode]
                   FROM tbl1 T1
                   WHERE T1.ItemCode in (' + @foo + ')'
exec (@SQLString )
Sign up to request clarification or add additional context in comments.

Comments

0

I would use table variable:

DECLARE @foo AS NVARCHAR(max) = 'yourstring'

DECLARE @SQLString table (name NVARCHAR(max))

    INSERT INTO @SQLString
    SELECT T1.[ItemCode]
    FROM tbl1 T1
    WHERE T1.ItemCode LIKE ('%' + @foo + '%')

SELECT * FROM @SQLString

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.