有沒有人看到問題出在哪裏,這個SQL代碼?我用UNION在查詢的FROM行中不斷得到紅色下劃線。如果你能夠告訴我如何防止代碼重複,甚至更好!SQL NOT IN子句不起作用
DECLARE @collection_site_address_id INT;
SET @collection_site_address_id =
(
SELECT TOP 1
client_address.addressid
FROM
dbo.ws_test_request
INNER JOIN client ON ws_test_request.collection_site_id = client.identifyingnumber
INNER JOIN client_address ON client.clientid = client_address.clientid
WHERE
sample_specimen_id = @sample_identifyingnumber
AND client_address.addresstypeid = 1
)
IF (
(SELECT TOP 1
client_address.addressid
FROM
dbo.ws_test_request
INNER JOIN client ON ws_test_request.collection_site_id = client.identifyingnumber
INNER JOIN client_address ON client.clientid = client_address.clientid
WHERE
sample_specimen_id = @sample_identifyingnumber
AND client_address.addresstypeid = 1
)
NOT IN (
SELECT
[address].addressid
FROM
[address]
JOIN (
SELECT
client_address.addressid,
client_address.addresstypeid,
FROM
dbo.fnClientRelatives(@clientid, 0, 1, 0) relatives
INNER JOIN client_address on client_address.clientid = relatives.clientid
LEFT OUTER JOIN client ON relatives.clientid = dbo.client.clientid
UNION
SELECT
contact_address.addressid,
contact_address.addresstypeid,
FROM
clientcontact
INNER JOIN contact_address ON contact_address.contactid=clientcontact.contactid and [email protected]
LEFT OUTER JOIN [contact] ON [clientcontact].contactid = [contact].contactid
LEFT OUTER JOIN [address] ON contact_address.addressid = address.addressid
) AS client_addressexternal ON client_addressexternal.addressid = address.addressid
WHERE
client_addressexternal.addresstypeid IN (3,1)
)
)
BEGIN
@collection_site_address_id = @default_collection_site_address_id
END
========
答:
有點調查後,它看起來像一個更有效的方式。您可以使用「IN」或「NOT IN」子句將表(結果集)與另一個表(結果集)進行比較。或者,您可以使用「EXISTS」或「NOT EXISTS」子句將標量(aka變量)與表(結果集)進行比較。
[標量] EXISTS([表/結果集])
[標量] IS NOT NULL AND NOT EXISTS([表/結果集])
OR
[表/結果集] IN(表/結果集])
[表/結果集] NOT IN(表/結果集])
DECLARE @collection_site_address_id INT;
SET @collection_site_address_id =
(
SELECT TOP 1
client_address.addressid
FROM
dbo.ws_test_request
INNER JOIN client ON ws_test_request.collection_site_id = client.identifyingnumber
INNER JOIN client_address ON client.clientid = client_address.clientid
WHERE
sample_specimen_id = @sample_identifyingnumber
AND client_address.addresstypeid = 1
)
IF
@collection_site_address_id IS NOT NULL AND NOT EXISTS
(
SELECT
[address].addressid
FROM
[address]
JOIN (
SELECT
client_address.addressid,
client_address.addresstypeid
FROM
dbo.fnClientRelatives(@clientid, 0, 1, 0) relatives
INNER JOIN client_address on client_address.clientid = relatives.clientid
LEFT OUTER JOIN client ON relatives.clientid = dbo.client.clientid
UNION
SELECT
contact_address.addressid,
contact_address.addresstypeid
FROM
clientcontact
INNER JOIN contact_address ON contact_address.contactid=clientcontact.contactid and [email protected]
LEFT OUTER JOIN [contact] ON [clientcontact].contactid = [contact].contactid
LEFT OUTER JOIN [address] ON contact_address.addressid = address.addressid
) AS client_addressexternal ON client_addressexternal.addressid = address.addressid
WHERE
client_addressexternal.addresstypeid IN (3,1)
)
BEGIN
SET @collection_site_address_id = @default_collection_site_address_id
END
和我忘了BEGIN/END詞法組內的SET – MacGyver