Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Price is inversly proportional to ease of use
Message
De
06/02/2003 17:20:49
 
 
À
06/02/2003 16:14:46
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Stored procedures, Triggers, UDFs
Divers
Thread ID:
00750165
Message ID:
00750282
Vues:
20
Agreed.

>Don't forget rollup is a key word in SQL Server even though not listed as a reserved work I would avoid using it for an object/var name.
>
>BOb
>
>
>>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
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform