[英]Stored procedure with case statement, subquery
if
@Status = '0' and @Complaint<> '0'
WITH NewTable As
(
Select sno = ROW_NUMBER()OVER (order by complaint_id), Complaint_Id, Complaint.ComplaintType_id, Complaint.complaintProfileId, Complaint.Description,
Complaint.Email, Complaint.PriorityLevel_id, Complaint.Date_Complained, Complaint.Status, Complaint.AdminComments, Complaint.Phone, Complaint.Evidence
from Complaints Complaint ),
with two as
if exists(select profile_id from MMBmembership
where profile_id = ComplaintProfileId)
select MMB_Name as @Name from MMB_BusinessProfiles where MMBId= (select MMB_id from MMBMembership
where profile_id = ComplaintProfileId )
if exists(select profile_id from UPPMembership
where profile_id = ComplaintProfileId)
set @Name = 'UPP'
else
set @name = 'Not Found'
SELECT * FROM NewTable,@Name left outer join two on
newtable.complaintProfileid = two.ProfileId
WHERE (ComplaintType_id = @Complaint) ORDER BY Date_Complained desc ,PriorityLevel_id
Thanks
Sun
除非我誤解了,否則我認為您不需要 SUB 查詢。 您只需要一個 LEFT JOIN,是的,您可以使用 CASE 語句
SELECT
CASE
WHEN MMB_Name IS NOT NULL THEN MMB_Name
WHEN UPPMembership.profile_id IS NOT NULL THEN 'UPP'
ELSE 'Not found'
END as Name
FROM Complaints Complaint
LEFT JOIN MMBMembership
ON MMBMembership.profile_id = Complaint.complaintProfileId
LEFT JOIN UPPMembership
ON UPPMembership.profile_id = Complaint.complaintProfileId
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.