Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Price is inversly proportional to ease of use
Message
From
06/02/2003 15:58:26
 
 
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Miscellaneous
Thread ID:
00750165
Message ID:
00750180
Views:
19
It doesn't work because Rollup is an alias for a column and the results were returned to the client. To get the behavior using your example, you'd probably do something like this:
-- Roll up the values
DECLARE @rollup int
select
    @rollup = sum(MyField)
from foo
    where left(IKey,1) = '1'

 -- insert the rolled up values
 update foo
     set
        MyField = @RollUp
where IKey = '100'
Now, a better solution would be to stay in the set based operations. So, you could also write:
UPDATE foo
SET myField = (SELECT SUM(myField) FROM foo WHERE ikey LIKE '1%' AND ikey != '100')
WHERE iKey='100'
-Mike

>Why does this not Work? I am certain I have oversimplified the mighty T-SQL language. I have some records that get a rollup value based on the records type. Much like an invoice total, except I have to store the rollup for whatever reason. There are about 13 fields to rollup so I have reduced it to one for readability. An overly simplified recordset might look like this;
>
record #1 IKEY = 101 | MyField = 10 | rTYPE = Item
>record #2 IKEY = 102 | MyField = 20 | rTYPE = Item
>record #3 IKEY = 103 | MyField = 30 | rTYPE = Item
>record #4 IKEY = 100 | MyField = 00 | rTYPE = Rollup
>record #5 IKEY = 201 | MyField = 18 | rTYPE = Item
>record #6 IKEY = 202 | MyField = 24 | rTYPE = Item
>
>Where the total that should be written to MyField for record IKEY 100 should be 60. I have pulled out all but the meat of the query here:
>
>-- Roll up the values
>select
>    sum(MyField) as RollUp
>from foo
>    where left(IKey,1) = '1'
>
> -- insert the rolled up values
> update foo
>     set
>        MyField = RollUp
>where IKey = '100'
>
>
>My humble thanks in advance,
Michael Levy
MCSD, MCDBA
ma_levy@hotmail.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform