将临时表插入SQL Server中的递归查询的问题

时间:2015-09-01 10:36:28

标签: sql sql-server common-table-expression temp-tables

我有以下查询。

DECLARE @PartNo Char(22)
DECLARE @PartIssue AS CHAR(4)
DECLARE @Level AS INT
DECLARE @PartType AS CHAR(12)
DECLARE @TempLeadTime AS FLOAT
DECLARE @CumLeadTime AS FLOAT 
DECLARE @bomlforcursor  AS FLOAT
DECLARE @ldtimeforcursor AS FLOAT
DECLARE @temp_level as float
DECLARE @BomParent as NVARCHAR(50)
DECLARE @ldt as NVARCHAR(50)
DECLARE @TestCursor as CURSOR

SELECT @PartIssue = drawissno, @PartNo = partnum, @Level=1, @PartType = sm FROM partmaster  where partnum = 'AE40-0287810'

WITH Hierarchyct(BOMLevel,bmchild, parttype, bomparent, bmqty, leadtime, childissue,bmethod,rmethod, requiredqty) AS
      (SELECT     @Level AS BOMLevel,
                        @PartNo AS bomchild, 
                        @PartType as parttype,
                        cast('' AS CHAR(22)) AS bomparent,  
                        cast(1.0 AS FLOAT) AS bomqty, 
                        (SELECT stleadtim FROM stock WHERE stocknum = @PartNo) AS ldtime,
                        @PartIssue as childissue, 
                        eccbom AS bmethod, 
                        eccroute as rmethod,
                        cast(1.0 AS FLOAT) AS requiredqty
                        --@CumLeadTime AS cumleadtime
      FROM eccissue 
      WHERE eccpart = @PartNo AND eccissueno = @PartIssue 

      UNION ALL
      SELECT            (hierarchyct.BOMLevel + 1) AS BOMLevel,
                        bom.bomchild, 
                        (SELECT sm from partmaster where partnum = bom.bomchild) as parttype,
                        bom.bomparent, 
                        bom.bomqty, 
                        (SELECT stleadtim FROM stock WHERE stocknum = bom.bomchild) as ldtime,
                        --(CASE WHEN (stock.stleadtim = 0 AND parttype = 'MANUFACTURED') THEN (bom.bomqty*routing.runtim) ELSE (SELECT stleadtim FROM stock WHERE stocknum = bom.bomchild) END) AS ldtime,
                        (CASE WHEN LTRIM(RTRIM(bom.bomissue)) = 'CURR' THEN (SELECT drawissno FROM PartMaster WHERE PartNum = bom.bomchild) ELSE bom.bomissue END) AS childissue,
                        bom.baltmethod AS bmethod,    
                        (SELECT eccroute FROM eccissue WHERE eccissue.eccissueno = (CASE WHEN LTRIM(RTRIM(bom.bomissue)) = 'CURR' THEN (SELECT drawissno FROM PartMaster WHERE PartNum = bom.bomchild) ELSE bom.bomissue END) AND eccissue.eccpart = bom.bomchild ) as rmethod,
                        (bom.bomqty * hierarchyct.requiredqty) AS requiredqty
                       -- @CumLeadTime + leadtime as cumleadtime
      FROM bom  INNER JOIN hierarchyct ON bom.bomparent = hierarchyct.bmchild 


      WHERE bom.baltmethod =(SELECT eccbom FROM eccissue WHERE eccissue.eccpart = hierarchyct.bmchild AND eccissue.eccissueno = hierarchyct.childissue )
      )


SELECT * 
FROM
(     
SELECT      hierarchyct.*,
CASE WHEN (stleadtim = 0 AND partmaster.sm = 'MANUFACTURED') THEN (Hierarchyct.bmqty*sum(routing.runtim)) ELSE stleadtim  END AS ldtime, --, SUM(1) as cumldtime
CASE WHEN (stleadtim = 0 AND partmaster.sm = 'MANUFACTURED') THEN (Hierarchyct.requiredqty*sum(routing.runtim)) ELSE stleadtim  END AS ldtimeforRQ

--, SUM(1) as cumldtime
FROM  hierarchyct left outer join routing on part = bmchild and raltmethod = rmethod left outer join stock on stocknum = bmchild  left outer join partmaster on partnum = Hierarchyct.bmchild 
GROUP BY BOMLevel,bmchild, parttype, bomparent, bmqty, leadtime, childissue,bmethod,rmethod, requiredqty, stleadtim, sm, part
--Where leadtime =0Mr1990
) as temp

这很好,但是当我尝试使用这个修改过的查询将最终结果放入临时表(我需要这个来运行游标)时:

DECLARE @PartNo Char(22)
DECLARE @PartIssue AS CHAR(4)
DECLARE @Level AS INT
DECLARE @PartType AS CHAR(12)
DECLARE @TempLeadTime AS FLOAT
DECLARE @CumLeadTime AS FLOAT 
DECLARE @bomlforcursor  AS FLOAT
DECLARE @ldtimeforcursor AS FLOAT
DECLARE @temp_level as float
DECLARE @BomParent as NVARCHAR(50)
DECLARE @ldt as NVARCHAR(50)
DECLARE @TestCursor as CURSOR
CREATE TABLE #CumLeadTimRep (BOMLevel INT, bmchild NVARCHAR(50), parttype NVARCHAR(50), bomparent NVARCHAR(50), bmqty INT, leadtime INT, childissue NVARCHAR(50), bmethod INT, method INT, requiredqty INT, ldtime INT, ldtimeforRQ INT)
SELECT @PartIssue = drawissno, @PartNo = partnum, @Level=1, @PartType = sm FROM partmaster  where partnum = 'AE40-0287810'

WITH Hierarchyct(BOMLevel,bmchild, parttype, bomparent, bmqty, leadtime, childissue,bmethod,rmethod, requiredqty) AS
      (SELECT     @Level AS BOMLevel,
                        @PartNo AS bomchild, 
                        @PartType as parttype,
                        cast('' AS CHAR(22)) AS bomparent,  
                        cast(1.0 AS FLOAT) AS bomqty, 
                        (SELECT stleadtim FROM stock WHERE stocknum = @PartNo) AS ldtime,
                        @PartIssue as childissue, 
                        eccbom AS bmethod, 
                        eccroute as rmethod,
                        cast(1.0 AS FLOAT) AS requiredqty
                        --@CumLeadTime AS cumleadtime
      FROM eccissue 
      WHERE eccpart = @PartNo AND eccissueno = @PartIssue 

      UNION ALL
      SELECT            (hierarchyct.BOMLevel + 1) AS BOMLevel,
                        bom.bomchild, 
                        (SELECT sm from partmaster where partnum = bom.bomchild) as parttype,
                        bom.bomparent, 
                        bom.bomqty, 
                        (SELECT stleadtim FROM stock WHERE stocknum = bom.bomchild) as ldtime,
                        --(CASE WHEN (stock.stleadtim = 0 AND parttype = 'MANUFACTURED') THEN (bom.bomqty*routing.runtim) ELSE (SELECT stleadtim FROM stock WHERE stocknum = bom.bomchild) END) AS ldtime,
                        (CASE WHEN LTRIM(RTRIM(bom.bomissue)) = 'CURR' THEN (SELECT drawissno FROM PartMaster WHERE PartNum = bom.bomchild) ELSE bom.bomissue END) AS childissue,
                        bom.baltmethod AS bmethod,    
                        (SELECT eccroute FROM eccissue WHERE eccissue.eccissueno = (CASE WHEN LTRIM(RTRIM(bom.bomissue)) = 'CURR' THEN (SELECT drawissno FROM PartMaster WHERE PartNum = bom.bomchild) ELSE bom.bomissue END) AND eccissue.eccpart = bom.bomchild ) as rmethod,
                        (bom.bomqty * hierarchyct.requiredqty) AS requiredqty
                       -- @CumLeadTime + leadtime as cumleadtime
      FROM bom  INNER JOIN hierarchyct ON bom.bomparent = hierarchyct.bmchild 


      WHERE bom.baltmethod =(SELECT eccbom FROM eccissue WHERE eccissue.eccpart = hierarchyct.bmchild AND eccissue.eccissueno = hierarchyct.childissue )
      )


SELECT * INTO #CumLeadTimRep()
FROM
(     
SELECT      hierarchyct.*,
CASE WHEN (stleadtim = 0 AND partmaster.sm = 'MANUFACTURED') THEN (Hierarchyct.bmqty*sum(routing.runtim)) ELSE stleadtim  END AS ldtime, --, SUM(1) as cumldtime
CASE WHEN (stleadtim = 0 AND partmaster.sm = 'MANUFACTURED') THEN (Hierarchyct.requiredqty*sum(routing.runtim)) ELSE stleadtim  END AS ldtimeforRQ

--, SUM(1) as cumldtime
FROM  hierarchyct left outer join routing on part = bmchild and raltmethod = rmethod left outer join stock on stocknum = bmchild  left outer join partmaster on partnum = Hierarchyct.bmchild 
GROUP BY BOMLevel,bmchild, parttype, bomparent, bmqty, leadtime, childissue,bmethod,rmethod, requiredqty, stleadtim, sm, part
--Where leadtime =0Mr1990
) as temp

SELECT * FROM #CumLeadTimRep

我收到以下错误。

  

Msg 2714,Level 16,State 1,Line 51
  已经有一个名为' #CumLeadTimRep'在数据库中。

     

Msg 156,Level 15,State 1,Line 62
  关键字'附近的语法错误为'。

尝试了很多,但它没有得到修复。可能是什么问题?

1 个答案:

答案 0 :(得分:0)

找到答案。问题在于试图创建一个已经存在的表。正确的语法如下:

DECLARE @PartNo Char(22)
DECLARE @PartIssue AS CHAR(4)
DECLARE @Level AS INT
DECLARE @PartType AS CHAR(12)
DECLARE @TempLeadTime AS FLOAT
DECLARE @CumLeadTime AS FLOAT 
DECLARE @bomlforcursor  AS FLOAT
DECLARE @ldtimeforcursor AS FLOAT
DECLARE @temp_level as float
DECLARE @BomParent as NVARCHAR(50)
DECLARE @ldt as NVARCHAR(50)
DECLARE @TestCursor as CURSOR
IF OBJECT_ID('tempdb.dbo.#CumLeadTimRep') IS NOT NULL 
begin
DROP TABLE #CumLeadTimRep
end
--CREATE TABLE #CumLeadTimRep (BOMLevel INT, bmchild NVARCHAR(50), parttype NVARCHAR(50), bomparent NVARCHAR(50), bmqty INT, leadtime INT, childissue NVARCHAR(50), bmethod INT, method INT, requiredqty INT, ldtime INT, ldtimeforRQ INT)
SELECT @PartIssue = drawissno, @PartNo = partnum, @Level=1, @PartType = sm FROM partmaster  where partnum = 'AE40-0287810'
WITH Hierarchyct(BOMLevel,bmchild, parttype, bomparent, bmqty, leadtime, childissue,bmethod,rmethod, requiredqty) AS
      (SELECT     @Level AS BOMLevel,
                        @PartNo AS bomchild, 
                        @PartType as parttype,
                        cast('' AS CHAR(22)) AS bomparent,  
                        cast(1.0 AS FLOAT) AS bomqty, 
                        (SELECT stleadtim FROM stock WHERE stocknum = @PartNo) AS ldtime,
                        @PartIssue as childissue, 
                        eccbom AS bmethod, 
                        eccroute as rmethod,
                        cast(1.0 AS FLOAT) AS requiredqty
                        --@CumLeadTime AS cumleadtime
      FROM eccissue 
      WHERE eccpart = @PartNo AND eccissueno = @PartIssue 

      UNION ALL
      SELECT            (hierarchyct.BOMLevel + 1) AS BOMLevel,
                        bom.bomchild, 
                        (SELECT sm from partmaster where partnum = bom.bomchild) as parttype,
                        bom.bomparent, 
                        bom.bomqty, 
                        (SELECT stleadtim FROM stock WHERE stocknum = bom.bomchild) as ldtime,
                        --(CASE WHEN (stock.stleadtim = 0 AND parttype = 'MANUFACTURED') THEN (bom.bomqty*routing.runtim) ELSE (SELECT stleadtim FROM stock WHERE stocknum = bom.bomchild) END) AS ldtime,
                        (CASE WHEN LTRIM(RTRIM(bom.bomissue)) = 'CURR' THEN (SELECT drawissno FROM PartMaster WHERE PartNum = bom.bomchild) ELSE bom.bomissue END) AS childissue,
                        bom.baltmethod AS bmethod,    
                        (SELECT eccroute FROM eccissue WHERE eccissue.eccissueno = (CASE WHEN LTRIM(RTRIM(bom.bomissue)) = 'CURR' THEN (SELECT drawissno FROM PartMaster WHERE PartNum = bom.bomchild) ELSE bom.bomissue END) AND eccissue.eccpart = bom.bomchild ) as rmethod,
                        (bom.bomqty * hierarchyct.requiredqty) AS requiredqty
                       -- @CumLeadTime + leadtime as cumleadtime
      FROM bom  INNER JOIN hierarchyct ON bom.bomparent = hierarchyct.bmchild 


      WHERE bom.baltmethod =(SELECT eccbom FROM eccissue WHERE eccissue.eccpart = hierarchyct.bmchild AND eccissue.eccissueno = hierarchyct.childissue )
      )

SELECT * INTO #CumLeadTimRep
FROM
(     
SELECT      hierarchyct.*,
CASE WHEN (stleadtim = 0 AND partmaster.sm = 'MANUFACTURED') THEN (Hierarchyct.bmqty*sum(routing.runtim)) ELSE stleadtim  END AS ldtime, --, SUM(1) as cumldtime
CASE WHEN (stleadtim = 0 AND partmaster.sm = 'MANUFACTURED') THEN (Hierarchyct.requiredqty*sum(routing.runtim)) ELSE stleadtim  END AS ldtimeforRQ
--, SUM(1) as cumldtime
FROM  hierarchyct left outer join routing on part = bmchild and raltmethod = rmethod left outer join stock on stocknum = bmchild  left outer join partmaster on partnum = Hierarchyct.bmchild 
GROUP BY BOMLevel,bmchild, parttype, bomparent, bmqty, leadtime, childissue,bmethod,rmethod, requiredqty, stleadtim, sm, part
--Where leadtime =0Mr1990
) as temp
SELECT * FROM #CumLeadTimRep