Go Back   DeveloperBarn Forums > Databases > SQL Development

Sponsored Links

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  
Old June 4th, 2008, 11:32 AM
mehere's Avatar
Super Sarcasm Mistress


 
Join Date: Mar 2008
Location: Wide Awake In Dreamland
Posts: 143
Thanks: 10
Thanked 27 Times in 25 Posts
Rep Power: 1
mehere will become famous soon enough

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:
Regret: It hurts to admit when you make mistakes - but when they're big enough, the pain only lasts a second.

Questions to Ponder:
Could it be that all those trick-or-treaters wearing sheets aren’t going as ghosts but as mattresses?

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  
Old June 4th, 2008, 11:46 AM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 815
Thanks: 19
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft Windows Microsoft .Net Microsoft SQL Server 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
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

Join our Folding team: DeveloperBarn Folding
Reply With Quote
  #3  
Old June 4th, 2008, 12:11 PM
mehere's Avatar
Super Sarcasm Mistress


 
Join Date: Mar 2008
Location: Wide Awake In Dreamland
Posts: 143
Thanks: 10
Thanked 27 Times in 25 Posts
Rep Power: 1
mehere will become famous soon enough

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


All times are GMT -4. The time now is 12:13 PM.



Content Relevant URLs by vBSEO 3.2.0