如何修改游标内的临时表?

时间:2018-05-02 13:37:05

标签: sql-server tsql database-cursor

我在sql中有临时表数据。我需要修改该表中的数据。

请附上快照 Bad habits : Putting NOLOCK everywhere

我需要一个包含月份数据的col,例如01-03 jan-mar 这是我写的代码。这不起作用:

DECLARE @month VARCHAR(50) -- month
DECLARE @year VARCHAR(256) -- year
DECLARE @monthf VARCHAR(256) -- year
DECLARE @months VARCHAR(256) -- year

DECLARE db_cursor CURSOR FOR 
SELECT months,[year] FROM #temp where len(months)>4

OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @month,@year  

WHILE @@FETCH_STATUS = 0  
BEGIN  

    IF SUBSTRING(@month,0,3) = '01'
    set @monthf='Jan'      
    ELSE IF SUBSTRING(@month,0,3) = '02'
    set @monthf='Feb'
    ELSE IF SUBSTRING(@month,0,3) = '03'
    set @monthf='Mar'
    ELSE IF SUBSTRING(@month,0,3) = '04'
    set @monthf='Apr'
    ELSE IF SUBSTRING(@month,0,3) = '05'
    set @monthf='May'
    ELSE IF SUBSTRING(@month,0,3) = '06'
    set @monthf='Jun'
    ELSE IF SUBSTRING(@month,0,3) = '07'
    set @monthf='Jul'
    ELSE IF SUBSTRING(@month,0,3) = '08'
    set @monthf='Aug'
    ELSE IF SUBSTRING(@month,0,3) = '09'
    set @monthf='Sep'
    ELSE IF SUBSTRING(@month,0,3) = '10'
    set @monthf='Oct'
    ELSE IF SUBSTRING(@month,0,3) = '11'
    set @monthf='Nov'
    ELSE IF SUBSTRING(@month,0,3) = '12'
    set @monthf='Dec'

    IF SUBSTRING(@month,4,2) = '01'
    set @months='Jan'      
    ELSE IF SUBSTRING(@month,4,2) = '02'
    set @months='Feb'
    ELSE IF SUBSTRING(@month,4,2) = '03'
    set @months='Mar'
    ELSE IF SUBSTRING(@month,4,2) = '04'
    set @months='Apr'
    ELSE IF SUBSTRING(@month,4,2) = '05'
    set @months='May'
    ELSE IF SUBSTRING(@month,4,2) = '06'
    set @months='Jun'
    ELSE IF SUBSTRING(@month,4,2) = '07'
    set @months='Jul'
    ELSE IF SUBSTRING(@month,4,2) = '08'
    set @months='Aug'
    ELSE IF SUBSTRING(@month,4,2) = '09'
    set @months='Sep'
    ELSE IF SUBSTRING(@month,4,2) = '10'
    set @months='Oct'
    ELSE IF SUBSTRING(@month,4,2) = '11'
    set @months='Nov'
    ELSE IF SUBSTRING(@month,4,2) = '12'
    set @months='Dec'


      update #temp set [Month]= where months=

      Delete from #temp where months=SUBSTRING(@month,0,3) and [Year]=@year
      Delete from #temp where months=SUBSTRING(@month,4,2) and [Year]=@year  


      FETCH NEXT FROM db_cursor INTO @month,@year   
END 

CLOSE db_cursor  
DEALLOCATE db_cursor

我需要另一个包含月份名称的列:对于01-03,它应该给jan-mar 你能帮帮我吗?我需要一个单独的列用于月份字符串数据。

2 个答案:

答案 0 :(得分:4)

根本不需要光标。

IF OBJECT_ID('tempdb..#Data') IS NOT NULL
    DROP TABLE #Data

CREATE TABLE #Data (
    Month VARCHAR(100),
    ResultMonthText VARCHAR(100))

INSERT INTO #Data (
    Month)
VALUES
    ('01-03'),
    ('07'),
    ('03'),
    ('03-05'),
    ('05-09'),
    ('02')

;WITH ParsedMonths AS
(
    SELECT
        D.Month,
        D.ResultMonthText,
        First = CONVERT(INT, SUBSTRING(D.Month, 1, 2)),
        Second = CONVERT(INT, NULLIF(SUBSTRING(D.Month, 4, 2), ''))
    FROM
        #Data AS D
),
MonthNames AS
(
    SELECT
        D.Month,
        D.ResultMonthText,
        FirstAsName = SUBSTRING(
            DATENAME(
                MONTH, 
                DATEADD(MONTH, D.First, 0) - 1 ),
            1,
            3),
        SecondAsName = SUBSTRING(
            DATENAME(
                MONTH, 
                DATEADD(MONTH, D.Second, 0) - 1 ),
            1,
            3)
    FROM
        ParsedMonths AS D
)
UPDATE D SET
    ResultMonthText = D.FirstAsName + ISNULL('-' + D.SecondAsName, '')
FROM
    MonthNames AS D



SELECT
    *
FROM
    #Data AS D

/*
Result:

    Month   ResultMonthText
    ------- ------------
    01-03   Jan-Mar
    07      Jul
    03      Mar
    03-05   Mar-May
    05-09   May-Sep
    02      Feb

*/

编辑:如果您仍想保留光标,请执行此操作(尽管我强烈建议您尽可能停止使用它们):

OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @month,@year  

WHILE @@FETCH_STATUS = 0  
BEGIN 

    DECLARE @FirstMonthNumber INT = CONVERT(INT, SUBSTRING(@month, 1, 2))
    DECLARE @SecondMonthNumber INT = CONVERT(INT, NULLIF(SUBSTRING(@month, 4, 2), ''))

    DECLARE @FirstMonthText VARCHAR(10) = SUBSTRING(
        DATENAME(
            MONTH, 
            DATEADD(MONTH, @FirstMonthNumber, 0) - 1 ),
        1,
        3)

    DECLARE @SecondMonthText VARCHAR(10) = SUBSTRING(
        DATENAME(
            MONTH, 
            DATEADD(MONTH, @SecondMonthNumber, 0) - 1 ),
        1,
        3)

    update #temp set 
        YourNewMonthColumn = @FirstMonthText + ISNULL('-' + @SecondMonthText, '')
    WHERE
        months = @month

    FETCH NEXT FROM db_cursor INTO @month,@year

END

我不知道为什么要更新列,然后也删除该行。

答案 1 :(得分:0)

也许

grid.Column(columnName: "Name", format: (item) => Html.ActionLink("ActionRoute", "Edit Name", new { Name = item.Name })))