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)
 need help on if statement

Author  Topic 

Idyana
Yak Posting Veteran

96 Posts

Posted - 2012-02-19 : 00:30:56
my tables and data as following,
declare @tDirMovie table
(cd varchar(50), descrp varchar(200), isCompulsory bit)
/*cd is a unique*/

insert into @tDirMovie values('01','mission impossible', 'true')
insert into @tDirMovie values('02','thor', 'false')
insert into @tDirMovie values('03','x-men', 'true')
insert into @tDirMovie values('04','batman', 'true')
insert into @tDirMovie values('05','hulk', 'false')


declare @tMyMovie table
(myID varchar(10), tDirMovie_Cd varchar(50))
/*@tMyMovie(tDirMovie_Cd) is a foreign key to @tDirMovie(cd)*/

--if myID='1925'
insert into @tMyMovie values('1925','01')
insert into @tMyMovie values('1925','02')

--if myID='4474'
insert into @tMyMovie values('4474','01')
insert into @tMyMovie values('4474','03')
insert into @tMyMovie values('4474','04')


-Requirement-
I want to check if all movie with IsCompulsory='true' is SELECTED or not

1st rule
Based on the data (myID='1925'). The result is all movie with IsCompulsory='true' is NOT SELECTED

2nd rule
Based on the data (myID='4474'). The result is all movie with IsCompulsory='true' is SELECTED

How my if statement look's like?

if (.....)
Begin
print 'The result is all movie with IsCompulsory='true' is SELECTED'
End
else
Begin
print 'The result is all movie with IsCompulsory='true' is NOT SELECTED'
End


Need help to complete my if statement

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-20 : 12:27:03
so you just want to print those messages like this?


if exists(select 1
from @tDirMovie d
inner join @tMyMovie m
on m.tDirMovie_Cd = d.Cd
where m.myID = @youridvalue
group by myID
having sum(case when isCompulsory ='false' then 1 else 0 end) >0
Begin
print 'The result is all movie with IsCompulsory='true' is NOT SELECTED'
End
else
Begin
print 'The result is all movie with IsCompulsory='true' is SELECTED'
End





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

Go to Top of Page

Idyana
Yak Posting Veteran

96 Posts

Posted - 2012-02-21 : 00:10:58
tq sir
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-21 : 14:21:39
wc

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

Go to Top of Page
   

- Advertisement -