What will happen if we run below queries?

Select * From sgt_person where person_id in(1,2);
Select * From sgt_person where person_id in('1','2');

Declare @emp_ids varchar(100) = '1,2';
Select * From sgt_person where person_id in(@emp_ids);

 Posted by vishalneeraj-24503 on 7/2/2014 | Category: Sql Server Interview questions | Views: 481 | Points: 40
Answer:

It will give records for 1st and 2nd queries but for 3rd query,it will generate error saying that

Msg 245, Level 16, State 1, Line 5
Conversion failed when converting the varchar value '1,2' to data type int.


1,2 will be treated as single string value and when passed in IN clause will give error as above.


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response