sql server - Setting a SQL local variable properly -
i'm wondering if there way have local variable use 'from' statement select statement not declared in 'set' of variable. have following example:
declare @idno int; declare @orderquantity int; set @idno='1'; --test value set @orderquantity=isnull(sum(table1.quantity), 0); select table1.idno 'id no', @orderquantity 'order quantity' table1 idno = @idno group idno, quantity
i have lots of columns add columns together, , columns added derived statements in @orderquantity variable.
so instead of having lengthy column definitions in select statement, i'd place things in variables make select statement simpler (e.g. select @orderquantity - @orderscancelled 'net sold')
the best way approach appreciated!
(using sql server)
if you're asking if can this:
declare @orderquantity int; declare @orderscancelled int; set @orderquantity=isnull(sum(table1.quantity),0); set @orderscancelled=isnull(sum(table1.cancelled),0); select @orderquantity - @orderscancelled
the answer yes
Comments
Post a Comment