Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 12th, 2004, 02:28 AM
bsa bsa is offline
Registered User
 
Join Date: Aug 2004
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
 
Old October 12th, 2004, 02:38 AM
Authorized User
 
Join Date: Jun 2003
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

 
Old October 12th, 2004, 10:05 PM
Friend of Wrox
 
Join Date: Jun 2003
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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.