Results 1 to 4 of 4

Thread: [Resolved] How to declare array on Stored Proc.

  1. #1

    Thread Starter
    Hyperactive Member naruponk's Avatar
    Join Date
    Feb 2004
    Location
    Some where in the world
    Posts
    423

    [Resolved] How to declare array on Stored Proc.

    Hi, there

    Does any one know that how to declare an array on Stored Procedure.

    VB Code:
    1. Dim Arr() As int
    2. Redim Arr(5)

    How can i convert above code to Stored Procedure?

    Thanks
    Last edited by naruponk; May 14th, 2005 at 10:40 PM.

  2. #2
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: How to declare array on Stored Proc.

    SQL Server doesn't have arrays, but there are ways to simulate arrays. One way is to use UDF (user defined functions) and a varchar variable the holds a comma/semicolon separated list of your array elements. Create one UDF that adds/updates an element into the variable and another UDF to return an element from a position in the variable.

    You can also take a look at this page to get more ideas http://www.sommarskog.se/arrays-in-sql.html

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to declare array on Stored Proc.

    Quote Originally Posted by naruponk
    Hi, there

    Does any one know that how to declare an array on Stored Procedure.

    VB Code:
    1. Dim Arr() As int
    2. Redim Arr(5)

    How can i convert above code to Stored Procedure?

    Thanks
    Code:
    Declare @Arr Table (Slot int, Val int)
    
    Insert Into @Arr (1,123)
    Insert Into @Arr (2,456)
    Insert Into @Arr (3,789)
    Insert Into @Arr (4,112233)
    Insert Into @Arr (5,445566)
    In a way that TABLE VARIABLE becomes an array - the SLOT column mimics the ARRAY size and the VAL column is your INTEGER value.

    You could use it as:

    Code:
    Set @SOMEVARIABLE=(SELECT VAL FROM @ARR WHERE SLOT=3)
    That little sub-query shown to the right of the = SIGN could be used pretty much anywhere - INSERT, UPDATE, WHERE clauses.

    The UDF method described by the prior post will work as well.

    But the bigger question is why? For the most part arrays are for iterative/logic driven languages - T-SQL is supposed to be "set-based" - and letting go of those "language" constructs can be challenging - but very well worth it.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

    Thread Starter
    Hyperactive Member naruponk's Avatar
    Join Date
    Feb 2004
    Location
    Some where in the world
    Posts
    423

    Talking Re: How to declare array on Stored Proc.

    Thanks kaffenils,szlamany

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