Reputation: 18980
Does anyone see where the issue is with this SQL code? I keep getting red underscores around the FROM lines in the query with the UNION. If you're able to also tell me how I can prevent the duplication of code, even better!!
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
========
After a bit of investigation, it looks like a more efficient way. You can either compare a table (result set) to another table (result set) using the "IN" or "NOT IN" clause. Or you can compare a scalar (aka variable) to a table (result set) using the "EXISTS" or "NOT EXISTS" clause.
[scalar] EXISTS ([table/result set])
[scalar] IS NOT NULL AND NOT EXISTS ([table/result set])
OR
[table/result set] IN ([table/result set])
[table/result set] NOT IN ([table/result set])
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
Upvotes: 0
Views: 843
Reputation: 10104
Remove the extra commas in the field list. e.g. replace
...
SELECT
client_address.addressid,
client_address.addresstypeid,
FROM
...
with
...
SELECT
client_address.addressid,
client_address.addresstypeid
FROM
...
Upvotes: 2
Reputation: 16708
You've got trailing commas after the last column in the select lists of those queries. Remove them and that should fix the red lines around the FROM
keywords, at least.
I.e.:
SELECT
client_address.addressid,
client_address.addresstypeid -- <-- trailing comma removed
FROM [...]
UNION
SELECT
contact_address.addressid,
contact_address.addresstypeid
FROM [...]
Upvotes: 1