Results 1 to 3 of 3

Thread: [RESOLVED] SPROC - Deliminated String

  1. #1

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Resolved [RESOLVED] SPROC - Deliminated String

    With help from techgnome and szlamany I am attempting to write a SPROC that will accept a delimited string (ie: Office 1,Office 2,Office 3) as a parameter. So after reviewing this POST by szlamany, I attempted to create a SPROC:

    VB Code:
    1. GO
    2. CREATE PROCEDURE dbo.TestSP
    3.     /*Declare Variables*/
    4.     @ListStr varchar(100) /*Hold Delimited String*/
    5.     @ListTbl Table (InvUnit varchar(50)) /*Creates Temp Table*/
    6.     @CP int /*Len of String */
    7.     @SV varchar(50) /*Holds Result */
    8.  
    9. AS
    10.  
    11. While @ListStr<>''
    12. Begin
    13.     Set @CP=CharIndex(',',@ListStr) /*Sets length of words - Instr */
    14.     If @CP<>0
    15.     Begin
    16.         Set @SV=Cast(Left(@ListStr,@CP-1) as varchar) /*Copies Portion of String*/
    17.         Set @ListStr=Right(@ListStr,Len(@ListStr)-@CP) /*Sets up next portion of string*/
    18.     End
    19.     Else
    20.     Begin
    21.         Set @SV=Cast(@ListStr as varchar)
    22.         Set @ListStr=''
    23.     End
    24.     Insert into @ListTbl Values (@SV) /*Inserts variable into Temp Table*/
    25. End

    But when I run it I get the following errors:

    Msg 102, Level 15, State 1, Procedure TestSP, Line 5
    Incorrect syntax near '@ListTbl'.
    Msg 137, Level 15, State 2, Procedure TestSP, Line 9
    Must declare the scalar variable "@ListStr".
    Msg 137, Level 15, State 2, Procedure TestSP, Line 11
    Must declare the scalar variable "@ListStr".
    Msg 137, Level 15, State 2, Procedure TestSP, Line 12
    Must declare the scalar variable "@CP".
    Msg 137, Level 15, State 2, Procedure TestSP, Line 14
    Must declare the scalar variable "@ListStr".
    Msg 137, Level 15, State 2, Procedure TestSP, Line 15
    Must declare the scalar variable "@ListStr".
    Msg 137, Level 15, State 2, Procedure TestSP, Line 19
    Must declare the scalar variable "@ListStr".
    Msg 137, Level 15, State 1, Procedure TestSP, Line 20
    Must declare the scalar variable "@ListStr".
    Msg 1087, Level 15, State 2, Procedure TestSP, Line 22
    Must declare the table variable "@ListTbl".

    I don't understand these error, does anyone have any ideas?


    P.S. I want this SPROC to run against either a MSSQL 7.0 or MSSQL 2005 Database.
    Thanks!
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: SPROC - Deliminated String

    It's been a while for me... but shouldn't there be brackets around the parameter list, and/or commas between each parameter?

  3. #3

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: SPROC - Deliminated String

    Stupid mistake the "AS" was in the wrong place, here is my corrected code:

    VB Code:
    1. IF EXISTS (select * from syscomments where id = object_id ('dbo.TestSP'))
    2.     DROP PROCEDURE dbo.TestSP
    3.  
    4. GO
    5. CREATE PROCEDURE dbo.TestSP
    6.     /*Declare Variables*/
    7.     @ListStr varchar(100) /*Hold Delimited String*/
    8. AS
    9.  
    10.  
    11. DECLARE @ListTbl Table (InvUnit varchar(50)) /*Creates Temp Table*/
    12. DECLARE @CP int /*Len of String */
    13. DECLARE @SV varchar(50) /*Holds Result */
    14.  
    15. While @ListStr<>''
    16. Begin
    17.     Set @CP=CharIndex(',',@ListStr) /*Sets length of words - Instr */
    18.     If @CP<>0
    19.     Begin
    20.         Set @SV=Cast(Left(@ListStr,@CP-1) as varchar) /*Copies Portion of String*/
    21.         Set @ListStr=Right(@ListStr,Len(@ListStr)-@CP) /*Sets up next portion of string*/
    22.     End
    23.     Else
    24.     Begin
    25.         Set @SV=Cast(@ListStr as varchar)
    26.         Set @ListStr=''
    27.     End
    28.     Insert into @ListTbl Values (@SV) /*Inserts variable into Temp Table*/
    29. End
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width