I am going to perform an UPDATE
on the result of a query in SQL Server 2008 but it seems UPDATE
needs a name for table it is not possible.
I am going to do this
UPDATE (SELECT ~)
set x = "1"
where x IS NULL
BUT really I have problem in "VIEW" to execute this code in SQL Server 2008.
My table:
CREATE TABLE [dbo].[Table_1]
(
[id] [int] NOT NULL,
[rep] [nchar](10) NOT NULL,
[qty] [int] NOT NULL,
[sell] [int] NOT NULL,
[settled] [bit] NULL
) ON [PRIMARY]
My code for update and the select inside is :
update (
SELECT rep, Expr1, Expr2, Expr3, Expr4, Expr5
FROM (SELECT o.rep, o.Expr1, o.Expr2, p.rep AS Expr3, p.Expr1 AS Expr4, p.Expr2 AS Expr5
FROM (SELECT oo.rep, SUM(oo.qty) AS Expr1, SUM(oo.sell) AS Expr2
FROM dbo.Table_1 AS oo
WHERE (oo.settled = 1)
GROUP BY oo.rep) AS o FULL OUTER JOIN
(SELECT pp.rep, SUM(pp.qty) AS Expr1, SUM(pp.sell) AS Expr2
FROM dbo.Table_1 AS pp
WHERE (pp.settled = 0)
GROUP BY pp.rep) AS p ON o.rep = p.rep) AS derivedtbl_1
)
set rep = Expr4
where rep IS NULL
Is there any body to help me to find it how is it possible to perform an update on the result of a query?
I have read many script for other DBs but SQL Server 2008 is different as I think.
The main aim to do this code is to fill null fields in "rep" which are created after FULL OUTER JOIN
. I will appreciate if is there any other easier way to fill the fields instead of update. This is my idea.
Thanks for your time .
Sample date :
Table :
rep qty sell settled
------------------------------
a 10 20 true
b 20 40 true
c 10 20 False
a 11 22 False
The result is like this :
rep Expr1 Expr2 Expr3 Expr4 Expr5
-----------------------------------------------------
a 10 20 a 11 22
b 20 40 null null null
null null null c 10 20
(I am trying to fill the rep for Expr3 with name of operator instead of null)
Kinbds