SQL 2005 - 表值函数编译正常,但在选择时会抛出错误语法近..

发布于 2024-09-16 16:50:08 字数 2926 浏览 2 评论 0原文

该表值函数编译正常,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终弃我 2024-09-23 16:50:08

将双引号更改为单引号,看看会发生什么...

...
..Hades ,'select '010' comno...
...

您还必须将所包含的单引号加倍。

当 SET QUOTED_IDENTIFIER 为 ON 时,“ 分隔对象/列名称等。不是字符串。这也是默认设置。

SET QUOTED_IDENTIFIER OFF 在运行时没有任何意义:它是在 CREATE/ALTER 时间设置的...这就是为什么您在运行时而不是 CREATE 时间收到错误的原因。CREATE <>顺便编译一下...它在运行时编译为计划。

Change double quote to single quote and see what happens...

...
..Hades ,'select '010' comno...
...

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文