Click here to Skip to main content
16,020,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created a stored procedure in SQL server 2008:-
SQL
CREATE PROCEDURE usp_select
@ProfcenCd NVARCHAR(3) 
AS
SELECT * FROM Profcen  WHERE PROFCEN_CD IN (@ProfcenCd)
GO

When i execute sp with a single value then it works correct and display record. E.g.:-
EXEC sp_select '1'

but when i pass multiple values then it doesn't display any record. 
EXEC sp_select '1,8'

Need Suggestion
Posted
Updated 21-May-13 3:37am
v2
Comments
Prasaad SJ 21-May-13 5:26am    
May I know what is question in Detail?? You are using only one arguments then you will be allowed to use the PROC only with one arguments.
Mayank Topiwala 21-May-13 5:46am    
Hi Prasad
Please look into select query. It contains IN keyword.
For this I need to pass multiples value in a single argument.
gvprabu 21-May-13 9:53am    
you can go for Dynamic Query.... I think it will easy for u in this case.

create this function
SQL
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[ParseValues]
(@String varchar(8000), @Delimiter varchar(10) )
RETURNS @RESULTS TABLE (ID int identity(1,1), Val varchar(50))
AS
BEGIN
    DECLARE @Value varchar(100)
    WHILE @String is not null
    BEGIN
        SELECT @Value=CASE WHEN PATINDEX('%'+@Delimiter+'%',@String) >0 THEN LEFT(@String,PATINDEX('%'+@Delimiter+'%',@String)-1) ELSE @String END, @String=CASE WHEN PATINDEX('%'+@Delimiter+'%',@String) >0 THEN SUBSTRING(@String,PATINDEX('%'+@Delimiter+'%',@String)+LEN(@Delimiter),LEN(@String)) ELSE NULL END
        INSERT INTO @RESULTS (Val)
        SELECT @Value
    END
RETURN
END

Now edit your sp
SQL
ALTER PROCEDURE usp_select
 @ProfcenCd NVARCHAR(3) 
AS
 SELECT * FROM Profcen WHERE PROFCEN_CD IN (select val from dbo.ParseValues(@ProfcenCd,',') where val <> '')
 GO

Happy Coding!
:)
 
Share this answer
 
Comments
Mayank Topiwala 21-May-13 5:42am    
Thanx aarti it works fine!!
just for example i pass 2 values but it don't show records with more than 2 values.
So how to to do this. Is there any changes I should do in SP ?
Aarti Meswania 21-May-13 5:47am    
Welcome!
see your procedure parameter declaration
you have wrote
@ProfcenCd NVARCHAR(3)
increase limit
@ProfcenCd NVARCHAR(800) or @ProfcenCd NVARCHAR(Max)
Mayank Topiwala 21-May-13 5:50am    
Thanx a lot aarti!! you solved my problem.
Aarti Meswania 21-May-13 5:51am    
Welcome!
Glad to help you! :)
Of course not, it thinkt that you're going to get record 1.8/1,8.

You need to alter the procedure so that you pass it a list of ids - or simply call the SP twice!
 
Share this answer
 
v2
Hi,

You can use Dynamic Query as follows...
SQL
CREATE PROCEDURE usp_select
      @ProfcenCd NVARCHAR(300)  -- 3 size is not enof for multiple values
AS
BEGIN
	DECLARE @SqlSQuery VARCHAR(max)
	SELECT @SqlSQuery = 'SELECT * FROM Profcen WHERE PROFCEN_CD IN ('+@ProfcenCd+')'
	PRINT @SqlSQuery 
	EXEC(@SqlSQuery)
END
GO


Check the link also....
Execute Dynamic SQL commands in SQL Server[^]
 
Share this answer
 
 
Share this answer
 

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