我需要帮助调整下面的proc,以便能够在从服务表进行搜索时从组织表中返回结果。组织可以在以organization_id键入的服务表中列出多个服务。一个组织,许多服务。 当前的proc用于搜索组织并返回该行,如果所有字段都为空,则返回所有组织。我想要更改它,以便我仍然可以维护它,但如果搜索服务,它会返回组织。
SELECT
organization_id,
organization_nm,
contact_nm,
phone_nr,
email_ad,
last_upd_dt,
update_frequency_cd
FROM ORGANIZATION
WHERE
organization_id = CASE
WHEN @organization_id = 0 THEN organization_id
WHEN @organization_id IS NULL THEN organization_id
ELSE @organization_id END
AND organization_nm LIKE CASE
WHEN @organization_nm IS NULL THEN organization_nm
ELSE '%' + @organization_nm + '%' END
AND update_frequency_cd = CASE
WHEN @update_frequency_cd = 'x' THEN update_frequency_cd
ELSE @update_frequency_cd END
AND operator_id = CASE
WHEN @operator_id = 0 THEN operator_id
WHEN @operator_id IS NULL THEN operator_id
ELSE @operator_id END
ORDER BY organization_nm
我做了这个内部联接,它让我得到了我想要的结果,但是我对如何将它融入过程感到茫然。我一直在尝试很多方法让它发挥作用,但不想用这些尝试来解决我的问题。任何使这两件作品合身的帮助都会非常感激,或者如果我能够通过适当/更简单的方式学习这项工作。
这是我所做的内部联接,它可以让我在搜索服务时找到我想要的结果。
SELECT
org.organization_id,
org.organization_nm,
org.contact_nm,
org.phone_nr,
org.email_ad,
org.last_upd_dt,
org.update_frequency_cd,
service_nm
FROM dbo.ORGANIZATION AS Org
INNER JOIN
(
SELECT organization_id AS orgId, service_nm
FROM dbo.RECYCLE_SERVICE
WHERE service_nm = 'my service'
) AS serv
ON Org.organization_id = serv.orgId
答案 0 :(得分:0)
注意 - 用户社区需要确认原始帖子...编辑其他答案时
尝试:
SELECT
org.organization_id,
org.organization_nm,
org.contact_nm,
org.phone_nr,
org.email_ad,
org.last_upd_dt,
org.update_frequency_cd,
service_nm
FROM dbo.ORGANIZATION AS Org
INNER JOIN
(
SELECT organization_id AS orgId, service_nm
FROM dbo.RECYCLE_SERVICE
WHERE service_nm = 'my service'
) AS serv
ON Org.organization_id = serv.orgId
WHERE
organization_id = CASE
WHEN @organization_id = 0 THEN organization_id
WHEN @organization_id IS NULL THEN organization_id
ELSE @organization_id END
AND organization_nm LIKE CASE
WHEN @organization_nm IS NULL THEN organization_nm
ELSE '%' + @organization_nm + '%' END
AND update_frequency_cd = CASE
WHEN @update_frequency_cd = 'x' THEN update_frequency_cd
ELSE @update_frequency_cd END
AND operator_id = CASE
WHEN @operator_id = 0 THEN operator_id
WHEN @operator_id IS NULL THEN operator_id
ELSE @operator_id END
ORDER BY organization_nm