有没有人知道这个SQL代码的问题在哪里?我使用UNION在查询中的FROM行周围不断获得红色下划线。如果你还能告诉我如何防止重复代码,甚至更好!
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 clientcontact.clientid=@clientid
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”子句将标量(也称为变量)与表(结果集)进行比较。
[标量] EXISTS([表/结果集])
[标量] IS NOT NULL且不是EXISTS([table / result set])
OR
[table / result set] IN([table / result set])
[表/结果集] 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 clientcontact.clientid=@clientid
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
答案 0 :(得分:2)
删除字段列表中的额外逗号。例如取代
...
SELECT
client_address.addressid,
client_address.addresstypeid,
FROM
...
与
...
SELECT
client_address.addressid,
client_address.addresstypeid
FROM
...
答案 1 :(得分:1)
您在这些查询的选择列表中的最后一列之后有逗号逗号。删除它们,这应该至少修复FROM
个关键字周围的红线。
即:
SELECT
client_address.addressid,
client_address.addresstypeid -- <-- trailing comma removed
FROM [...]
UNION
SELECT
contact_address.addressid,
contact_address.addresstypeid
FROM [...]