Click here to Skip to main content
15,908,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
I want to write sp for list of users having ids ('1','2')

the query will be:

select * from Table_USERS
where userid in ('1','2')


But the list of userids i.e.('1','2','3'...) will come as parameter. So i declare a variable named @userid as follows:

declare @userid varchar(100);
set @userid='''1'',''2''';
select * from Table_USERS
where userid in
(CASE WHEN @userid <>''
THEN cast(@userid as varchar(20))
ELSE userid END)


But it giving error: "Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value ''1','2'' to data type int."

Note: My Requirement If @userid is '' then all the records should listed else the records where ids exists in parameter @userid.

Thanks in advance.
Posted

I think you should be used Dynamic sql for this problem.
This link may be useful for you <a href="http://www.codeproject.com/KB/database/Building_Dynamic_SQL.aspx"></a>
 
Share this answer
 
Comments
tanishtaman 15-Dec-11 6:59am    
thanks buddy...but unable to do from that.
Jigar Bagadai 15-Dec-11 7:02am    
which kind of problem you facing using this link ?
tanishtaman 15-Dec-11 7:06am    
prob is solved. thanks...
Try using the below piece of transaction to solve your problem :

SQL
declare @userid varchar(100)
--set @userid='''109453'',''109454'''
set @userid = ''''
declare @sql varchar(200)
set @sql = (CASE WHEN @userid <>'''' THEN 'select * from Table_USERS
 where userid in(' + @userid + ')' ELSE 'select * from Table_USERS' END)
exec (@sql)
 
Share this answer
 
Comments
tanishtaman 15-Dec-11 6:58am    
great job! Thanks
userid in
(CASE WHEN @userid <>''
 THEN cast(@userid as varchar(20)) 
ELSE userid END)

userid is string but your case statement returns a varchar hence the issue.
You need to ensure your case statement returns an int, so cast the WHEN clause output into an int.
 
Share this answer
 
v2
Use split function, splitting by ","
Below is SQL Function for splitting by "," to a parameter
==================================
USE [EBS_Local]
GO
/****** Object: UserDefinedFunction [dbo].[Split] Script Date: 12/15/2011 16:57:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <author,,bhavik>
-- Create date: <create>
-- Description: <split string="">
-- =============================================
ALTER FUNCTION [dbo].[Split](@String varchar(8000), @Delimiter char(1)) --'1,2,3,4,5',','
returns @temptable TABLE (items varchar(8000))
as
begin
declare @idx int
declare @slice varchar(8000)

select @idx = 1
if len(@String)<1 or @String is null return

while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String

if(len(@slice)>0)
insert into @temptable(Items) values(@slice)

set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end
=============================

Use this function now write query

Select * from tableName where userid in (Select items from dbo.split(@UserID))

it will solve your problem.
 
Share this answer
 
Comments
tanishtaman 15-Dec-11 6:56am    
hi, thanks for replying....
Your solution did not help my prob.

Error coming:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900