[英]SQL - Possible Dynamic SQL Join Issue?
I have been researching dynamic joins for a few hours now and am having an issue figuring this out. 我已经研究了几个小时的动态联接,并且在解决这个问题上遇到了问题。 I am bulk loading XML feed data into a temporary SQL table and then adding/updating the production table from the temp table. 我将XML feed数据批量加载到临时SQL表中,然后从temp表中添加/更新生产表。 However, I am also attempting to use conditional logic in order to preserve data when necessary. 但是,我也尝试使用条件逻辑以便在必要时保留数据。 The code below is basically a replication of the Merge() method that is only available in SQL Server 2008 and above (the production server is 2005). 下面的代码基本上是Merge()方法的复制,该方法仅在SQL Server 2008及更高版本(生产服务器为2005)中可用。 The conditional logic, specifically, that I need to implement in this query is that if the [eMail] field of the temp table is not blank, then update that field in the production table. 具体来说,我需要在此查询中实现的条件逻辑是:如果临时表的[eMail]字段不为空,则在生产表中更新该字段。 Otherwise, do not update the [eMail] field in the production server (in order to preserve data). 否则,请勿更新生产服务器中的[eMail]字段(以保留数据)。 Here is what I have tried, and does not work due to incorrect syntax: 这是我尝试过的,由于语法错误而无法使用:
UPDATE rt
SET rt.[AccountID] = tmp.[AccountID],
rt.[CustNbr] = tmp.[CustNbr],
rt.[ClientID] = tmp.[ClientID],
rt.[ClientX] = tmp.[ClientX],
rt.[CorpID] = tmp.[CorpID],
rt.[fName] = tmp.[fName],
rt.[lName] = tmp.[lName],
rt.[Position] = tmp.[Position],
rt.[eMail] = tmp.[eMail],
rt.[ClientNotes] = tmp.[ClientNotes],
rt.[DeptID] = tmp.[DeptID]
FROM AccountClient rt
IF(SELECT eMail FROM AccountClientTemp) <> ''
INNER JOIN AccountClientTemp tmp
ON tmp.[fName] = rt.[fName]
and tmp.[lName] = rt.[lName]
--and tmp.[eMail] = rt.[eMail]
and tmp.[Position] = rt.[Position]
and tmp.[DeptID] = rt.[DeptID]
ELSE
INNER JOIN AccountClientTemp tmp
ON tmp.[fName] = rt.[fName]
and tmp.[lName] = rt.[lName]
and tmp.[eMail] = rt.[eMail]
and tmp.[Position] = rt.[Position]
and tmp.[DeptID] = rt.[DeptID]
INSERT INTO AccountClient
([AccountID], [CustNbr], [ClientID], [CorpID], [fName], [lName], [Position],
[eMail], [ClientNotes], [DeptID])
SELECT tmp.[AccountID], tmp.[CustNbr], tmp.[ClientID], tmp.[CorpID], tmp.[fName], tmp.[lName],
tmp.[Position], tmp.[eMail], tmp.[ClientNotes], tmp.[DeptID]
FROM AccountClientTemp tmp
LEFT JOIN AccountClient rt
ON tmp.[fName] = rt.[fName]
and tmp.[lName] = rt.[lName]
and tmp.[eMail] = rt.[eMail]
and tmp.[Position] = rt.[Position]
WHERE rt.[fName] IS NULL
and rt.[lName] IS NULL
and rt.[eMail] IS NULL
and rt.[Position] IS NULL
You can't put an "if" statement in the middle of a query. 您不能在查询中间放置“ if”语句。 So, your update should look like: 因此,您的更新应如下所示:
UPDATE rt
SET rt.[AccountID] = tmp.[AccountID],
rt.[CustNbr] = tmp.[CustNbr],
rt.[ClientID] = tmp.[ClientID],
rt.[ClientX] = tmp.[ClientX],
rt.[CorpID] = tmp.[CorpID],
rt.[fName] = tmp.[fName],
rt.[lName] = tmp.[lName],
rt.[Position] = tmp.[Position],
rt.[eMail] = (case when tmp.[eMail] = '' then rt.eMail else tmp.eMail end),
rt.[ClientNotes] = tmp.[ClientNotes],
rt.[DeptID] = tmp.[DeptID]
FROM AccountClient rt
INNER JOIN AccountClientTemp tmp
ON tmp.[fName] = rt.[fName] and
tmp.[lName] = rt.[lName] and
(tmp.[eMail] = rt.[eMail] or tmp.eMail = '') and
tmp.[Position] = rt.[Position] and
tmp.[DeptID] = rt.[DeptID];
On first glance, the INSERT looks ok. 乍一看,INSERT看起来还不错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.