Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 12th, 2004, 02:28 AM
bsa bsa is offline
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamic stored procedure

Hi All,
I have a stored procedure which has to select for eg. Customers from mastCustomer table on the basis of the Customers selected by the user in the frontend. While calling the procedure i am passing a string of IDs representing the Customers selected by the user.

The string is like this...
      "1,2,4,5"
Now the ID column is the mastCustomer table is of type Int.

The foll. code is written in the stored procedure..

Create Procedure GetCustomer @strIDs varchar(100) as
   Select * from mastCustomer where ID in @strIDs


The above code doesnt work. Can someone please help me.

Thanks,
BSA
Reply With Quote
  #2 (permalink)  
Old October 12th, 2004, 02:38 AM
Authorized User
 
Join Date: Jun 2003
Location: Manchester, , United Kingdom.
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi
Not sure if this is the best way, but it works for me

CREATE PROCEDURE dbo.updLetterStatus
    @members as varchar(1000)
AS
 EXEC('SELECT * FROM tableName
    WHERE membID IN (' + @members + ') ')

The @members string is pasted to the SP like '1, 2, 3'

Ian

Reply With Quote
  #3 (permalink)  
Old October 12th, 2004, 10:05 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi BSA,

As COMMA is a delimeter for Parameters to procedures, I would suggest you not to pass comma in the @strIDs. This could be worked around in a way that you pass something else in place of COMMA and then within the procedure, convert that somethingelse back to comma.

Eg: From frontend, once the user selects the customers, replace all the comma with something like COLON.
Code:
'Not sure what frontend that you use. If that is ASP, then use
strSql = "Execute GetCustomer @strIDs='" & Replace(strSelectedCustomer, ",", ":") & "'"
So that the @strIDs would contain '1:2:3:4:6'.

Then within you procedure use the same replace function to convert those COLONs back to comma.
Code:
Create Procedure GetCustomer @strIDs varchar(100) as
   Select @strIDs=Replace(@strIDs, ':', ',')
   Select * from mastCustomer where ID in (@strIDs)
   -- Here the @strIDs would be converted back to '1,2,3,4,6'
   -- Also you are missing ()s around @strIDs.
This should be what you are looking for.

Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection
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 Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL in C# class vs. Dynamic Stored Procedure holf BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 October 26th, 2006 06:45 PM
stored procedure dynamic insert harpua Classic ASP Databases 3 January 21st, 2005 12:50 AM
error building dynamic sql in stored procedure paaf64 SQL Server 2000 3 October 6th, 2004 03:08 PM
Stored Procedure - Dynamic Where Clause Terry_Pino BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 July 2nd, 2004 04:39 PM
C# and stored procedure Msmsn C# 1 August 26th, 2003 11:03 PM



All times are GMT -4. The time now is 12:00 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.