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 July 22nd, 2003, 04:02 PM
Authorized User
 
Join Date: Jun 2003
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default SP Variable

Ok – real easy question, I’m sure – but I can’t sort it out.

Below is a code snippet where I want to turn this into a SP, and pass in the value of @ActivityChoice. I’ve hard coded the value below to be ‘RC’ but I want it to have more values dependant on what was passed.

It can be any combination or just one of the following: RC, I, RI, FO.

DECLARE @ActivityChoice CHAR(2)
SELECT @ActivityChoice = "RC"
--Want to pass in value of @ActivityChoice
--example values could be RC, I, RI, FO

select enfact_stat_code
from enfact
where enfact_stat_code in (@ActivityChoice)

Right now it returns:
enfact_stat_code
----------------
RC
RC
RC
RC
RC
RC
RC

A sample output when I get it working would be, if I passed it ‘RC’, and ‘FO’ and ‘I”
enfact_stat_code
----------------
FO
FO
RC
I
I
RC
FO
RC
RC
RC
I
FO
RC
RC
__________________
_________________________
Joe Horton
Database Developer / Software Engineer
WISHA/Legal Services Software Development
Department of Labor and Industries
Voice (360) 902-5928 fax (360) 902-6200
 
Old July 22nd, 2003, 04:47 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

It's not so easy a question. You cannot do what you want directly.

There are a few ways this can be done; one of them is dynamic SQL - you build a string containing the SQL along with the IN clause parameter and then execute it, i.e.
Code:
    SET @SQL='SELECT enfact_stat_code FROM enfact WHERE enfact_stat_code  IN (' + @ActivityChoice + ')')
    exec @SQL
This assumes your input parameter contains quoted strings delimited by a comma.

There are a lot of reasons why dynamic SQL is generally not recommended.

Another way if you are using SQL Server 2000, is to create the following user defined function. This function takes your comma delimited string of values (this time without quotes) and parses it and returns a table variable. You can then use this table variable in your SQL as, for example:
Code:
    SELECT enfact_stat_code FROM enfact WHERE enfact_stat_code IN
        (SELECT VAL FROM dbo.inlist(@ActivityChoice ))
This is the UDF:
Code:
    CREATE FUNCTION Inlist (@list varchar(8000))
    RETURNS @tbl TABLE (val varchar(10) not null)  AS
    BEGIN
        Declare @index int,
            @pos int,
            @str varchar(8000)
        Set @pos = 1
        Set @index = 1
        While @index > 0
            BEGIN
                set @index = charindex(',', @list, @pos)
                IF @index > 0
                    Set @str = substring(@list, @pos, @index - @pos)
                ELSE
                    Set @str = substring(@list, @pos, Len(@list))
                Set @str = ltrim(rtrim(@str))
                INSERT @tbl (val) VALUES (@str)
                Set @pos = @index + 1
            END
        RETURN
    END


Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old July 22nd, 2003, 06:01 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 215
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There is a comprehensive list of methods for doing this which you can find at:
http://www.algonet.se/~sommar/arrays-in-sql.html

regards
David Cameron





Similar Threads
Thread Thread Starter Forum Replies Last Post
SP Encryption carumuga SQL Server 2005 1 September 20th, 2008 10:30 AM
ASSIGNING A JAVA SCRIPT VARIABLE TO A XSL VARIABLE SOMANATHAN10 XSLT 1 February 21st, 2007 04:26 AM
exec sp within another sp collie SQL Server 2000 1 December 22nd, 2004 05:46 AM
SP used in a SP Mitch SQL Server 2000 16 October 28th, 2003 12:59 PM
Can a SP run another SP as sa? dbradley SQL Server 2000 0 July 17th, 2003 08:35 AM





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