【问题标题】:Converting DB2 Function to SQL Server Function将 DB2 函数转换为 SQL Server 函数
【发布时间】:2016-06-21 05:11:30
【问题描述】:

我正在将一堆函数从 DB2 转换为 SQL Server,这个让我卡住了,我已经从这个转换了函数:

create function prf_t_excinter_nzw(AC_ID_i int, TFH_i double) specific prf_t_excinter_nzw   
RETURNS TABLE(ft_id int,rg_id int,thr double,value double        )  
R1: BEGIN ATOMIC
RETURN  select l.ft_id, l.rg_id, r.thr,     case when l.from is null      
then to_value else ( thr - from ) / ( to - from ) * ( to_value - from_value ) + from_value end as value        
from (     select ft_id, rg_id, to, from, to_value, from_value     from    (     select       row_number() over ( 
partition by l.ft_id, l.rg_id, l.thr order by l.ft_id, l.rg_id, l.thr, r.thr desc) as #,
l.ft_id, l.rg_id, l.thr as to, r.thr as from, l.value as to_value, r.value as from_value
from     (select ft_id, rg_id, thr, value from table ( prf_t_ftexc_nzw( AC_ID_i, TFH_i)) as d     
order by  ft_id, rg_id, thr asc     ) as l     
left outer join     (      select ft_id, rg_id, thr, value       from table ( prf_t_ftexc_nzw( AC_ID_i, TFH_i)) as d order by ft_id, rg_id, thr asc) as r     
on l.ft_id = r.ft_id and l.rg_id = r.rg_id and      l.thr > r.thr    ) as l_r    where # = 1  ) as l  join  (   select prm_id, thr from prf_t_prm_1d_thr as r 
where prm_id in ( select id from prf_t_prm where name like '%RG_NZW')    ) as r  on l.rg_id = r.prm_id and( l.from < r.thr or l.from is null) and  ( r.thr <= l.to )  
order by l.ft_id, l.rg_id, r.thr  ;  END

到这里:

create function prf_t_excinter_nzw(@AC_ID_i int,@TFH_i float)
RETURNS TABLE
RETURN  select l.ft_id, l.rg_id, r.thr,case when l.from_value is null then to_value else ( thr-from_value )/(to_value-from_value)*(to_value-from_value)+from_value end as value
from (
select ft_id,rg_id,to_value,from_value,to_value,from_value 
from (
    select row_number() over ( partition by l.ft_id, l.rg_id, l.thr order by l.ft_id,l.rg_id, l.thr, r.thr desc) as num, l.ft_id, l.rg_id, l.to_value, r.from_value
    from (
        select ft_id, rg_id, thr, ac_value as to_value 
        from prf_t_ftexc_nzw(@AC_ID_i,@TFH_i) as d) as l 
        left outer join (
            select ft_id, rg_id, thr, ac_value as from_value
            from prf_t_ftexc_nzw(@AC_ID_i,@TFH_i) as d) as r 
            on l.ft_id = r.ft_id 
            and l.rg_id=r.rg_id 
            and l.thr > r.thr) as l_r 
            where num = 1) as l 
            join (
                select prm_id, thr from prf_t_prm_1d_thr as r 
                where prm_id in (select id from prf_t_prm where name like '%RG_NZW')) as r  
                on l.rg_id=r.prm_id and (l.from_value<r.thr or l.from_value is null) 
                and (r.thr<=l.to_value)

此时我收到错误消息 8156,级别 16,状态 1,过程 prf_t_excinter_nzw,第 18 行 为“l”多次指定了“to_value”列 有什么提示/建议可以解决吗?

【问题讨论】:

  • 您是否尝试过 Microsoft 提供的用于 DB2 工具的 SSMA 6.0.1。 microsoft.com/en-us/download/details.aspx?id=51216
  • @Hiten004 我尝试用 SSMA 6.0.0 转换它,但它失败了,我可以尝试使用最新版本,认为它是 6.0.2
  • 您是否查看了错误。我想你可能想把它作为问题发布!
  • SSA 无法解析原始 SQL,但如果您认为它有帮助,我可以发布它

标签: sql sql-server db2


【解决方案1】:

第5行肯定是报错了:

select ft_id,rg_id,to_value,from_value,to_value,from_value 

to_valuefrom_valueSELECT 列表中列出了两次。如果不是子查询,这会很好,但由于它是子查询,因此每个字段都需要唯一的名称,在这种情况下,它们是多余的,因此只需删除重复项。

错误消息将您指向 l 对象,这是您为子查询提供的别名,该子查询以上述有问题的 SELECT 行开头。

如果您不对查询中的不同对象重复使用别名,这些问题就会更容易被发现,因为这里有一些标记为 l 的东西。

【讨论】:

  • 不错,更改受保护的名称有点忘乎所以,谢谢!解决了它!
【解决方案2】:

问题是在第二级选择中有两次字段“to_value”。

删除一个

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 2015-12-25
    相关资源
    最近更新 更多