| Author |
Topic |
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-06-02 : 22:29:41
|
| Hi, i wanna know are sql server 05 allow us to do the following statement??DECLARE @xxxx INTSET @xxxx = 0WHILE @xxxx<13BEGINIF @yyyy(@xxxx) IS NULLSET @yyyy(@xxxx) = 0END |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-06-02 : 23:16:32
|
| maybe i was not clear...heres my example code with errorDECLARE @x INTDECLARE @y1 INTDECLARE @y2 INTDECLARE @y3 INTDECLARE @y4 INTDECLARE @y5 INTDECLARE @variable VARCHAR(30)SET @x = 1SET @y1 = 10SET @y2 = 20SET @y3 = 30SET @y4 = 5SET @y5 = 45WHILE @x<6BEGINif @y(@x) IS NOT NULLprint @y(@x)SET @x = @x + 1ENDi would wan to have the result in102030545 |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2009-06-02 : 23:16:41
|
| What's @yyyy?This is OK, but I don't know what @yyyy is in your code.DECLARE @xxxx INTSET @xxxx = 0WHILE @xxxx<13BEGIN PRINT @xxxx SET @xxxx = @xxxx + 1END |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2009-06-02 : 23:18:32
|
quote: Originally posted by waterduck maybe i was not clear...heres my example code with errorDECLARE @x INTDECLARE @y1 INTDECLARE @y2 INTDECLARE @y3 INTDECLARE @y4 INTDECLARE @y5 INTDECLARE @variable VARCHAR(30)SET @x = 1SET @y1 = 10SET @y2 = 20SET @y3 = 30SET @y4 = 5SET @y5 = 45WHILE @x<6BEGINif @y(@x) IS NOT NULLprint @y(@x)SET @x = @x + 1ENDi would wan to have the result in102030545
I still don't know what you mean with @y(@x) - that's not valid in VB either |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-06-02 : 23:19:35
|
| erm....is like a parameter in a parameter@y(@x) will likely equal to @y1, @y1, @3 depending @x is wat digit |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2009-06-02 : 23:20:54
|
| Actually I guess you really mean that @y should be an array?If so, you'd need to use a temporary table or table variable but you need to explain what you actually want to do in the loop because in SQL code you typically don't need to loop, you rather run a query over the rows in the table. |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-06-02 : 23:32:01
|
| However i was doing FIFO(first in first out concept)where i have my variable insert with value@1=100@2=100@3=50@4=51@5=0@6=0@7=0@8=0@9=0@10=0@11=0@12=0so i need a loop to achieve the result of@1=0@2=99@3=0@4=0@5=0@6=0@7=0@8=0@9=0@10=0@11=0@12=0 |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2009-06-02 : 23:37:04
|
| OK, my mind reading is failing me, I cannot begin to guess how you get from@1=100@2=100@3=50@4=51@5=0@6=0@7=0@8=0@9=0@10=0@11=0@12=0to @1=0@2=99@3=0@4=0@5=0@6=0@7=0@8=0@9=0@10=0@11=0@12=0 |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-06-02 : 23:41:35
|
| woops i write mistake!!!However i was doing FIFO(first in first out concept)where i have my variable insert with value@1=100@2=100@3=-50@4=-51@5=0@6=0@7=0@8=0@9=0@10=0@11=0@12=0so i need a loop to achieve the result of@1=0@2=99@3=0@4=0@5=0@6=0@7=0@8=0@9=0@10=0@11=0@12=0really really sorry...my mistake..i forgot to put minus...sorry sory T.T |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2009-06-02 : 23:45:33
|
| Still can't guess what you're trying to do |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-02 : 23:53:58
|
if you have a table storing these values, justselect sum(value)from yourtable will give you 99What is the things that you need to do that can't handle by table & set-based query ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-06-02 : 23:55:10
|
| hmmi think i have found another logic to do it...thx alot for accompany me^^ |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-02 : 23:58:34
|
Good L[spoiler]D[/spoiler]uck  KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-06-03 : 01:46:34
|
quote: Originally posted by waterduck hmmi think i have found another logic to do it...thx alot for accompany me^^
What is that logic?MadhivananFailing to plan is Planning to fail |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-06-03 : 06:16:07
|
| Erm...guys i think i need help...i have the concept in my mind, but i wasn't able to code it outTABLEA------TYPE DATE(DD/MM/YY) QUANTITYPDO 01/01/06 100PDO 02/01/06 100DO 01/01/07 50DO 01/02/07 100PDO 01/03/07 25PDO 01/08/08 30my concept goes like:loop cursor tableA---if @type = PDO------if @bal >=@quantity---------@bal = @bal - @quantity------else--------insert into #temp_table(type, date, quantity)---if @type = DO------set @bal = @bal + quantity------loop #temp_table---------if #temp_table.quantity>@bal------------#temp_table.quantity=#temp_table.quantity-@bal------------set @bal = 0---------else------------set @bal = @bal - #temp_table.quantity------------#temp_table.quantity=0------end loopend loopif @bal <> 0---print 'balance is negatif'else---exec(select sum(quantity) from #temp_table)sorry if i not able to describe detail...my english not so well |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-06-03 : 21:08:23
|
| any1 can guide me? |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-06-04 : 00:15:38
|
| SOLVED ^^ |
 |
|
|
|