我正在运行一个可能返回空行集的SQL查询。我想使用CASE语句处理它。
这是我尝试过的但是没有用
Case
When @@ROWCOUNT > 0 Then LTRIM(RTRIM(t.FirstName)) Else 'UNKNOWN'
End
case语句不返回任何值
如果我使用print @@ RowCount而不是Select我得到0值。
我该如何解决这个问题?
好的查询相当大,所以我会尝试解释它的作用。我在一个从HR数据库返回数据的函数中使用它。我需要员工信息,如姓名和工作任务。在某些情况下,我需要处理员工未知的数据。在那些情况下,我传递一个employeeId数为零。当我运行查询时,这会导致返回空。
我想要的是让函数将Name数据作为UNKNOWN返回。
希望这有助于清除请求
我已经修改了查询,因此我可以显示以下所有相关部分:
DECLARE @EID int = 0
DECLARE @AssignmentType varchar(max) = 'FirstName'
DECLARE @ReturnValue varchar(max)
DECLARE @EIDTemp TABLE(EID varchar(6), Section varchar(max), Division
varchar(max), Bureau varchar(max), FirstName varchar(max), MiddleName
varchar(max), LastName varchar(max));
Insert Into @EIDTemp
SELECT L1.Employee, P1.NAME As Section,
CASE
WHEN L1.DEPARTMENT IN (7010) THEN 'Legal Services Division'
WHEN L1.DEPARTMENT IN (7030,7040) THEN 'Fiscal Management Division'
Else 'Other'
END AS Division,
CASE
WHEN L1.DEPARTMENT IN (7130) THEN 'Administrative Services Bureau'
ELSE ','
END As Bureau
,L1.FIRST_NAME
,L1.MIDDLE_NAME
,L1.LAST_NAME
FROM [SOOPS-LAWREPT].[LAWDATA].dbo.EMPLOYEE L1
JOIN [SOOPS-LAWREPT].[LAWDATA].dbo.DEPTCODE L2 ON L1.Department =
L2.Department
JOIN [SOOPS-LAWREPT].[LAWDATA].dbo.JOBCODE L3 on L1.JOB_CODE = L3.JOB_CODE
JOIN [SOOPS-LAWREPT].[LAWDATA].dbo.PRSYSTEM P1 ON L1.PROCESS_LEVEL =
p1.PROCESS_LEVEL
WHERE L1.EMP_STATUS='A1'
-- Select the Return Value
-------------------------------
IF (@AssignmentType = 'FirstName')
BEGIN
SET @ReturnValue = (SELECT TOP 1 LTRIM(RTRIM(t.MiddleName)) From @EIDTemp t
where EID = @EID);
END
IF (@AssignmentType = 'MiddleName')
BEGIN
SET @ReturnValue = (SELECT TOP 1 LTRIM(RTRIM(t.MiddleName)) From @EIDTemp t
where EID = @EID);
END
IF (@AssignmentType = 'LastName')
BEGIN
SET @ReturnValue = (SELECT TOP 1 LTRIM(RTRIM(t.LastName)) From @EIDTemp t
where EID = @EID);
END
答案 0 :(得分:1)
问题在于如何设置变量。当您使用SET时,如果没有返回任何行,它将更改为NULL。
您应该更改代码,以便它执行类似的操作。
SET @ReturnValue = 'Unknown'
IF (@AssignmentType = 'FirstName')
BEGIN
SELECT @ReturnValue = LTRIM(RTRIM(t.MiddleName)) From @EIDTemp t
where EID = @EID
END
这会将初始值设置为“未知”。这将在您的所有IF语句之前,并将在整个集合中工作。如果没有返回行,则值将保持不变。我删除了TOP 1,因为它不需要。如果您有多行,它将从结果集的最后一行接收值。如果有很多行,你总是可以使用MAX或MIN。
答案 1 :(得分:1)
@Perry,以下内容更像您所寻找的内容:
WITH employee_ids AS
(
SELECT
emp.Employee AS eid
,P1.NAME As Section
,CASE
WHEN emp.DEPARTMENT IN (7010) THEN
'Legal Services Division'
WHEN emp.DEPARTMENT IN (7030,7040) THEN
'Fiscal Management Division'
ELSE
'Other'
END AS Division
,CASE
WHEN emp.DEPARTMENT IN (7130) THEN
'Administrative Services Bureau'
ELSE
','
END As Bureau
,emp.FIRST_NAME
,emp.MIDDLE_NAME
,emp.LAST_NAME
,ROW_NUMBER() OVER (PARTITION BY emp.EMPLOYEE) AS eid_match
FROM
[SOOPS-LAWREPT].[LAWDATA].dbo.EMPLOYEE AS emp
INNER JOIN
[SOOPS-LAWREPT].[LAWDATA].dbo.DEPTCODE AS depts
ON (emp.Department = depts.Department)
INNER JOIN
[SOOPS-LAWREPT].[LAWDATA].dbo.JOBCODE AS jcs
ON (emp.JOB_CODE = jcs.JOB_CODE)
INNER JOIN
[SOOPS-LAWREPT].[LAWDATA].dbo.PRSYSTEM AS prs
ON (emp.PROCESS_LEVEL = prs.PROCESS_LEVEL)
WHERE
emp.EMP_STATUS='A1'
)
,provisional_results AS
(
SELECT
CASE @AssignmentType
WHEN 'FirstName' THEN
employee_ids.FirstName
WHEN 'MiddleName' THEN
employee_ids.MiddleName
WHEN 'LastName' THEN
employee_ids.LastName
ELSE
NULL
END AS provisional_return_value
FROM
(VALUES (0)) AS default_values(default_value)
LEFT JOIN
employee_ids
ON (employee_ids.eid = @eid)
AND (eid_match = 1)
)
SELECT
@ReturnValue = ISNULL(LTRIM(RTRIM(provisional_return_value)), 'UNKNOWN')
FROM
provisional_results
我还没有对我的桌面进行测试,因为我不在我的桌面上,所以请原谅任何小的语法错误,但它应该达到你所需要的。
答案 2 :(得分:1)
运行insert / select后需要立即保存@@RowCount
变量并返回变量。如果您不保存变量,另一个语句最终将清除行数...
示例:强>
--Variable to hold the rowcount
DECLARE @Count int = 0
...
Insert Into @EIDTemp
SELECT L1.Employee, P1.NAME As Section,
...
WHERE L1.EMP_STATUS='A1'
--Save the result
SELECT @Count=@@RowCount
...
--Use the saved result
Case
When @Count > 0 Then LTRIM(RTRIM(t.FirstName)) Else 'UNKNOWN'
End
答案 3 :(得分:0)
我的同事通过在开始时检查员工ID的值并使用此代码返回UNKNOWN来解决问题
> DECLARE @EID int = 0
DECLARE @AssignmentType varchar(max) = 'FirstName'
DECLARE @ReturnValue varchar(max) = 'UNKNOWN'
DECLARE @EIDTemp TABLE(EID varchar(6), Section varchar(max), Division
varchar(max), Bureau varchar(max), FirstName varchar(max), MiddleName
varchar(max), LastName varchar(max));
if @EID = 0
goto done
-- Insert into a Temp Table
-------------------------------
Insert Into @EIDTemp
SELECT L1.Employee, P1.NAME As Section,
CASE
WHEN L1.DEPARTMENT IN (7010) THEN 'Legal Services Division'
WHEN L1.DEPARTMENT IN (7030,7040) THEN 'Fiscal Management
Division'
Else 'Other'
END AS Division,
CASE
WHEN L1.DEPARTMENT IN (7130) THEN 'Administrative Services Bureau'
ELSE ','
END As Bureau
,L1.FIRST_NAME
,L1.MIDDLE_NAME
,L1.LAST_NAME
FROM [SOOPS-LAWREPT].[LAWDATA].dbo.EMPLOYEE L1
JOIN [SOOPS-LAWREPT].[LAWDATA].dbo.DEPTCODE L2 ON L1.Department =
L2.Department
JOIN [SOOPS-LAWREPT].[LAWDATA].dbo.JOBCODE L3 on L1.JOB_CODE =
L3.JOB_CODE
JOIN [SOOPS-LAWREPT].[LAWDATA].dbo.PRSYSTEM P1 ON L1.PROCESS_LEVEL =
p1.PROCESS_LEVEL
WHERE L1.EMP_STATUS='A1' AND l1.EMPLOYEE = @EID
-- Select the Return Value
-------------------------------
IF (@AssignmentType = 'FirstName')
BEGIN
SET @ReturnValue = (SELECT TOP 1 LTRIM(RTRIM(t.FirstName)) From @EIDTemp
t where EID = @EID);
END
IF (@AssignmentType = 'MiddleName')
BEGIN
SET @ReturnValue = (SELECT TOP 1 LTRIM(RTRIM(t.MiddleName)) From
@EIDTemp t where EID = @EID);
END
IF (@AssignmentType = 'LastName')
BEGIN
SET @ReturnValue = (SELECT TOP 1 LTRIM(RTRIM(t.LastName)) From @EIDTemp
t where EID = @EID);
END
done:
print @ReturnValue