SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 need help in update statement using if
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Idyana
Yak Posting Veteran

82 Posts

Posted - 04/05/2012 :  08:10:07  Show Profile  Reply with Quote
My tables and data as following,


declare @tApplicant table (myID varchar(200), myCategory varchar(10) null);
insert into @tApplicant(myID) values('1925');
insert into @tApplicant(myID) values('4474');
insert into @tApplicant(myID) values('8398');
insert into @tApplicant(myID) values('4656');
insert into @tApplicant(myID) values('2288');
insert into @tApplicant(myID) values('4876');
/* myID is unique */



declare @tMySPM table (myID varchar(200), subjectCd varchar(10));
insert into @tMySPM values('1925','99');
insert into @tMySPM values('1925','19');
insert into @tMySPM values('2288','78');
insert into @tMySPM values('2288','99');
insert into @tMySPM values('4656','72');
/* Combination of myID and subjectCd is unique */


declare @tMySTPM table (myID varchar(200), subjectCd varchar(10));
insert into @tMySTPM values('4474','99');
insert into @tMySTPM values('4474','19');
insert into @tMySTPM values('2288','78');
insert into @tMySTPM values('2288','99');
insert into @tMySTPM values('8398','72');
/* Combination of myID and subjectCd is unique */


What I would do
1. update @tApplicant set myCategory='A',
if myID exists in @tMySPM and @tApplicant(myCategory) is null
2. update @tApplicant set myCategory='B',
if myID exists in @tMySTPM and @tApplicant(myCategory) is null
3. update @tApplicant set myCategory=null,
if myID not exists in @tMySPM / @tSTPM and @tApplicant(myCategory) is null

My expected result,
myID  | myCategory
-----------------------------
1925	A
4474	B
8398	B
4656	A
2288	A
4876	NULL


Really need help. I'm stuck

So far, my T-SQL as following,
update @tApplicant 
set myCategory = 
(if exists(t2.myID) 
Begin
'A'
End
else if exists(t3.myID) 
Begin
'B'
End
else NULL 
end)

from @tApplicant t1
inner join @tMySPM t2 on t1.myID=t2.myID
inner join @tMySTPM t3 on t1.myID=t3.myID

parody
Posting Yak Master

111 Posts

Posted - 04/05/2012 :  10:06:21  Show Profile  Reply with Quote
A number of ways. Heres one.

UPDATE tA
SET tA.myCategory = 
	CASE 
		WHEN tM.myID IS NOT NULL THEN 'A'
		WHEN tMT.myID IS NOT NULL THEN 'B'
		ELSE NULL
	END
FROM @tApplicant tA
	LEFT JOIN @tMySPM tM
		ON tA.MyID = tM.MyID
	LEFT JOIN @tMySTPM tMT
		ON tA.MyID = tMT.MyID
Go to Top of Page

Idyana
Yak Posting Veteran

82 Posts

Posted - 04/05/2012 :  11:21:51  Show Profile  Reply with Quote
tq sir
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47157 Posts

Posted - 04/05/2012 :  12:01:39  Show Profile  Reply with Quote

update t
set myCategory = t1.Cat
from @tApplicant t
cross apply (select MAX('A') AS Cat
             from @tMySPM 
             where MyID = t.MyID
             UNION ALL
             select MAX('B')
             from @tMySTPM tMT
	     where MyID = t.MyID
            )t1


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

Go to Top of Page

Idyana
Yak Posting Veteran

82 Posts

Posted - 04/07/2012 :  02:40:05  Show Profile  Reply with Quote
tq mr visakh
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47157 Posts

Posted - 04/08/2012 :  18:13:00  Show Profile  Reply with Quote
welcome

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

Go to Top of Page

vinu.vijayan
Posting Yak Master

India
227 Posts

Posted - 04/09/2012 :  05:13:17  Show Profile  Reply with Quote
As Simple as this


Update @tApplicant
Set myCategory = Case
		   When myCategory IS NULL AND Exists(Select myID From @tMySPM Where myID = @tApplicant.myID)
		   Then 'A'
		   When myCategory IS NULL AND Exists(Select myID From @tMySTPM Where myID = @tApplicant.myID)
		   Then 'B'
		   Else NULL
		 End


Vinu Vijayan
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000