Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 need help in Update statement

Author  Topic 

nvakeel
Yak Posting Veteran

52 Posts

Posted - 2008-03-17 : 16:38:28
Here I get a problem in updating a table.
CREATE TABLE #TMP1
(NUM INT, VAL VARCHAR(5))
INSERT INTO #TMP1
SELECT 1,2
UNION
SELECT 2,3
UNION
SELECT 3,NULL
UNION
SELECT 4,5

UPDATE #TMP1
SET VAL = CASE WHEN NUM = 3
THEN (SELECT SUM(CAST(VAL AS INT) ) FROM #TMP1)
END

SELECT * FROM #TMP1
(4 row(s) affected)
Warning: Null value is eliminated by an aggregate or other SET operation.

(4 row(s) affected)
NUM VAL
----------- -----
1 NULL
2 NULL
3 10
4 NULL

(4 row(s) affected)


Here I lost the values in "VAL" while doing SUM operatin which i should not.
can anyone help me

tm
Posting Yak Master

160 Posts

Posted - 2008-03-17 : 16:51:24
Update statement should be:

UPDATE #TMP1
SET VAL = CASE WHEN NUM = 3
THEN (SELECT SUM(CAST(VAL AS INT) ) FROM #TMP1)
ELSE VAL -- << Add ELSE VAL
END
Go to Top of Page

nvakeel
Yak Posting Veteran

52 Posts

Posted - 2008-03-18 : 08:13:04
thnkss
Go to Top of Page
   

- Advertisement -