In in SQL

I have a string like '36123', '312132', '13124', which I am passing from my code.
My sp look likse select *from mytable where itemno in (mystring). So far, If I do this exec mysp '36123', '312132', '13124', it return nothing. If I pass one is works just fine. What should I do?
VBdotnet2005Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
You can review this article for different ways to handle this issue:
http://www.experts-exchange.com/articles/1536/Delimited-list-as-parameter-what-are-the-options.html
0
Vikas GargBusiness Intelligence DeveloperCommented:
Hi,

You can create a function which will return comma separated value to list which you can use in the in clause.

USE [StagingAreaKPIWStandard]
GO

/****** Object:  UserDefinedFunction [dbo].[FN_Split]    Script Date: 5/27/2015 10:24:24 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE FUNCTION [dbo].[FN_Split]
(
  @sInputList VARCHAR(8000),
  @sDelimiter CHAR(1) = ','
) RETURNS @List TABLE (row [int] identity(0,1) not null, item VARCHAR(8000))
/**
select * from FN_Split('hello,world,this,is,a,test',',')
****/
BEGIN

DECLARE @sItem VARCHAR(8000)

SET @sItem = ''

WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
BEGIN
    SET @sItem= RTRIM(LTRIM(SUBSTRING(@sInputList,1, CHARINDEX(@sDelimiter, @sInputList, 0) - 1)))
    SET @sInputList= RTRIM(LTRIM(SUBSTRING(@sInputList, CHARINDEX(@sDelimiter, @sInputList, 0)+LEN(@sDelimiter),LEN(@sInputList))))
    INSERT INTO @List SELECT @sItem
END

INSERT INTO @List SELECT CASE WHEN LEN(@sInputList) < 1 THEN NULL ELSE @sInputList END

RETURN


END 


GO

Open in new window


select *from mytable where itemno in (SELECT ISNULL(ITEM,'0') Item FROM dbo.FN_Split(mystring,','))
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Because your stored procedure is only accepting one parameter and you're passing many (exec mysp '36123', '312132', '13124'). The '36123', '312132', '13124' should be treated as a string so it can be a single parameter:
exec mysp '''36123''', ''312132'', ''13124'''

Open in new window

Also check the parameter data type
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.