现有SQL语句中所需的修改

时间:2015-12-08 07:48:33

标签: sql-server if-statement

我有以下SQL查询,它根据单个员工的ID显示特定公司所有员工的列表。

select TU.ApmId as 'ApmId', TU.UserId as 'PayRoll Id', 
       TU.UserName as 'Employee Name', P.ParLongDesc as 'Company Name'
from smart2uat.dbo.employee_mast TU, smart2uat.dbo.company_mast P 
where TU.ParID = P.PARID and TU.ParID = (select EM.ParId 
                                         from smart2uat.dbo.employee_mast EM   
                                         where EM.apmid='CBA134')

其中CBA134是员工ID,ParId是公司ID。 我想在代码中进行一些小修改: 如果给定员工(employeeID)的ParID29332,则必须显示所有3家公司的合并员工详细信息,否则必须显示该员工单独一家公司的详细信息。请帮助代码。

示例数据:

VDS007 VDS007 VERNE D'SOUZA    MS india Pvt. ltd - MUMBAI 
AJG004 AJG124 GAYATRI NIJASURE MS india Pvt. ltd - MUMBAI 
etc... etc....

(CBA134 Parid = 29所属公司MS india Pvt.ltd - MUMBAI)

预期产出:

VDS007 VDS007 VERNE D'SOUZA    MS india Pvt. ltd - MUMBAI 
AJG004 AJG124 GAYATRI NIJASURE MS india Pvt. ltd - MUMBAI 
KJG004 VJG124 ASHWIN NIJASURE  SAF india Pvt. ltd - MUMBAI 
AUG004 BJG124 ARUN GOPAL       GKR india Pvt. ltd - MUMBAI
etc....etc....

(CBA134 par id为29,因此我想要公司Par ID的员工详细信息  3,32,29))PARID 29 - MS india Pvt。有限公司 - MUMBAI,PARID 32 - SAF india Pvt。有限公司 - MUMBAI,PARID 3 - GKR india Pvt。有限公司 - 孟买

1 个答案:

答案 0 :(得分:0)

我自己做过......谢谢你们......

declare @partemp int
select @partemp = ParId from smart2uat.dbo.employee_mast where apmid='CBA134'

if @partemp in (3,29,29)

begin
select TU.ApmId as 'ApmId', TU.UserId as 'PayRoll Id',TU.UserName as 'Employee Name',
P.ParLongDesc as 'Company Name'
from smart2uat.dbo.employee_mast TU, smart2uat.dbo.company_mast P where TU.ParID=P.PARID
and TU.ParID in (3,32,29) 
end

else 

begin
select TU.ApmId as 'ApmId', TU.UserId as 'PayRoll Id',TU.UserName as 'Employee Name',
P.ParLongDesc as 'Company Name'
from smart2uat.dbo.employee_mast TU, smart2uat.dbo.company_mast P where TU.ParID=P.PARID
and TU.ParID = @partemp
end