2016-08-29 3 views
0

Я бегу этот запрос и получаю ошибку:Inner Присоединяйтесь и Coalesce вопрос

Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '.'.

Msg 4145, Level 15, State 1, Line 6
An expression of non-boolean type specified in a context where a condition is expected, near 'and'.

Моя целевая таблица содержит NULLs и источник содержит NULLs и данные.

Мне нужно заполнить цель адресами из исходной таблицы.

DECLARE @FullStatement nvarchar(4000) 
SET @FullStatement= ' Using ' + @TargetTable+ ' AS itarget'+ 
          ' USING '[email protected]+' AS isource 

SELECT 
    isource.[Address], itarget.[Address] 
FROM 
    isource 
INNER JOIN 
    itarget ON target.VersionEndDate IS NULL 
      AND target.ResearchPropertyId = source.ResearchPropertyId 
    (coalesce(isource.[Address], '') = coalesce(itarget.[Addres], '')) and 
    (coalesce(itarget.[Address], '') = coalesce(isource.[Address], ''))' 

IF @debug = 1 
    PRINT @FullStatement 

EXECUTE sp_executesql @FullStatement 

ответ

0

вам нужно изменить каждый '' на '' '' в этом блоке:

(coalesce(isource.[Address], '') = coalesce(itarget.[Addres], '')) and 
    (coalesce(itarget.[Address], '') = coalesce(isource.[Address], '')) 
+0

Thanks Anoton. Я получаю эту ошибку сейчас ... Msg 102, Level 15, State 1, Line 3 Неправильный синтаксис около '.'. Msg 102, Level 15, State 1, Line 7 Неверный синтаксис рядом с '='. – Camille

+0

, когда вы печатаете сообщение, вы должны иметь возможность запускать этот скрипт. Если это не терпит неудачу, то EXECUTE также не должен терпеть неудачу. В вашем скрипте есть некоторые опечатки, такие как Addres, source вместо isource, target вместо itarget и т. Д. Исправьте все, напечатайте «@FullStatement» и попробуйте скопировать-вставить и выполнить его без EXECUTE. Если он по-прежнему не работает, отредактируйте сообщение и добавьте это значение «@FullStatement» – Anton

+0

. Я удалил опечатки и никаких ошибок больше. Однако столбец «Цель» не обновлялся. Думаю, мне нужно использовать MERGE? – Camille

0

AND и single quotes отсутствуют.

DECLARE @FullStatement nvarchar(4000) 
SET @FullStatement= ' Using ' + @TargetTable+ ' AS itarget'+ 
          ' USING '[email protected]+' AS isource 

SELECT 
    isource.[Address], itarget.[Address] 
FROM 
    isource 
INNER JOIN 
    itarget ON target.VersionEndDate IS NULL 
      AND target.ResearchPropertyId = source.ResearchPropertyId AND -- Here Need a AND 
    (coalesce(isource.[Address], '''') = coalesce(itarget.[Addres], '''')) and 
    (coalesce(itarget.[Address], '''') = coalesce(isource.[Address], ''''))' 

IF @debug = 1 
    PRINT @FullStatement 

EXECUTE sp_executesql @FullStatement