Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to retrieve the data when a comma separated ids are sent as input parameters


how to retrieve the data when a comma separated ids are sent as input parameters

Author
Message
ramsai1973
ramsai1973
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 25
Dear all,

I have two tables namely:
1. User table
2. Location table

Given below are their design details:

User table columns:
UserID INT, (Primary Key)
UserForename VARCHAR(200),
UserSurname VARCHAR(200),
LocationID INt (foriegn key)

Location table columns:
LocationID INT, (Primary Key)
LocationName VARCHAR(200)


Issue:
I need a store procedure to retrieve user details based on the LocationIDs passed as the input parameter where the input parameter is the array of LocationIDs separated by comma. [for eg: '1,2,3']

Please kindly help me with a stored procedure as explained above.

Thanks in advance,
Ram
anthony.green
anthony.green
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6108 Visits: 6080
One method would be a string splitter.

Take a look in my signature for Jeff's string splitter and call it in your query


declare @temp table(Userid int, forename varchar(200), surname varchar(200), locationid int)
insert into @temp values (1,'ant','green',1),(2,'green','ant',2)
declare @param varchar(10) = '1,2'
SELECT
UserID,
Forename,
Surname
FROM
@temp t
INNER JOIN
dbo.DelimitedSplit8K(@param,',') f
on
t.locationid = f.Item





Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
When a question, really isn't a question - Jeff Smith
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Pulivarthi Sasidhar
Pulivarthi Sasidhar
UDP Broadcaster
UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)

Group: General Forum Members
Points: 1448 Visits: 992
CREATE Proc usersbyLocation(@locids varchar(max)=NULL)
AS
BEGIN
Declare @str varchar(max)
SET @str='SELECT UserID,
UserForename,
UserSurname,
LocationName
from tUser JOIN
tLocation on tUser.locationID=tLocation.LocationID
Where tLocation.LocationID in ('+@locids+')'
PRINT @locids

Exec (@str)
END
--Test
--Exec usersbyLocation '1,2,3,4,5,6'
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24295 Visits: 37999
Pulivarthi Sasidhar (3/25/2013)



CREATE Proc usersbyLocation(@locids varchar(max)=NULL)
AS
BEGIN
Declare @str varchar(max)
SET @str='SELECT UserID,
UserForename,
UserSurname,
LocationName
from tUser JOIN
tLocation on tUser.locationID=tLocation.LocationID
Where tLocation.LocationID in ('+@locids+')'
PRINT @locids

Exec (@str)
END
--Test
--Exec usersbyLocation '1,2,3,4,5,6'


Look above, no dynamic sql required.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search