如何在SQL中编写存储过程并在其中使用if else语句 [英] How to write a stored procedure in SQL and use if else statements in it
本文介绍了如何在SQL中编写存储过程并在其中使用if else语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的员工表包含EmpId,EmpName,DeptID,Salary,EditedOn,EditedBy和Address字段。它有以下条目:
I have an employee table with fields EmpId , EmpName , DeptID , Salary , EditedOn, EditedBy and Address .It has following entries:
EmpId EmpName DeptID Salary EditedBy EditedOn Address
1 Sonia 2 25000 1 2015-02-10 NULL
2 Rahul 1 15000 1 2015-01-10 NULL
3 Pooja 3 12000 1 2015-04-05 NULL
5 Tanya 7 32000 1 2015-04-10 NULL
6 Raj 8 27000 1 2015-04-03 NULL
16 NULL 1 NULL NULL 2015-04-15 NULL
17 NULL 1 NULL NULL 2015-04-15 NULL
18 NULL 1 NULL NULL 2015-04-15 NULL
19 Roshni NULL NULL NULL NULL Vasco
27 gh NULL NULL NULL NULL kk
28 Priyanka 7000 NULL NULL Porvorim
29 Ramesh NULL NULL NULL NULL Vasco
我想创建一个strored过程proc_GetEmployeeOnSalaryrange并指定foll条件:
I want to create a strored procedure proc_GetEmployeeOnSalaryrange and specify foll conditions :
if salary is between ( 0-10000)
Select EmpName, Salary , 'Range A'
Else if salary is between ( 10001-20000)
Select EmpName, Salary , 'Range B' and third condition is :
if salary is above 20000
Select EmpName, Salary , 'Range C'
和范围A,范围B和范围C应该是硬编码的。我不知道要使用的if else语法。所以请帮忙。
and Range A , Range B and Range C should be hard coded . I dont know the if else syntax to be used. so please help .
推荐答案
试试
Sql Server - 如何在SQL Server中编写存储过程 [ ^ ]
http://www.mssqltips.com/sqlservertutorial/161/creating-a -simple-stored-procedure / [ ^ ]
如果
声明 - IF...ELSE(Transact-SQL) [ ^ ]
案例
- CASE(Transact-SQL) [ ^ ]
简单使用SQL CASE表达式 [ ^ ]
Try
Sql Server - How To Write a Stored Procedure in SQL Server[^]
http://www.mssqltips.com/sqlservertutorial/161/creating-a-simple-stored-procedure/[^]
If
statement - IF...ELSE (Transact-SQL)[^]
Case
- CASE (Transact-SQL)[^]
A Simple Use of SQL CASE Expression[^]
试试这个:
Try this:
Select EmpName, CASE WHEN Salary > 0 AND Salary <=10000 THEN ' A'
WHEN Salary > 10000 AND Salary <20000 THEN 'B'
WHEN Salary > 20000 THEN 'C'
END AS RangeOfSalary
请参阅: CASE .. 。(SQL) [ ^ ]
你好,你需要看* case * statement
Hi there, you need to look at *case* statement
这篇关于如何在SQL中编写存储过程并在其中使用if else语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文