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 2008 Forums
 Transact-SQL (2008)
 using update or nested

Author  Topic 

sachingovekar
Posting Yak Master

101 Posts

Posted - 2012-11-17 : 07:02:57
i have the following table

create table #data1
(
assetcenter int,
hped int,
model varchar(20),
result int
)

-- INSERT DATA
insert into #data1 (assetcenter, hped,MODEL,result)
select 0,1,'MODEL DL3',null union all
select 4,3 ,'MODEL R7',null union all
select 0,null,NULL,null


I can get the result column using the below statement:

SELECT t1.assetcenter,t1.hped,t1.MODEL
,CASE WHEN t1.model like '%DL3%' or t1.model like '%R7%' THEN 2 ELSE t1.result END
FROM
(
select assetcenter, hped,MODEL,
case
when assetcenter = 0 and hped > 0 then hped
when assetcenter > hped then hped
when assetcenter = 0 and hped is null then 1
end as result
from #data1
) t1


However i want to update the result column USING UPDATE STATEMENT - How can i achieve that?

OUTPUT REQUIRED:
-------------------------


0 1 MODEL DL3 1
4 3 MODEL R7 2
0 NULL NULL 1

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-11-17 : 09:46:15
Your result does not seem to agree with the query - you have "t1.model like '%DL3%' or t1.model like '%R7%' THEN 2 " in the outer query, which would mean that for the first row, result would be 2, yet in your sample result, it is given as 1.

Ignoring that for the moment, unless there is some other reason to use subqueries, you can do the updates in a single query like shown below:
UPDATE #data1 SET
result =
CASE
WHEN model LIKE '%DL3%' OR model LIKE '%R7%' THEN 2
WHEN assetcenter = 0 AND hped > 0 THEN hped
WHEN assetcenter > hped THEN hped
WHEN assetcenter = 0 AND hped IS NULL THEN 1
END;
Go to Top of Page

sachingovekar
Posting Yak Master

101 Posts

Posted - 2012-11-18 : 09:27:32
consider the below table and data.

the output of the select statement below should be achieved using only one update statement.

I WANT TO UPDATE THE "RESULT" column of #data1 table and the output should be the same as selct statement.

create table #data1
(
assetcenter int,
hped int,
model varchar(20),
result int
)

-- INSERT DATA
insert into #data1 (assetcenter, hped,MODEL,result)
select 0,1,'MODEL DL3',null union all
select 4,3 ,'MODEL R7',null union all
select 0,null,NULL,null union all
select 5,3,null,null union all
select 0,null,'model r7',null

-- SELECT
SELECT t1.assetcenter,t1.hped,t1.MODEL,
CASE WHEN t1.model like '%DL3%' or t1.model like '%R7%' THEN 2
WHEN t1.result = 3 then 4
ELSE t1.result END
FROM
(
select assetcenter, hped,MODEL,
case
when assetcenter = 0 and hped > 0 then hped
when assetcenter > hped then hped
when assetcenter = 0 and hped is null then 1
end as result
from #data1
) t1
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-11-19 : 01:55:28
just turn it into an update


UPDATE t1
SET t1.RESULT=
CASE WHEN t1.model like '%DL3%' or t1.model like '%R7%' THEN 2
WHEN t1.result1 = 3 then 4
ELSE t1.result1 END
FROM
(
select assetcenter, hped,MODEL,
case
when assetcenter = 0 and hped > 0 then hped
when assetcenter > hped then hped
when assetcenter = 0 and hped is null then 1
end as result1,RESULT
from #data1
) t1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sachingovekar
Posting Yak Master

101 Posts

Posted - 2012-11-19 : 03:11:55
Thank you Visakh!!...It works fine :)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-11-19 : 03:25:38
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -