我想在SSIS脚本任务中使用此游标,使用C#直到解析所有记录

时间:2014-09-15 21:56:13

标签: c# sql sql-server ssis

DECLARE @Line1 VARCHAR (MAX) 
DECLARE @Con_Line1 VARCHAR (MAX) 
DECLARE Pointer CURSOR FOR 
  SELECT TOP 13 Lines 
  FROM   dbo.Stage_Trdrev_TD 

OPEN Pointer 

FETCH NEXT FROM Pointer INTO @Line1 

WHILE @@FETCH_STATUS = 0 
  BEGIN 
      SET @Con_Line1 = ISNULL(@Con_Line1, ''+'01') 
                       + ISNULL (SUBSTRING(@Line1, 3, 101) + ' ', '') 

      FETCH NEXT FROM Pointer INTO @Line1 
  END 

SELECT LEFT(@Con_Line1, LEN(@Con_Line1) - 1) AS [Line] 

INSERT INTO Parse_Date_TD 
VALUES      (@Con_Line1) 

CLOSE Pointer 

DEALLOCATE Pointer

WITH D 
     AS (SELECT TOP 13 * 
         FROM   dbo.[Stage_Trdrev_TD]) 
DELETE FROM D 

我想使用此游标来解析一个只有一列而一条记录由13行组成的表中的记录。我使用上面的光标手动,但现在我希望它从ssis脚本任务运行。直到解析所有记录。

谢谢...

1 个答案:

答案 0 :(得分:0)

没有理由将其变成脚本(任务或组件)

你正在做什么?您正在从表中删除前13行并将结果连接在一起以推入另一个表。这已经被SO所覆盖。

将执行SQL任务添加到SSIS包中。

这是我的桌面设置

CREATE TABLE 
    dbo.Stage_Trdrev_TD
(
    Lines varchar(100)
);

CREATE TABLE 
    dbo.Parse_Date_TD 
(
    Lines varchar(8000)
);

INSERT INTO 
    dbo.Stage_Trdrev_TD
SELECT
    V.name
FROM
    master.dbo.spt_values AS V;

这是实际工作。我创建一个临时表来保存我删除的行。那些被删除的行被连接成一行,然后我将其推入已解析的表中。

------------------
-- Now the "magic"
-------------

DECLARE
    @tbl TABLE
(
    lines varchar(100)
);

-- Tops without order is nuts
DELETE TOP (13) S
OUTPUT 
    DELETED.lines
INTO
    @tbl
FROM 
    dbo.Stage_Trdrev_TD AS S

-- Show the data before
SELECT * 
FROM @tbl T


-- Show what's about to go in
SELECT DISTINCT 
    STUFF
    (
        (
            SELECT 
                ',' + T.lines
            FROM 
                @tbl Ti
            FOR XML PATH('')
        )
        ,1
        ,1
        ,''
    ) AS ConcatenatedLines

FROM 
    @tbl T

-- Actually put it in

INSERT INTO
    dbo.Parse_Date_TD
SELECT DISTINCT 
    STUFF
    (
        (
            SELECT 
                ',' + T.lines
            FROM 
                @tbl Ti
            FOR XML PATH('')
        )
        ,1
        ,1
        ,''
    ) AS ConcatenatedLines

FROM 
    @tbl T;

请注意,如果源数据中包含NULL,则会导致结果不佳。