我有以下SQL,它将为我提供职位描述和部门输出:
declare @JOB_DESC nvarchar (50)
declare @DEPARTMENT nvarchar (50)
declare @Initials nvarchar (50)
select case
@Initials = 'XBF' then (@JOB_DESC = 'Partner', @DEPARTMENT = '_Banking and Finance'),
@Initials = 'XMA' then (@JOB_DESC = 'Partner', @DEPARTMENT = '_Disputes'),
else
SELECT @JOB_DESC = u.NAME, @DEPARTMENT = u.PracticeAreaName
FROM USERS u where u.USERID = @Initials
select @JOB_DESC
select @DEPARTMENT
所以我基本上想要强制作业描述和部门,如果首字母是XBF或XMA,但否则我想从普通用户表中获取我的输出,但这对我不起作用,我很难找到正确的SQL在线。管理工作室在案例陈述中引用第一个'='时说“'''''''''''''''''''''''........
答案 0 :(得分:0)
只需使用IF...ELSE
执行此操作 - CASE
语句不会更容易。
IF @Initials = 'XBF'
BEGIN
SELECT 'Partner' AS JOB_DESC, '_Banking and Finance' AS DEPARTMENT
END
ELSE IF @Initials = 'XMA'
BEGIN
SELECT 'Partner' AS JOB_DESC, '_Disputes' AS DEPARTMENT
END
ELSE
BEGIN
SELECT u.NAME AS JOB_DESC, u.PracticeAreaName AS DEPARTMENT
FROM USERS u where u.USERID = @Initials
END
如果您希望它们作为变量
,则使用类似的代码declare @JOB_DESC nvarchar (50)
declare @DEPARTMENT nvarchar (50)
IF @Initials = 'XBF'
BEGIN
SELECT @JOB_DESC = 'Partner', @DEPARTMENT = '_Banking and Finance'
END
ELSE IF @Initials = 'XMA'
BEGIN
SELECT @JOB_DESC = 'Partner', @DEPARTMENT = '_Disputes'
END
ELSE
BEGIN
SELECT @JOB_DESC = u.NAME, @DEPARTMENT = u.PracticeAreaName
FROM USERS u where u.USERID = @Initials
END
select @JOB_DESC
select @DEPARTMENT