Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

i have to set one flage based on 4 different values in sql.

here is my code

SQL
DECLARE @Flag int ,
		@Count1 int 	,
		@Count2 int 	,
		@Count3 int 	,
		@Count4 int ,
                @ShippingLineID	int
	select @Count1 =   COUNT(ContainerID) from tbl_CMS_Container where ShippingLine = @ShippingLineID
	
	select @Count2 =   COUNT(EmptyContainerID) from tbl_CMS_Empty_Container where ShippingLine = @ShippingLineID
	select @Count3 =   COUNT(ExportContainerID) from tbl_CMS_Export where ShippingLine = @ShippingLineID
	
	select @Count4 =   COUNT(GatePassId) from tbl_CMS_GatePass where ShippingLine = @ShippingLineID



now if any of the count is > 0 then @flag should be 1. else @flag should be 0. in short flag should be 0 if all the counts are 0 or null. otherwise it should be 1.
Posted

SQL
DECLARE @Flag int ,
		@Count1 int 	,
		@Count2 int 	,
		@Count3 int 	,
		@Count4 int ,
                @ShippingLineID	int
	select @Count1 =   COUNT(ContainerID) from tbl_CMS_Container where ShippingLine = @ShippingLineID
	
	select @Count2 =   COUNT(EmptyContainerID) from tbl_CMS_Empty_Container where ShippingLine = @ShippingLineID
	select @Count3 =   COUNT(ExportContainerID) from tbl_CMS_Export where ShippingLine = @ShippingLineID
	
	select @Count4 =   COUNT(GatePassId) from tbl_CMS_GatePass where ShippingLine = @ShippingLineID

if @Count1 = 0 and @Count2 = 0 and @Count3 = 0 and @Count4 = 0 
	BEGIN
	SET @Flag = 0 
	END
	else 
	begin
	SET @Flag = 1
	end
	
	SELECT @Flag
 
Share this answer
 
Use the below query. We can directly use the sum of all the counts becuase count will be either 0 or greater than 0.

DECLARE @Flag int ,
@Count1 int ,
@Count2 int ,
@Count3 int ,
@Count4 int ,
@ShippingLineID int

select @Count1 = COUNT(ContainerID) from tbl_CMS_Container where ShippingLine = @ShippingLineID
select @Count2 = COUNT(EmptyContainerID) from tbl_CMS_Empty_Container where ShippingLine = @ShippingLineID
select @Count3 = COUNT(ExportContainerID) from tbl_CMS_Export where ShippingLine = @ShippingLineID
select @Count4 = COUNT(GatePassId) from tbl_CMS_GatePass where ShippingLine = @ShippingLineID

SET @Flag = @Count1 + @Count2 + @Count3 + @Count4
 
Share this answer
 
Comments
ravikhoda 18-Feb-14 22:22pm    
thanks but i solved it by using above method. please check my solution.
which count?count1,count2,count3,count4??? or all
U can do it!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900