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 2000 Forums
 SQL Server Development (2000)
 Update query

Author  Topic 

sqldba2k6
Posting Yak Master

176 Posts

Posted - 2006-12-26 : 14:38:57
declare @customer TABLE (
ID int NOT NULL ,
VS int NOT NULL
)

declare @Customerview TABLE (
ID int NOT NULL ,
Tname nchar (10) NOT NULL ,
FN nchar (3) NOT NULL,
PV int NOT NULL,
status nchar (2)
)


insert @customer
select 1, 2 union all
select 2, 1


insert @customerview
select 1, 'PERSONAL','VT',3,'Y' union all
select 2, 'PERSONAL','VT',4,'Z'

Need to update the PV in customerview table with if the value of column VS is not equal with value of teh customer view PV and the status with Y .
I want the below results when i query the
Select * from @customerview

select 1, 'PERSONAL','VT',2,'Y' union all
select 2, 'PERSONAL','VT',4,'Z'


snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2006-12-26 : 14:51:07
This should do it
create table #customer (
ID int NOT NULL ,
VS int NOT NULL
)

create table #Customerview (
ID int NOT NULL ,
Tname nchar (10) NOT NULL ,
FN nchar (3) NOT NULL,
PV int NOT NULL,
status nchar (2)
)


insert #Customer
select 1, 2 union all
select 2, 1


insert #Customerview
select 1, 'PERSONAL','VT',3,'Y' union all
select 2, 'PERSONAL','VT',4,'Z'

update #Customerview
set PV = #Customer.VS
from #Customerview
inner join #Customer on #Customerview.ID = #Customer.ID
where #Customerview.PV <> #Customer.VS and #Customerview.status = 'Y'

Select * from #Customerview
Go to Top of Page

sqldba2k6
Posting Yak Master

176 Posts

Posted - 2006-12-26 : 16:16:40
Thanks a lot!.
Go to Top of Page
   

- Advertisement -