SQL NOT IN子句不工作
有人知道这个SQL代码的问题在哪里吗?在与UNION的查询中,我在FROM行的周围不断得到红色下划线。如果你能告诉我如何防止代码重复,那就更好了SQL NOT IN子句不工作,sql,sql-server-2008,syntax,Sql,Sql Server 2008,Syntax,有人知道这个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 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 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子句将标量aka变量与表结果集进行比较
[标量]存在[表/结果集]
[标量]不为NULL且不存在[表/结果集]
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
或
[表格/结果集]中的[表格/结果集]
[表格/结果集]不在[表格/结果集]中
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
在这些查询的select列表的最后一列后面有尾随逗号。删除它们,至少可以修复FROM关键字周围的红线 即:
在这些查询的select列表的最后一列后面有尾随逗号。删除它们,至少可以修复FROM关键字周围的红线 即:
删除字段列表中的多余逗号。e、 g.更换
...
SELECT
client_address.addressid,
client_address.addresstypeid,
FROM
...
与
删除字段列表中的多余逗号。e、 g.更换
...
SELECT
client_address.addressid,
client_address.addresstypeid,
FROM
...
与
我忘记了BEGIN/END词法组中的集合,也忘记了BEGIN/END词法组中的集合