SQL 2005 - 表值函数编译好但在选中时抛出不正确的语法..

时间:2010-08-28 13:54:38

标签: sql-server-2005 tsql oracle10g

这个表值函数编译好了,

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'。

1 个答案:

答案 0 :(得分:1)

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

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

你也必须将所包含的单曲加倍。

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

SET QUOTED_IDENTIFIER OFF has no meaning at runtime:它设置为CREATE / ALTER时间...这就是为什么你在运行时得到的错误而不是CREATE时间。创建&lt;&gt;顺便编译......它在运行时编译成计划。