SQL 2005 - 表值函数编译正常,但在选择时会抛出错误语法近..
该表值函数编译正常,
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
时,它会抛出此错误
当选择Msg 102, Level 15, State 1, Line 1 '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'.
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 '.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将双引号更改为单引号,看看会发生什么...
您还必须将所包含的单引号加倍。
当 SET QUOTED_IDENTIFIER 为 ON 时,“ 分隔对象/列名称等。不是字符串。这也是默认设置。
SET QUOTED_IDENTIFIER OFF 在运行时没有任何意义:它是在 CREATE/ALTER 时间设置的...这就是为什么您在运行时而不是 CREATE 时间收到错误的原因。CREATE <>顺便编译一下...它在运行时编译为计划。
Change double quote to single quote and see what happens...
You'll also have to double up the contained singles too.
When SET QUOTED_IDENTIFIER is ON, " delimits an object/column name etc. Not a string. This is the default setting too.
The SET QUOTED_IDENTIFIER OFF has no meaning at runtime: it's set at CREATE/ALTER time... this is why you get the error at runtime not CREATE time. CREATE <> compile by the way... it's compiled to a plan at runtime.