I am receiving the error SQL0104N An unexpected token "," was found following "select .loan_number". Expected tokens may include: "". SQLSTATE=42601
IF OBJECT_ID('tempdb..#Temp1') is not null begin drop table #Temp1
select *
into #Temp1
from openquery(LnkServer,
'
Select
X.loan_number,
X.ls_code,
x.ls_actual_completion_date
x.ls_scheduled_completion_date
,max(Case WHEN (ls_code = ''924'' and ls_scheduled_completion_date <> (''01/01/1900'')
then ls_scheduled_completion_code
else NULL End) as Ls_scheduled_completion_date
,max(Case WHEN (ls_code = ''926'' and ls_scheduled_completion_date <> (''01/01/1900'')
then ls_scheduled_completion_code
else NULL End) as Ls_scheduled_completion_date
,max(Case WHEN (ls_code = ''927'' and ls_scheduled_completion_date <> (''01/01/1900'')
then ls_scheduled_completion_code
else NULL End) as Ls_scheduled_completion_date
,max(Case WHEN (ls_code = ''928'' and ls_scheduled_completion_date <> (''01/01/1900'')
then ls_scheduled_completion_code
else NULL End) as Ls_scheduled_completion_date
from master x
inner join(select loan_number, MAX(ls_scheduled_completion_date) as COMPL_DATE
MAX(ls_actual_completion_date) as APPROVAL_DATE
from master
Where LS_code in (''924'', ''926'', ''927'', ''928'', ''D08'', ''H38'', ''H79'', ''H42'',
''M40'', ''M29'', ''M10'', ''M40'', ''P31'', ''P49'', ''S17'')
Group by loan_number order by loan_number) y
on x.loan_number = y.loan_number
and x.ls_scheduled_completion_date = y.Compl_date
and x.ls_actual_completion_date = y.approval_date
Group by x.loan_number, x.ls_actual_completion_date, ls_scheduled_date
for fetch only with ur')