多个步骤/在光标中选择

时间:2018-05-24 19:02:55

标签: sql sql-server tsql

目前我遇到的问题是我试图在游标中运行几个步骤但到目前为止没有运气。

目标: 1.使用8k基础产品计算1个相关产品的“交叉销售率”

逻辑 1.检索包含“相关产品”(TempdbB)的所有OrderID(1次) 2.检索包含基础产品的所有OrderID'(TempdbA)(8k次) 3.内部加入他们找到常见的OrderID(TempdbC)(8k次) 4.计算(Common OrderID)/ Count(基本产品OrderID)以获得'交叉销售率'(8k次)(输出到结果表中)

我最初的想法是在每个基础产品的步骤2,3,4上运行光标,然后计算交叉销售率并插入“结果表”。

在游标循环中创建几个带有少量SELECT语句的tempdb时,问题就出现了一堆错误。我想知道如何处理这个问题。

非常感谢你,感谢任何帮助!

以下代码:

-----------------------DECLARE VARIABLES------------------------------------
DECLARE @brand int SET @brand = 1 
DECLARE @store int SET @store = 01920 
DECLARE @sku nvarchar(max)  SET @sku = 'xxx'  -- Related Product- 
DECLARE @vsku nvarchar(max) --USED for Cursor to insert Base Products
DECLARE @startdate datetime SET @startdate = '2018-05-09'  --Set Start Date
DECLARE @enddate datetime  SET @enddate = '2018-05-22' -- Set End Date


------------------------CREATE TEMPDB --------------------------------------
IF OBJECT_ID('tempdb..#TempA') IS NOT NULL --TempA: Pull ALL ORDERS CONTAINING RELATED Product 
DROP TABLE #TempA
IF OBJECT_ID('tempdb..#TempB') IS NOT NULL --TempB: PULL ALL ORDERS CONTAINING Base Product
DROP TABLE #TempB
IF OBJECT_ID('tempdb..#TempC') IS NOT NULL--TempC: PULL ALL ORDERS THAT CONTAIN BOTH Related and Base Product
    DROP TABLE #TempC

CREATE TABLE #Output(
        [BaseProduct] nvarchar(max),
        [RelatedProduct] nvarchar(max),
        [Cross-SellRate] nvarchar(max)
        )

-------TEMPB A: PULL ALL OrderID CONTAINING Related Product-----------------
    SELECT DISTINCT OpOrID, OpPrSKU, OpQty, OpCancelled
    into #TempA
    FROM tblOrder op (NOLOCK) 
    JOIN tblpayment orp (NOLOCK) ON op.oporid = orp.PyOrID
    WHERE orp.PyDateNew BETWEEN @startdate AND @enddate AND opprsku = @sku AND opcancelled = 0


---------------------DECLARE CURSOR-----------------------------------------
Declare x cursor for
Select distinct [Base Product] from tblCrossSellData 

Open x
Fetch Next From X into @vsku

While @@FETCH_STATUS = 0 
BEGIN


------TEMPB: USE CURSOR TO PULL ALL ORDERIDS CONTAINING THE SPECIFIC ANCHOR Product FOR A SKU LIST 
SELECT DISTINCT OpOrID, OpSoID, OpPrSKU, OpQty, ClID 
    into #TempB
    FROM tblorder op (NOLOCK)
    INNER JOIN tblProduct m (NOLOCK) ON m.prsku = p.opprsku   
    INNER JOIN tblproclass c (NOLOCK) ON c.prsku = p.opprsku
    INNER JOIN tblpayment orp (NOLOCK) ON p.oporid = orp.PyOrID
    WHERE orp.PyDateNew BETWEEN @startdate AND @enddate AND opsoid = @store
    AND p.opprsku = @vsku AND OpCancelled = 0

------TEMPC: SELECT MUTUAL ORID---------------------------------------------
SELECT DISTINCT a.OpOrID, a.OpSoID
    into #TempC
    FROM #TempA a
    INNER JOIN #TempB b (NOLOCK) ON a.OpOrID = b.OpOrID

-----------------CALCULATION FOR Attachment Rate----------------------------
SELECT @vsku as 'Base SKU', @sku as 'Related SKU', 
CAST(CAST(((CAST((SELECT COUNT(OpOrID) FROM #TempC) as float))
                    /CAST((SELECT COUNT(OpOrID) FROM #TempB) as float)*100) as decimal(18,3)) as varchar(5)) + ' %'
     AS 'Cross-Sell Rate'
     Insert Into #Output

Drop table #TempB
Drop table #TempC

    FETCH NEXT FROM X into @vsku
End

Close X
Deallocate X

-----Retrieve 8k Rows of Base Product, Related Product and Attachment Rate--
Select * from #output

     drop table #TempA
     drop table #output
  

SQL IS的错误:消息156,级别15,状态1,行74不正确   关键字'FETCH'附近的语法。

我试图切换FETCH NEXT语句,它只会在不同的行上给我msg 156。

2 个答案:

答案 0 :(得分:0)

根据Seans的提议,这是一个基本的代码段。由于我没有可用的SQL Server,所以它只是快速的:

z

编辑:可能有一个更简单的代码导致预期的结果。但是,正如肖恩所说,我们需要一些样本数据和预期结果。令人困惑的是两个子查询中对两个表tblOrder和tblPayment的引用。可能根本不需要......

答案 1 :(得分:0)

这看起来不对我

SELECT @vsku as 'Base SKU', @sku as 'Related SKU', 
       CAST(CAST(((CAST((SELECT COUNT(OpOrID) FROM #TempC) as float))
                  /CAST((SELECT COUNT(OpOrID) FROM #TempB) as float)*100) 
            as decimal(18,3)) as varchar(5)) 
       + ' %' AS 'Cross-Sell Rate'
Insert Into #Output

Insert应该是第一个

创建那些#tables并截断,而不是每次都重新创建。