SQL Server Management Studio 2016 中有关 JOIN 和 WITH 的语法问题

syntax issues regarding JOIN and WITH in SQL Server Management Studio 2016

我是 T-SQL 的新人,所以请多多包涵。

我正在尝试遵循以下说明:

执行以下操作

Join Indivs16000 to Cmtes16 on Indivs16000.CmteID = Cmtes16.CmteID 
with  Indivs16000.RecipCode not like P*  

如果您想包括对领导 PAC 的个人贡献,请不要根据 Recipcode 排除。相反,限制为 Indivs.Party 不为空且 Indivs.Party<>””(不等于空白。)

我想包括对领导力 PACS 的个人贡献。为了按照这些说明进行操作,我编写了以下查询:

JOIN Indivs16000 to Cmtes16
ON Indivs16000.CmteID = Cmtes16.CmteID
WITH Indivs16000.Party NOT NULL
AND Indivs16000.Party<>

执行后我遇到了以下错误:

Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'Join'.
Msg 319, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

出了什么问题?我正在使用 Microsoft SQL Server 2016 (Management Studio)。

SELECT * 
FROM 
Indivs16000 as A
INNER JOIN 
Cmtes16 as B
ON A.CmteID = B.CmteID
WHERE AND A.Party != '' and A.Party is not null

正如 Sean Lange 指出的那样,i.Party<>''i.Party > '' 消除了检查 i.Party 是否为 null 的需要。

您可以在 select 中选择要 return 的列以及 table 它们应该来自的列。

为您的 table 名称使用别名可以使内容更具可读性,并避免您浪费一些 finite number of keystrokes.

select 
    i.*
  , c.col1
  , c.col2
from Indivs16000 as i
  inner join Cmtes16 as c
    on i.CmteID = c.CmteID
where i.Party<>''
  --and i.Party is not null /* not needed with i.Party <>'' */