如何将Cursor转换为SELECT查询?

时间:2011-03-30 20:14:01

标签: tsql select cursor

我正在使用SSRS 2008,我有一个存储过程,目前使用游标删除和添加数据,但即使我将其转换为选择查询,我收到错误:

  

Msg 102,Level 15,State 1,Line 39   ')'附近的语法不正确。

这是原作Cursor:

OPEN PARTS
FETCH PARTS INTO    @PART_NUM,
@PART_DESC
SET @PARTS_FETCH = @@FETCH_STATUS
WHILE @PARTS_FETCH = 0 BEGIN
SET @THE_DATE = dateadd("yy", -1, dateadd("m", -1, getdate()))
SET @END_DATE = DATEADD(ms, -5, DATEADD(mm, DATEDIFF(m, 0, getdate()) + 1, 0))

-- Get PL for part number 
Delete from @tbl_PL

Insert @tbl_PL 
SELECT FRUD.tblXref.product_code FROM FRUD.tblfieldOrderablePart INNER JOIN
FRUD.MAX_APPROVED ON FRUD.tblfieldOrderablePart.fop_no = FRUD.MAX_APPROVED.fop_no AND 
FRUD.tblfieldOrderablePart.fop_revision = FRUD.MAX_APPROVED.MaxOffop_revision INNER JOIN
FRUD.tblXref ON FRUD.MAX_APPROVED.fop_no = FRUD.tblXref.fop_no AND 
FRUD.MAX_APPROVED.MaxOffop_revision = FRUD.tblXref.fop_revision
WHERE (dbo.FORMAT_PART(FRUD.tblfieldOrderablePart.fop_no) = dbo.FORMAT_PART(@PART_NUM))
-- End Get PL

WHILE @THE_DATE <= @END_DATE BEGIN
SET @THE_DATE = DATEADD(ms, -5, DATEADD(mm, DATEDIFF(m, 0, @THE_DATE) + 1, 0))
-- Get census using PL          
SELECT @ALL_TOTAL = SUM(TheSum) FROM (SELECT CAST(RELIABILITY.Census.Census AS DECIMAL(9,2)) AS TheSum, (CONVERT(datetime, LEFT(CONVERT(char, Period), 4) + '-' + RIGHT(Period, 2) + '-1', 102)) as ThePeriod
FROM RELIABILITY.Census
WHERE RELIABILITY.Census.PL In (Select distinct * FROM @tbl_PL)
AND (CONVERT(datetime, LEFT(CONVERT(char, Period), 4) + '-' + RIGHT(Period, 2) + '-1', 102) >=
DATEADD(mm, DATEDIFF(mm, 0, @THE_DATE) - 5, 0)) AND (CONVERT(datetime, LEFT(CONVERT(char, Period), 4)
+ '-' + RIGHT(Period, 2) + '-1', 102) <= @THE_DATE)                         UNION ALL                           SELECT     CAST(Census AS DECIMAL(9,2)) AS TheSum, Period                           FROM         [MANUAL].SMARTSOLVE_CENSUS                         WHERE     (Period >= DATEADD(mm, DATEDIFF(mm, 0, @THE_DATE) - 5, 0) AND Period <= @THE_DATE) AND (PL In (Select distinct * FROM @tbl_PL)))A 

这是我转换为选择:

DECLARE @THE_DATE datetime,
        @END_DATE datetime,
        @THE_GOAL decimal(18,2),
        @PART_NUM nvarchar(50),
        @UNCHANGED_PART_NUM nvarchar(50),
        @PART_DESC varchar(35),
        @PARTS_FETCH int,
        @NUM_FAILED int,
        @AVG_CENSUS decimal(18,2),
        @PL_VAR nvarchar(50),   
        @PL_FETCH int,
        @PL_TOTAL decimal(9,2),
        @ALL_TOTAL decimal(9,2)

--WHILE @PARTS_FETCH = 0 BEGIN
    SET @THE_DATE = dateadd("yy", -1, dateadd("m", -1, getdate()))
    SET @END_DATE = DATEADD(ms, -5, DATEADD(mm, DATEDIFF(m, 0, getdate()) + 1, 0))  
    --WHILE @THE_DATE <= @END_DATE BEGIN
SET @THE_DATE = DATEADD(ms, -5, DATEADD(mm, DATEDIFF(m, 0, @THE_DATE) + 1, 0))
-- Get census using PL

SELECT @ALL_TOTAL = SUM(TheSum) FROM 
(SELECT CAST(RELIABILITY.Census.Census AS DECIMAL(9,2)) AS TheSum 
from RELIABILITY.Census
WHERE RELIABILITY.Census.PL In (Select distinct * FROM #tbl_PL) 
AND (CONVERT(datetime, LEFT(CONVERT(char, Period), 4) + '-' + RIGHT(Period, 2) + '-1', 102) >=  
DATEADD(mm, DATEDIFF(mm, 0, @THE_DATE) - 5, 0)) AND (CONVERT(datetime, LEFT(CONVERT(char, Period), 4)+ '-' + RIGHT(Period, 2) + '-1', 102) <= @THE_DATE)                    
UNION ALL
SELECT     CAST(Census AS DECIMAL(9,2)) AS TheSum, Period           
FROM         [MANUAL].SMARTSOLVE_CENSUS                 
WHERE     (Period >= DATEADD(mm, DATEDIFF(mm, 0, @THE_DATE) - 5, 0) AND Period <= @THE_DATE) AND (PL In (Select distinct * FROM #tbl_PL))
))A

3 个答案:

答案 0 :(得分:2)

正如Joel和Lamak指出的那样,你有一个额外的东西)如果你使用类似Instant SQL formatter的东西,这实际上很容易找到。这会给出错误

)(11,3) expected token:Unknown
)(11,3) expected token: 

它还将SQL格式化如下,这使得它更容易理解。

SELECT @ALL_TOTAL = SUM(thesum) 
FROM   (SELECT CAST(reliability.census.census AS DECIMAL(9, 2)) AS thesum 
        FROM   reliability.census 
        WHERE  reliability.census.pl IN (SELECT DISTINCT * 
                                         FROM   #tbl_pl) 
               AND ( CONVERT(DATETIME, LEFT(CONVERT(CHAR, period), 4) + '-' + 
                     RIGHT( 
                     period, 2) 
                     + 
                     '-1' 
                     , 102) >= Dateadd(mm, Datediff(mm, 0, @THE_DATE) - 5, 0) ) 
               AND ( CONVERT(DATETIME, LEFT(CONVERT(CHAR, period), 4)+ '-' + 
                     RIGHT( 
                     period, 2) + 
                     '-1', 
                     102) <= @THE_DATE ) 
        UNION ALL 
        SELECT CAST(census AS DECIMAL(9, 2)) AS thesum, 
               period 
        FROM   [MANUAL].smartsolve_census 
        WHERE  ( period >= Dateadd(mm, Datediff(mm, 0, @THE_DATE) - 5, 0) 
                 AND period <= @THE_DATE ) 
               AND ( pl IN (SELECT DISTINCT * 
                            FROM   #tbl_pl) ))a 

答案 1 :(得分:1)

听起来你的括号不匹配,仔细检查并确保它们都正确排列。在最后一个select语句的末尾,你还有一个“)”,而不是你打开过的那个。这是很多筑巢!确保一切都符合您的预期,否则如果您的UNION应用于错误的级别,您可能会得到意想不到的结果。

答案 2 :(得分:1)

如果我正确阅读您的查询,您有两个错误。第一个是最后一行的额外“)”,它应该是:

WHERE     (Period >= DATEADD(mm, DATEDIFF(mm, 0, @THE_DATE) - 5, 0) AND Period <= @THE_DATE) AND (PL In (Select distinct * FROM #tbl_PL))
) A

而且,我认为在UNION ALL之后您的查询中还有更多列,您选择的是CAST(Census AS DECIMAL(9,2)) AS TheSum, Period,而在第一个列中您只选择了一列:CAST(RELIABILITY.Census.Census AS DECIMAL(9,2)) AS TheSum