MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 19th, 2004, 04:21 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
passing a qry in as a parameter to proc?

possible to pass in a query as a parameter to a stored proc?

Number of constraints right now would make it a lot easier if I could pass in a query that selects all the ID's, tried but couldn't come up w/anything, just have a simple proc that does the deletes on 1 ID @ a time...since there are up to 100 that will need to be deleted, the qry as a param would be much more convenient. below is the proc...Thx for any help.
Code:
CREATE PROCEDURE [dbo].[s_DeletePeople]

@PeopleID int
/* single id to be deleted, tried just passing
 * in a query that resembled a string and using it
 * but it didn't work either.
 */

AS

Delete	from tProjectManager
where	Manager_ID in (@PeopleID)

Delete	from tMerchandiser
where	ID in (@PeopleID)

Delete	from tProjectCall
where	merchandiser_id in (@PeopleID)

Delete 	from tManager
where	ID in (@PeopleID)

Delete	from tDistrictManager
where	ID in (@PeopleID)

Delete	from tPeople
where	ID in (@PeopleID)

....and on and on
GO

Reply With Quote
  #2  
Old July 21st, 2004, 09:23 AM
pickledshark pickledshark is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Blackpool
Posts: 22 pickledshark User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 32 sec
Reputation Power: 0
Send a message via ICQ to pickledshark Send a message via MSN to pickledshark
There are a couple of ways around this problem. It appears that you want to send a list of numbers (ids) to the stored procedure, which is undefined (e.g. it could be one id or one thousand ids).

I have seen this problem solved by passing a string of all the ids, seperated by a delimeter.

So to pass the ids 1,2,3,4 and 5 i would make my string look like this

'|1|2|3|4|5|'

| is the character we can identify as the delimiter

So in my stored procedure i could do the following (where @EmpIDs is the above string)

DELETE FROM Employees
WHERE (PATINDEX('%|'+CONVERT(VarChar,EmpID)+'|%',@EmpIDs) > 0)



Another way to do it would be to use dynamic sql. You can pass a query as a string
e.g. 'SELECT EmpID FROM Employees'

Then use Exec to execute the statement

e.g. EXEC(@Statement)

Your example would be more like:

DECLARE @Test VarChar(100)
SELECT @Test = 'SELECT TOP 10 EmpID FROM Employees (NOLOCK)'
DECLARE @Test1 VarChar(100)
SELECT @Test1 = ' DELETE FROM Employees
WHERE EmpID IN ('+@Test+')'
EXEC (@Test1)

This would delete the top 10 employees.

Hope this gives you some ideas!

Reply With Quote
  #3  
Old July 22nd, 2004, 11:51 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
ya it does...I got this solved yesterday though. I just would up passing in a delimited string of id's (used a comma) and it worked out fine. Thx for the reply!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > passing a qry in as a parameter to proc?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT