|
-
May 14th, 2005, 02:42 AM
#1
Thread Starter
Hyperactive Member
[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:
Dim Arr() As int
Redim Arr(5)
How can i convert above code to Stored Procedure?
Thanks
Last edited by naruponk; May 14th, 2005 at 10:40 PM.
-
May 14th, 2005, 05:17 AM
#2
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
-
May 14th, 2005, 08:57 AM
#3
Re: How to declare array on Stored Proc.
 Originally Posted by naruponk
Hi, there
Does any one know that how to declare an array on Stored Procedure.
VB Code:
Dim Arr() As int
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.
-
May 14th, 2005, 10:40 PM
#4
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|