如何在Sql中编写Switch语句以避免其他语句 [英] How Can I Write Switch Statement In Sql To Avoid Of If Else Statements

查看:680
本文介绍了如何在Sql中编写Switch语句以避免其他语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





根据我的要求,我需要获取程序名称。

所以我希望使用Switch声明而不是使用多个if语句。

(我不想准备一个带有paramid,proc名称的表。)

请让我知道SQL支持switch语句如果是的话是什么语法。



谢谢。

Hi,

In my requirements based on the param i need to get the Procedure name.
So i wish to use Switch statement rather than using of multiple if statements.
(I don't want to prepare a table with paramid,proc name.)
Please let me know SQL supports switch statement if yes what is the syntax.

Thank you.

推荐答案





MSSQL相当于switch是它的Case语句。



它的用法在以下文档中描述:CASE(Transact-SQL) [ ^ ]



...希望它有所帮助。
Hi,

MSSQL's equivalent of switch is it's Case statement.

It's usage is described in the documentation for: CASE (Transact-SQL)[^]

... hope it helps.


您可以根据需要在sql server中使用CASE。

例如:

You can use CASE in sql server as per your need.
for example :
DECLARE @TestVal int
SET @TestVal = 3
SELECT
CASE @TestVal
WHEN 1 THEN 'First'
WHEN 2 THEN 'Second'
WHEN 3 THEN 'Third'
ELSE 'Other'
END



请转到学习链接更多关于案例 SQL Server中的CASE语句 [ ^ ]


这篇关于如何在Sql中编写Switch语句以避免其他语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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