首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要替代SQL Server 2000的交叉应用

需要替代SQL Server 2000的交叉应用
EN

Stack Overflow用户
提问于 2012-05-08 05:07:36
回答 1查看 1.8K关注 0票数 1

我使用交叉应用参数编写了以下脚本,该脚本在我的SQL Server 2008 developer数据库中运行良好,但对于具有SQL Server 2000数据库且不想更改SSMS中的兼容性设置的客户端则不起作用。

有人可以建议一种方法来转换我下面的脚本,以便不使用交叉应用参数吗?谢谢!

代码语言:javascript
复制
DECLARE @Flag INT
SET @Flag = 1
WHILE (@Flag < (SELECT TOP 1 (COUNT(CUSTOMERPN)) AS COUNTCUST FROM FC_Nestle
GROUP BY CustomerPN
ORDER BY COUNTCUST DESC))
BEGIN
--UPDATE AFS_TOPRODUCE COUNTS
UPDATE FC_Nestle
SET AFS_ToProduce = CustReqQty - AFS_OH
--UPDATE SUBSEQUENT AFS_OH INVENTORY COUNTS
update FC_Nestle 
set AFS_OH = - fc2.AFS_ToProduce 
from FC_Nestle 
CROSS APPLY 
(   
    select fc2.AFS_ToProduce   
    from    
    (     
        select top 1         
            fc2.AFS_ToProduce       
        from FC_Nestle fc2      
        where fc2.ForecastID < FC_Nestle.ForecastID and fc2.CustomerPN = FC_Nestle.CustomerPN     
        order by fc2.ForecastID desc   
    ) fc2   
    where fc2.AFS_ToProduce < 0 
) fc2 
where FC_Nestle.AFS_ToProduce > 0 
SET @Flag = @Flag + 1
END
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-08 05:55:56

这应该会对你有所帮助:

代码语言:javascript
复制
update fc_test
   set AFS_OH = - fc2.AFS_ToProduce
  from fc_test
 -- Self join to find previous row
 inner join fc_test fc2
 -- which has lower forecastid
    on fc2.ForecastID < fc_test.ForecastID
 where fc_test.AFS_ToProduce > 0
 -- and negative afs_toproduce
   and fc2.afs_toproduce < 0
 -- and is a row immediately before
 -- ie there is no other row closer to fc_test then current fc2
   and not exists (select null 
                     from fc_test fc3
                    where fc3.forecastid > fc2.forecastid
                      and fc3.ForecastID < fc_test.ForecastID)

我使用了与上次相同的表名和列名。

现场测试在Sql Fiddle上。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10489154

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档