patternsqlMinor
MS SQL server set variable to where clause
Viewed 0 times
sqlwhereservervariableclauseset
Problem
I am new in MS SQL server and oracle database
I have the following query:
but I need something like this:
problem is how can I set value
I have the following query:
SELECT * from TBL1 where adc in ('21','24');but I need something like this:
DECLARE @sqlq nvarchar(500)
Set @sqlq = '21','23';--(how can I write this)
SELECT* from OPENQUERY([CBSCSDB], 'SELECT * from TBL1 where adc in (@sqlq)');problem is how can I set value
@sqlq and put it in the select statement in where clause.Solution
You can use table variables
DECLARE @sqlq TABLE
(
adc VARCHAR(100)
)
INSERT INTO @sqlq VALUES (21);
INSERT INTO @sqlq VALUES (23);
SELECT * FROM TBL1 WHERE adc IN (SELECT adc FROM @sqlsq)Code Snippets
DECLARE @sqlq TABLE
(
adc VARCHAR(100)
)
INSERT INTO @sqlq VALUES (21);
INSERT INTO @sqlq VALUES (23);
SELECT * FROM TBL1 WHERE adc IN (SELECT adc FROM @sqlsq)Context
StackExchange Database Administrators Q#153768, answer score: 3
Revisions (0)
No revisions yet.