DeveloperBarn Forums

Go Back   DeveloperBarn Forums > Databases > SQL Development

Discuss "Looping through variables." in the SQL Development forum.

SQL Development - Structured Query Language (SQL) is the talk of databases. Discuss topics such as joins, triggers and other advanced SQL topics.


Reply « Previous Thread | Next Thread »  
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old June 4th, 2008, 11:32 AM
mehere's Avatar
Global Sarcasm Mistress

 
Join Date: Mar 2008
Location: Wide Awake In Dreamland
Posts: 113
Thanks: 10
Thanked 21 Times in 19 Posts
Rep Power: 1
mehere is on a distinguished road

Awards Showcase
Microsoft SQL Server Classic ASP 
Total Awards: 2

Default Looping through variables.

I have a CURSOR (I know, shouldn’t use them, but I have no other way) and I have declared several variables like so:

Code:
Declare @strAttrib_1 varchar(10)
Declare @strAttrib_2 varchar(10)
Declare @strAttrib_3 varchar(10)

Declare @i int

Set @i = 0
Now what I was hoping to do was loop through those to see if they contain various values. Something like this:
Code:
WHILE @i < 4
BEGIN
            IF @strAttrib_+@i = ‘something’
            BEGIN
                        --do something here
            END
            IF @strAttrib+@i = ‘somethingelse’
            BEGIN
                        --do something else here
            END
            --ETC
END
Is there a way to evaluate @strAttrib_ with the value in @i to get the actual value from the variable that was declared?
__________________
Quote of the Month:
Sanity: Minds are like parachutes. Just because you've lost yours doesn't mean you can borrow mine.

Questions to Ponder:
Why is the third hand on the watch called the second hand?

iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
copyright © 2008 sbenj69
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old June 4th, 2008, 11:46 AM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 543
Thanks: 14
Thanked 41 Times in 40 Posts
Blog Entries: 2
Rep Power: 1
jmurrayhead will become famous soon enough

Awards Showcase
Microsoft .Net Microsoft SQL Server Microsoft Windows Classic ASP 
Total Awards: 4

Default

Hmm...not sure if that can be done. But I know in SQL Server 2005, you can use lists and arrays. Not sure if this could help you or not, but this link shows something that looks like what you're trying to do: http://www.sommarskog.se/arrays-in-sql-2005.html

Code:
CREATE FUNCTION iter$simple_intlist_to_tbl (@list nvarchar(MAX))
   RETURNS @tbl TABLE (number int NOT NULL) AS
BEGIN
   DECLARE @pos        int,
           @nextpos    int,
           @valuelen   int

   SELECT @pos = 0, @nextpos = 1

   WHILE @nextpos > 0
   BEGIN
      SELECT @nextpos = charindex(',', @list, @pos + 1)
      SELECT @valuelen = CASE WHEN @nextpos > 0
                              THEN @nextpos
                              ELSE len(@list) + 1
                         END - @pos - 1
      INSERT @tbl (number)
         VALUES (convert(int, substring(@list, @pos + 1, @valuelen)))
      SELECT @pos = @nextpos
   END
  RETURN
END
__________________
jmurrayhead
Did I help you out? Make me popular by clicking the icon!

If you found a post helpful, please click the button in the lower right-hand corner of the post.

Powered by ASP.Net
Reply With Quote
  #3 (permalink)  
Old June 4th, 2008, 12:11 PM
mehere's Avatar
Global Sarcasm Mistress

 
Join Date: Mar 2008
Location: Wide Awake In Dreamland
Posts: 113
Thanks: 10
Thanked 21 Times in 19 Posts
Rep Power: 1
mehere is on a distinguished road

Awards Showcase
Microsoft SQL Server Classic ASP 
Total Awards: 2

Default

well ... to make a long story short ... those variables will represent columns in a table (15 total) that contain a value to determine particular attributes for a driver. i need to see what attributes that driver has and insert that info into another table that is being used for another program to do some load testing. other than running 15 queries and dozens of "IF" statements, i was trying to condense it. yes, i'm a programmer and i'm lazy.

and j ... thanks. i saw that link along with another one for SQL Server 2000, my problem though, is some attributes have dates associated with them, they're in another 15 fields, but not all. i thought about putting them both in an array, but then i wouldn't know which date went with which attribute from the array. and yes, i did try it. if a driver had 8 attributes but only 3 of them had dates i would get an attribure array of 8 values but only a date array of 3 ... kind of hard to match up.

Last edited by mehere; June 4th, 2008 at 12:16 PM. Reason: to add more detail ...
Reply With Quote
Reply

  DeveloperBarn Forums > Databases > SQL Development

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


Sponsored Links

ASP.NET Resource Index
a directory of ASP.NET tutorials, applications, scripts, assemblies and articles for the novice to professional developer.

Free Web Directory
Including Chats and Forums Resources, Offer automatic, instant and free directory submissions.
URLZ Web Directory
URLZ Web Directory

Free Web Directory - Add Your Link
The Little Web Directory
Free Web Directory
Pegasus free web directory is a free directory organised by categories.

Web Directory & SEO Services
dirroot web directory


All times are GMT -4. The time now is 12:37 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
Copyright © 2008 DeveloperBarn.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46