If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > How to pass a string of interger to a SP with an in() clause

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-29-09, 18:15
mikezx10 mikezx10 is offline
Registered User
 
Join Date: Oct 2003
Posts: 226
How to pass a string of interger to a SP with an in() clause

I need to pass a string to my sp but dont know how the EXEC string is below as well as the sp.

--EXEC [spGetUserCompanyUsers] '1,2,3'
ALTER PROCEDURE [dbo].[spGetUserCompanyUsers]
@CompanyIds varchar(50)
AS
BEGIN

SELECT [User].UserId
, [User].UserDesc
, Companies.CompanyId
FROM [User]
INNER JOIN UserCompany ON [User].UserId = UserCompany.UserId
INNER JOIN Companies ON UserCompany.CompanyId = Companies.CompanyId
WHERE Companies.[Name] IN(@CompanyIds)

END
Reply With Quote
  #2 (permalink)  
Old 09-29-09, 19:19
Brett Kaiser Brett Kaiser is offline
Window Washer
 
Join Date: Nov 2002
Location: Jersey
Posts: 10,303
well it looks ok, except your IN needs to have the string deconstructed and convert to int
__________________
Brett
8-)

It's a Great Day for America everybody!

dbforums Yak CorralRadio 'Rita
dbForums Member List
I'm Good Once as I ever was

The physical order of data in a database has no meaning.
Reply With Quote
  #3 (permalink)  
Old 09-29-09, 19:25
mikezx10 mikezx10 is offline
Registered User
 
Join Date: Oct 2003
Posts: 226
How do i do that?
Reply With Quote
  #4 (permalink)  
Old 09-30-09, 07:11
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
What version of SQL Server are you on?
__________________
George
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 09-30-09, 07:39
mikezx10 mikezx10 is offline
Registered User
 
Join Date: Oct 2003
Posts: 226
SQL2005 is the version
Reply With Quote
  #6 (permalink)  
Old 09-30-09, 11:20
m.timoney m.timoney is offline
Registered User
 
Join Date: Oct 2002
Location: Leicester - UK
Posts: 819
easiest ways are to use the Split function in a CLR function

another is
SQL Tip: HowTo Convert Delimited String to Table
__________________
Definition of a Beginner, Someone who doesn't know the rules.

Definition of an Expert, Someone who knows when to ignore the rules.
Reply With Quote
  #7 (permalink)  
Old 09-30-09, 11:40
m.timoney m.timoney is offline
Registered User
 
Join Date: Oct 2002
Location: Leicester - UK
Posts: 819
actually that example is pretty hideous

i'd probably go for something like

Code:
create function ConvertStringToList
(
@string varchar(1000),
@delimiter char
)
returns @rtn table (VAL int)
as
begin

	declare @index int
	set @index = CHARINDEX(@delimiter, @string)


	IF @index >0
	BEGIN
		set @rtn = ConvertStringToList(SUBSTRING(@string, @index + 1, LEN(@string) - @index),@delimiter,@partiallist)
		insert into @rtn values(SUBSTRING(@string, 1, @index - 1)
	END
	ELSE
		insert into @rtn values(@string)
    return
end
how ever that was written on the fly and hasn't been tested
__________________
Definition of a Beginner, Someone who doesn't know the rules.

Definition of an Expert, Someone who knows when to ignore the rules.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On