如何根据4个不同的值设置标志变量 [英] how to set flag variable based on 4 different values

查看:53
本文介绍了如何根据4个不同的值设置标志变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我必须根据sql中的4个不同值设置一个flage。



这是我的代码



Hi all

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

here is my code

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







现在,如果任何计数是> 0然后@flag应为1.否则@flag应该为0.如果所有计数都为0或null,则短标志应为0。否则它应该是1.




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.

推荐答案

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


使用以下查询。我们可以直接使用所有计数的总和,因为计数将为0或大于0.



DECLARE @Flag int,

@ Count1 int,

@ Count2 int,

@ Count3 int,

@ Count4 int,

@ShippingLineID int



从tbl_CMS_Container中选择@ Count1 = COUNT(ContainerID),其中ShippingLine = @ShippingLineID

从tbl_CMS_Empty_Container中选择@ Count2 = COUNT(EmptyContainerID)其中ShippingLine = @ShippingLineID

从tbl_CMS_Export选择@ Count3 = COUNT(ExportContainerID),其中ShippingLine = @ShippingLineID

从tbl_CMS_GatePass选择@ Count4 = COUNT(GatePassId),其中ShippingLine = @ ShippingLineID



SET @Flag = @ Count1 + @ Count2 + @ Count3 + @ Count4
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


哪个计数?count1,count2,共3个记录,count4 ???或者全部

你可以做到!
which count?count1,count2,count3,count4??? or all
U can do it!


这篇关于如何根据4个不同的值设置标志变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆