Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use IN Operator in SQL Server

Tags:

sql

sql-server

How to use IN Operator in SQL Server

Here Is the table Structure

Create Table Sample(Id INT,Name Varchar(50))

While I am the Query like this I can get the Value

Select * FROM Sample WHERE Id IN ('74','77','79','80')

While I am executing the above Query I can't able to get the Records Related to that table getting error executing this error.

DECLARE @s VARCHAR(MAX)

SET @s='74','77','79','80'

Select * FROM Sample WHERE Id IN (@s)

1 Answers

You are using wrong way

use the following way

DECLARE @s VARCHAR(MAX)
DECLARE @d VARCHAR(MAX)

SET @s='74 , 77 , 79 , 80'

set @d = 'select * from arinvoice where arinvoiceid in('+@s+')'
exec (@d)

here IN operator use integers collection not string collection..

like image 122
Gaurav Agrawal Avatar answered Dec 05 '25 09:12

Gaurav Agrawal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!