Click here to Skip to main content
15,886,810 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
select this my first query
@advisor_id=30;

SQL
SELECT STUFF((SELECT ',' + cast(advisor_id as varchar) AS [text()] FROM tbl_advisor_registration where create_by_user_id=@advisor_id FOR XML PATH('') ), 1, 1, '' )  as advisor_id



Result is

advisor_id
2,3,4,5,6,7,8

i waana get the advisor_details of this (2,3,4,5,6,7,8)

@advisor_id =2,3,4,5,6,7,8;//here which datatype have to use?

select *from tbl_advior where advisor id in (@advisor_id)

if i use int ..am getting convering error

i cant be use as varchar, so which datatype have to use here?


Thanks
Posted

Make it a dynamic query by appending the @advisor_id as shown. Set the varchar size as per your need.

SQL
declare @query varchar(1000)
declare @advisor_id varchar(1000)

set @query= 'select *from tbl_advior where advisor id in ('+@advisor_id +')'

exec @query
 
Share this answer
 
Comments
King Fisher 24-Jan-14 1:43am    
advisor_id is integer .so ,we cant pass this as string
ArunRajendra 24-Jan-14 1:47am    
I guess you are reading thin from XML and append as varchar? If so this should work just give a try. If you get error post the error will rectify it.
King Fisher 24-Jan-14 2:09am    
Error converting data type varchar to bigint.

this is i amgetting while execute it
ArunRajendra 24-Jan-14 2:56am    
Can you post the full query?
Make a function, execute it in your Database's query window (Don't change anything) :

CREATE FUNCTION [dbo].[Split]
(
	@List nvarchar(2000),
	@SplitOn nvarchar(5)
)  
RETURNS @RtnValue table 
(
		
	Id int identity(1,1),
	Value nvarchar(100)
) 
AS  
BEGIN
	While (Charindex(@SplitOn,@List)>0)
	Begin 
		Insert Into @RtnValue (value)
		Select Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1))) 
			Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List))
	End 
	Insert Into @RtnValue (Value)
	Select Value = ltrim(rtrim(@List))
	Return
END

Then write your query as :

SQL
(
@Ad_ID nvarchar(100)
)
as
select * from tbl_advior where advisor_id IN (select Value from dbo.Split(@Ad_ID,','))


Please mark as answer if it helpful.....
 
Share this answer
 
v2
Comments
King Fisher 25-Jan-14 6:14am    
its working ,thank you
Sujee1 25-Jan-14 6:17am    
Welcome :)

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