Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

how to retrieve the data when a comma separated ids are sent as input parameters Expand / Collapse
Author
Message
Posted Monday, March 25, 2013 6:05 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, March 25, 2013 6:03 AM
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
Post #1434851
Posted Monday, March 25, 2013 6:24 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:23 AM
Points: 5,217, Visits: 5,065
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
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

Post #1434868
Posted Monday, March 25, 2013 6:29 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 3:44 AM
Points: 1,059, Visits: 696



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'
Post #1434873
Posted Monday, March 25, 2013 8:48 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 7:09 PM
Points: 23,218, Visits: 31,905
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.



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)
Post #1434951
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse