This table valued function compiles ok,
alter function [dbo].[ftsls031nnnHades](@withExpiredEntries smallint ) returns
@t table( comno varchar(3),
t$cuno varchar(6),
t$cpgs varchar(6),
t$dile float,
t$qanp float,
t$stdt varchar(10),
t$tdat varchar(10),
t$disc float,
t$damt float,
t$cdis char(3),
t$gnpr float,
t$refcntd float,
t$refcntu float) as
------------------------------------------------------*/
/*-------------------------------------------------------
declare @withExpiredEntries bit; set @withExpiredEntries =0
declare @t table( comno varchar(3),
t$cuno varchar(6),
t$cpgs varchar(6),
t$dile float,
t$qanp float,
t$stdt varchar(10),
t$tdat varchar(10),
t$disc float,
t$damt float,
t$cdis char(3),
t$gnpr float,
t$refcntd float,
t$refcntu float)
------------------------------------------------------*/
Begin
set quoted_identifier off
if (@withExpiredEntries = 0) -- without expired entries
Begin
insert @t
select *
from openQuery(Hades ,"select '010' comno, trim(t$cuno) t$cuno,trim(t$cpgs) t$cpgs,t$dile,t$qanp,to_char(t$stdt,'dd Mon yy') t$stdt,to_char(t$tdat,'dd Mon yy') t$tdat,to_char(t$disc,'999.99') t$disc,t$damt,t$cdis,t$gnpr,t$refcntd,t$refcntu from baan.ttdsls031010 where (to_char(t$Tdat,'yyyy-mm-dd') >= To_char(current_date,'yyyy-mm-dd')) and (to_char(t$stdt,'yyyy-mm-dd') <= To_char(current_date,'yyyy-mm-dd'))
union all select '020' comno, trim(t$cuno) t$cuno,trim(t$cpgs) t$cpgs,t$dile,t$qanp,to_char(t$stdt,'dd Mon yy') t$stdt,to_char(t$tdat,'dd Mon yy') t$tdat,to_char(t$disc,'999.99') t$disc,t$damt,t$cdis,t$gnpr,t$refcntd,t$refcntu from baan.ttdsls031020 where (to_char(t$tdAt,'yyyy-mm-dd') >= To_char(current_date,'yyyy-mm-dd')) and (to_char(t$stdt,'yyyy-mm-dd') <= To_char(current_date,'yyyy-mm-dd'))")
return
End
insert @t
select *
from openQuery(Hades ,"select '010' comno, trim(t$cuno) t$cuno,trim(t$cpgs) t$cpgs,t$dile,t$qanp,to_char(t$stdt,'dd Mon yy') t$stdt,to_char(t$tdat,'dd Mon yy') t$tdat,to_char(t$disc,'999.99') t$disc,t$damt,t$cdis,t$gnpr,t$refcntd,t$refcntu from baan.ttdsls031010
union all select '020' comno, trim(t$cuno) t$cuno,trim(t$cpgs) t$cpgs,t$dile,t$qanp,to_char(t$stdt,'dd Mon yy') t$stdt,to_char(t$tdat,'dd Mon yy') t$tdat,to_char(t$disc,'999.99') t$disc,t$damt,t$cdis,t$gnpr,t$refcntd,t$refcntu from baan.ttdsls031020 ")
return
end
it throws this error when selected
Msg 102, Level 15, State 1, Line 1 Incorrect syntax near 'select '010' comno, trim(t$cuno) t$cuno,trim(t$cpgs) t$cpgs,t$dile,t$qanp,to_char(t$stdt,'dd Mon yy') t$stdt,to_char(t$tdat,'dd '.