我可以在变量声明中使用条件语句吗? [英] Can I Use Conditional Statement In Variable Declaration?

查看:83
本文介绍了我可以在变量声明中使用条件语句吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have Created a Portal in which when user enter some ID.then many columns associated for that ID shows up. Now i want that if user enter some specific IDs then values of some columns(NASSETS,Depreciation key)associated with That IDs should be fixed.

I want Nasset to be blank for some Asset Classes.I want depriciation key as 0000 for some Asset Classes.

Can I use "If Statement" in declarion of variable.

I have this stored procedure for that work.










SELECT FA.Asset_Class AS 'Asset Class',
           '1000' AS 'Company Code',
           '1' AS 'NASSETS',
           U.Asset_Desc AS 'Name',
           U.Asset_Desc+' ('+ U.Material_Code +')' AS 'Asset main no. text',
           UOM.Base_UOM AS 'Base Unit of Measure',
           'X' AS 'XHIST',
           'X' AS 'Include asset',
           '' AS 'Asset capitalization date',
           P.Plant_Name AS 'Business Area',
           C.CostCenter AS 'Cost Center',
           P.Plant_Name AS 'Plant',
           FA.IO_No AS 'Order',

           '01' AS 'Real depreciation area',
           'R999' AS 'Depreciation key',
           '' AS 'Planned useful life in years',
           '' AS 'Planned useful life in periods',
           '20' AS 'Real depreciation area',
           'R999' AS 'Depreciation key',
           '' AS 'Planned useful life in years',
           '' AS 'Planned useful life in periods',

           '4' AS 'AFASL_01',
           '4' AS 'AFASL_02'

推荐答案

目前还不完全清楚你是什么但是我很确定你想要 CASE(Transact-SQL) [ ^ ]声明。



例如,使用由
It's not entirely clear what you mean but I'm fairly sure you want the CASE (Transact-SQL)[^] statement.

For example, using some test data generated by
-- Some test data
Create Table Table1
(
	id int identity(1,1),
	someTextData AS 'Data' + Cast(id as varchar),
	someDateData AS DATEADD(dd, id, CAST('2000-01-01' as Date))
)
go
insert into Table1 default values
go 100

您可以调整查询结果

select 
	id, someTextData, someDateData, 

	CASE WHEN MONTH(someDateData) = 1 THEN 'January Sales' 
             ELSE someTextData 
        END as contrived

from Table1



另一方面,如果你的意思是你声明了一个变量并且想要调整它的值,你仍然可以使用CASE语句


On the other hand, if you mean that you have a variable declared and want to adjust it's value, you can still use the CASE statement

declare @AVariable INT = (select count(*) from Table1)
declare @AnotherVariable INT = CASE WHEN @AVariable > 50 then 1 else 0 end
select @AnotherVariable

如果你打算做一些更复杂的事情 - 例如多个语句将取决于条件然后使用 IF ... ELSE(Transact-SQL) [ ^ ]声明

例如

If you are planning to do something a little more complex - e.g. multiple statements would depend on the condition then use the IF...ELSE (Transact-SQL)[^] statement
e.g.

IF @Avariable > 100 
BEGIN
	SET @AnotherVariable = 200
        --  ... do some other stuff
END
ELSE
BEGIN
	SET @AnotherVariable = 100
        --  ... do some other stuff
END 
select @AnotherVariable


这篇关于我可以在变量声明中使用条件语句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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