SQL Server游标问题

时间:2016-06-06 16:53:59

标签: sql sql-server cursor

我正在尝试创建一个sql server游标,将表CUT_CALENDAR中的日历放入新表SD_CALENDAR。

CUT_CALENDAR创建脚本:

CREATE TABLE [CUT_CALENDAR] (
ID NUMERIC(20) CONSTRAINT [PK_6Y45HHQDPSJSQEUOTN2GNDWXOY] PRIMARY KEY,
CALENDAR_TYP_ID NUMERIC(5) NOT NULL,
NAME NVARCHAR(35),
DESCRIPTION NVARCHAR(255),
PTY_ID NUMERIC(20) CONSTRAINT DF_3ISO3MPIGW2L7OIMXOJNNLPWUQ DEFAULT 1 NOT NULL,
LOCK_CODE NVARCHAR(20),
DATA_OWNER_ID NUMERIC(20) CONSTRAINT DF_3Q2WTFG4G6FXGXOHXZAKFFAI3I DEFAULT 1 NOT NULL,
CNT_ID NUMERIC(20) CONSTRAINT DF_IVFT64KE3N57TMX52OZIPU3AMI DEFAULT 1 NOT NULL,
NON_BUSINESS_DAYS_MONDAY CHAR(1) CONSTRAINT DF_JTFRF3HVMRYIG4AWQCZJTQTMIU DEFAULT 0,
NON_BUSINESS_DAYS_TUESDAY CHAR(1) CONSTRAINT DF_GPI2Z6AK6WSE2CVKMCMV46JYXY DEFAULT 0,
NON_BUSINESS_DAYS_WEDNESDAY CHAR(1) CONSTRAINT DF_GA57HDUUJTNEDHP2CJ6XGB53BE DEFAULT 0,
NON_BUSINESS_DAYS_THURSDAY CHAR(1) CONSTRAINT DF_HI7HAKZWNF2VEMWPNAUVTKZM34 DEFAULT 0,
NON_BUSINESS_DAYS_FRIDAY CHAR(1) CONSTRAINT DF_UQGTSAY72KC6U545QIPIKR7ZTA DEFAULT 0,
NON_BUSINESS_DAYS_SATURDAY CHAR(1) CONSTRAINT DF_2GRDDH5JUW6RMLDRY65IVOD7PY DEFAULT 0,
NON_BUSINESS_DAYS_SUNDAY CHAR(1) CONSTRAINT DF_XU3OGD5YNGERJBCXH5SWWBN4ZY DEFAULT 0,
CCY_ID NUMERIC(20) CONSTRAINT DF_HUNPATKMCI5STSEHPOIBRKWI3I DEFAULT 1 NOT NULL,
CODE NVARCHAR(30) NOT NULL,
VERSION NUMERIC(10),
SEAL VARCHAR(255),
LST_UPD_TS DATETIME
)
GO

SD_Calendar创建脚本:

CREATE TABLE [SD_CALENDAR] (
ID NUMERIC(20) CONSTRAINT [PK_27JF3DGCVK2RVHT7HCM6SV7RYU] PRIMARY KEY,
NAME NVARCHAR(35) NOT NULL,
CALENDAR_ROLE_ID NUMERIC(5) NOT NULL,
DESCRIPTION NVARCHAR(255),
USE_IN_CUTOFF CHAR(1) CONSTRAINT DF_J44DWSAWFNQ3632RMRKFZVBPO4 DEFAULT 0,
PTY_ID NUMERIC(20) CONSTRAINT DF_DPWNK4DJMHUXP4B2J3LJ6ZMOII DEFAULT 1 NOT NULL,
LOCK_CODE NVARCHAR(20),
DATA_OWNER_ID NUMERIC(20) CONSTRAINT DF_VEW2ORACEXZLCJGHJES4GHB5JE DEFAULT 1 NOT NULL,
CNT_ID NUMERIC(20) CONSTRAINT DF_3FQIIHS73J5JNIFVS42JIFK5BQ DEFAULT 1 NOT NULL,
NON_BUSINESS_DAYS_MONDAY CHAR(1) CONSTRAINT DF_LYWFX7W3YQ64UZJELPLK7AEXEQ DEFAULT 0,
NON_BUSINESS_DAYS_TUESDAY CHAR(1) CONSTRAINT DF_XYATRHGVU4QR76WZEYZUGIBCGI DEFAULT 0,
NON_BUSINESS_DAYS_WEDNESDAY CHAR(1) CONSTRAINT DF_7DMDZX637PDCR62XEO4K33ZIUM DEFAULT 0,
NON_BUSINESS_DAYS_THURSDAY CHAR(1) CONSTRAINT DF_VXM4YLGFTBKDQBMETXRX5AN2O4 DEFAULT 0,
NON_BUSINESS_DAYS_FRIDAY CHAR(1) CONSTRAINT DF_ROJIGQYMBG6IS7APUNSG4ZJR3A DEFAULT 0,
NON_BUSINESS_DAYS_SATURDAY CHAR(1) CONSTRAINT DF_GTCST5LNRKFAEKKZ6GR425KIFQ DEFAULT 0,
NON_BUSINESS_DAYS_SUNDAY CHAR(1) CONSTRAINT DF_4XRBQ6OBNO5YC4HTALGOC3O42M DEFAULT 0,
CCY_ID NUMERIC(20) CONSTRAINT DF_ANDQ75FQTG2OMYZTZTB4MBTXFQ DEFAULT 1 NOT NULL,
CODE NVARCHAR(30) NOT NULL,
VERSION NUMERIC(10),
SEAL VARCHAR(255),
LST_UPD_TS DATETIME
)
GO

我有以下脚本:

 DECLARE @sdCalendarId NUMERIC(20)
    DECLARE @calendarTypId NUMERIC(5)
    DECLARE @name NVARCHAR(35)
    DECLARE @description NVARCHAR(255)
    DECLARE @ptyId NUMERIC(20)
    DECLARE @lockCode NVARCHAR(20)
    DECLARE @dataOwnerId NUMERIC(20)
    DECLARE @cntId NUMERIC(20)
    DECLARE @nonBusinessDaysMonday CHAR(1)
    DECLARE @nonBusinessDaysTuesday CHAR(1)
    DECLARE @nonBusinessDaysWednesday CHAR(1)
    DECLARE @nonBusinessDaysThursday CHAR(1)
    DECLARE @nonBusinessDaysFriday CHAR(1)
    DECLARE @nonBusinessDaysSaturday CHAR(1)
    DECLARE @nonBusinessDaysSunday CHAR(1)
    DECLARE @ccyId NUMERIC(20)
    DECLARE @code NVARCHAR(30)
    DECLARE @version NUMERIC(10)
    DECLARE @seal VARCHAR(255)
    DECLARE @lstUpdTs DATETIME

    DECLARE cursorCutoffCalendar CURSOR FOR
      SELECT ID, NAME, CALENDAR_TYP_ID,DESCRIPTION,'Y',PTY_ID,LOCK_CODE,DATA_OWNER_ID,CNT_ID,NON_BUSINESS_DAYS_MONDAY,NON_BUSINESS_DAYS_TUESDAY,NON_BUSINESS_DAYS_WEDNESDAY,NON_BUSINESS_DAYS_THURSDAY,NON_BUSINESS_DAYS_FRIDAY,NON_BUSINESS_DAYS_SATURDAY,NON_BUSINESS_DAYS_SUNDAY,CCY_ID,CODE,VERSION,SEAL,LST_UPD_TS
      FROM CUT_CALENDAR
      WHERE ID != 1

    BEGIN
        SELECT @sdCalendarId = COALESCE(MAX(ID),1) FROM SD_CALENDAR

        OPEN cursorCutoffCalendar
            FETCH NEXT FROM cursorCutoffCalendar INTO @sdCalendarId, @calendarTypId, @name, @description, @ptyId, @lockCode, @dataOwnerId, @cntId, @nonBusinessDaysMonday, @nonBusinessDaysTuesday, @nonBusinessDaysWednesday, @nonBusinessDaysThursday, @nonBusinessDaysFriday, @nonBusinessDaysSaturday, @nonBusinessDaysSunday, @nonBusinessDaysMonday, @ccyId, @code, @version, @seal, @lstUpdTs
            WHILE @@FETCH_STATUS = 0
        BEGIN
            SET @sdCalendarId = @sdCalendarId + 1

                INSERT INTO SD_CALENDAR (ID, NAME, CALENDAR_ROLE_ID,DESCRIPTION,USE_IN_CUTOFF,PTY_ID,LOCK_CODE,DATA_OWNER_ID,CNT_ID,NON_BUSINESS_DAYS_MONDAY,NON_BUSINESS_DAYS_TUESDAY,NON_BUSINESS_DAYS_WEDNESDAY,NON_BUSINESS_DAYS_THURSDAY,NON_BUSINESS_DAYS_FRIDAY,NON_BUSINESS_DAYS_SATURDAY,NON_BUSINESS_DAYS_SUNDAY,CCY_ID,CODE,VERSION,SEAL,LST_UPD_TS)
                VALUES @sdCalendarId, @name, @calendarTypId,@description,'Y',@ptyId,@lockCode,@dataOwnerId,@cntId,@nonBusinessDaysMonday,@nonBusinessDaysTuesday,@nonBusinessDaysWednesday,@nonBusinessDaysThursday,@nonBusinessDaysFriday,@nonBusinessDaysSaturday,@nonBusinessDaysSunday,@ccyId,@code,@version,@seal,@lstUpdTs
            END

    FETCH NEXT FROM cursorCutoffCalendar INTO @sdCalendarId, @calendarTypId, @name, @description, @ptyId, @lockCode, @dataOwnerId, @cntId, @nonBusinessDaysMonday, @nonBusinessDaysTuesday, @nonBusinessDaysWednesday, @nonBusinessDaysThursday, @nonBusinessDaysFriday, @nonBusinessDaysSaturday, @nonBusinessDaysSunday, @nonBusinessDaysMonday, @ccyId, @code, @version, @seal, @lstUpdTs
    CLOSE cursorCutoffCalendar
    DEALLOCATE cursorCutoffCalendar

    GO

但是当我运行这个脚本时,我得到以下错误:

  

'@sdCalendarId'附近的语法不正确

我没有得到进一步的堆栈跟踪,所以我没有看到有什么问题。我对Sql server中的游标很新。谁能告诉我我做错了什么?

2 个答案:

答案 0 :(得分:0)

这是光标方法的更好替代方案。这将是此所需的全部代码。

INSERT INTO SD_CALENDAR 
(
    ID
    , NAME
    , CALENDAR_ROLE_ID
    , DESCRIPTION
    , USE_IN_CUTOFF
    , PTY_ID
    , LOCK_CODE
    , DATA_OWNER_ID
    , CNT_ID
    , NON_BUSINESS_DAYS_MONDAY
    , NON_BUSINESS_DAYS_TUESDAY
    , NON_BUSINESS_DAYS_WEDNESDAY
    , NON_BUSINESS_DAYS_THURSDAY
    , NON_BUSINESS_DAYS_FRIDAY
    , NON_BUSINESS_DAYS_SATURDAY
    , NON_BUSINESS_DAYS_SUNDAY
    , CCY_ID
    , CODE
    , VERSION
    , SEAL
    , LST_UPD_TS
)
SELECT (SELECT COALESCE(MAX(ID), 1) FROM SD_CALENDAR) + ROW_NUMBER() OVER (ORDER BY ID /*or whatever column(s) you want*/) as RowNum
    , NAME
    , CALENDAR_TYP_ID
    , DESCRIPTION
    ,'Y'
    , PTY_ID,LOCK_CODE
    , DATA_OWNER_ID
    , CNT_ID
    , NON_BUSINESS_DAYS_MONDAY
    , NON_BUSINESS_DAYS_TUESDAY
    , NON_BUSINESS_DAYS_WEDNESDAY
    , NON_BUSINESS_DAYS_THURSDAY
    , NON_BUSINESS_DAYS_FRIDAY
    , NON_BUSINESS_DAYS_SATURDAY
    , NON_BUSINESS_DAYS_SUNDAY
    , CCY_ID
    , CODE
    , VERSION
    , SEAL
    , LST_UPD_TS
FROM CUT_CALENDAR
WHERE ID != 1

答案 1 :(得分:0)

您的光标构造不正确。

应该是这样的:

DECLARE csrName CURSOR FOR
    SELECT STATEMENT
OPEN csrName
FETCH csrName INTO @YourVariables
WHILE @@FETCH_STATUS = 0 
BEGIN
    --Do your stuff
    FETCH csrName INTO @YourVariables
END
CLOSE csrName
DEALLOCATE csrName