创建多个表,这些表由一个表组成,其中包含所有数据

时间:2016-07-07 19:28:40

标签: sql-server ssis

我有一张这样的表:

element code    name    seq active
assessmentCategory  1   Entry   NULL    True
assessmentCategory  2   On-Going    NULL    True
assessmentCategory  3   Exit    NULL    True
careerCluster   01  Agriculture NULL    True
careerCluster   02  Architecture and Construction   NULL    True
careerCluster   03  Arts and Communications NULL    True
careerCluster   04  Business and Management NULL    True
careerCluster   05  Education and Training  NULL    True
careerCluster   06  Finance NULL    True
careerCluster   07  Govermant and Public Administration NULL    True
careerCluster   08  Health Science  NULL    True
careerCluster   09  Hospitality and Tourism NULL    True
careerCluster   10  Human Service   NULL    True
careerCluster   11  Information Technology  NULL    True
careerCluster   12  Law and Public Safety   NULL    True
careerCluster   13  Manufacturing   NULL    True
careerCluster   14  Marketing, Sales and service    NULL    True
careerCluster   15  Science and Technology  NULL    True
careerCluster   16  Transportation and Logistics    NULL    True
cte FT  Full Time CTE Student   1   True
cte PT  Part Time CTE Student   2   True
disability1 05  Gifted without Disability (no longer reported)  NULL    False
disability1 2105    Gifted without Disability   0   False
disability1 2106    Gifted with Disability  0   False
disability1 2121    Autistic/Autism NULL    True
disability1 2122    Deaf-blindness  NULL    True
disability1 2123    Hearing impairment including deafness   NULL    True
disability1 2124    Intellectual Disability NULL    True
disability1 2125    Multiple disabilities   NULL    True
disability1 2126    Orthopedic impairment   NULL    True
disability1 2127    Emotional disturbance   NULL    True
disability1 2128    Specific learning disability    NULL    True
disability1 2129    Speech or language impairment   NULL    True
disability1 2130    Traumatic brain injury  NULL    True
disability1 2131    Visual impairment including blindness   NULL    True
disability1 2132    Other health impairment NULL    True
disability1 2134    Developmental delay NULL    True
disability1 2135    Infants and Toddlers with Disabilities  NULL    True
disability2 05  Gifted without Disability (no longer reported)  NULL    False
disability2 2105    Gifted without Disability   0   False

如何使用SSIS或TSQL创建以元素列中的唯一值命名的新表,并包含代码,名称,seq和活动列中的值?

2 个答案:

答案 0 :(得分:0)

DECLARE @element VARCHAR(25)

DECLARE ElementCursor CURSOR FOR
    SELECT DISTINCT
        Element 
    FROM
        dbo.[Validation]

OPEN ElementCursor

FETCH NEXT FROM ElementCursor
INTO @element

    WHILE @@FETCH_STATUS = 0
    BEGIN
       BEGIN TRY
          DECLARE @SQL NVARCHAR(MAX)
          SET @SQL = '
                IF OBJECT_ID(' + CHAR(39) + concat('v_',@ElementID) + CHAR(39) + ') IS NOT NULL 
                    BEGIN
                        DROP TABLE ' + concat('v_',@ElementID) + ' 
                    END

                CREATE TABLE '+ concat('v_',@ElementID) + ' (
                    Code VARCHAR(10) NULL
                    ,name VARCHAR(50) NULL
                    ,seq INT NULL
                    ,active BIT NULL
                )

             INSERT INTO ' + concat('v_',@ElementID) + ' (Code,name,seq,active)
             SELECT
                Code
                ,name
                ,seq
                ,active
             FROM
                dbo.[Validation]
             WHERE
                element = ' + CHAR(39) + @element + CHAR(39)

         PRINT @SQL
         EXECUTE (@SQL)

       END TRY
       BEGIN CATCH
          EXECUTE usp_GetErrorInfo;
       END CATCH

        FETCH NEXT FROM ElementCursor
        INTO @element
    END

CLOSE ElementCursor
DEALLOCATE ElementCursor

答案 1 :(得分:0)

我现在注释掉了递归位:

编辑:这是工作代码!感谢百万马特

我的代码

          USE [vPhiladelphia-CV1]
GO

CREATE PROCEDURE jmkParsingValidationTables AS


DECLARE @ElementID varchar(30)
DECLARE @value varchar(30)

DECLARE MY_CURSOR CURSOR FOR 

    SELECT DISTINCT [element]
    FROM [dbo].[Validation]

OPEN MY_CURSOR
    FETCH NEXT FROM MY_CURSOR 
    INTO @ElementID
    WHILE @@FETCH_STATUS = 0

    BEGIN TRY
        DECLARE @SQL NVARCHAR(MAX)
        SET @SQL = '
            IF OBJECT_ID(' + CHAR(39) + concat('v_',@ElementID) + CHAR(39) + ') IS NOT NULL
                BEGIN
                    DROP TABLE [' + concat('v_',@ElementID) + ']
                END

            CREATE TABLE [' + concat('v_',@ElementID) + '] (
                code VARCHAR(300) NULL
                ,name VARCHAR(300) NULL
                ,seq VARCHAR(300) NULL
                ,active VARCHAR(300) NULL
            )

            INSERT INTO [' + concat('v_',@ElementID) + '] (code,name,seq,active)
            SELECT
                code
                ,name
                ,seq
                ,active
            FROM
                [Validation]
            WHERE
                element = ' + CHAR(39) + @ElementID + CHAR(39) 

            PRINT @SQL  
            EXECUTE (@SQL)

            FETCH NEXT FROM MY_CURSOR INTO @ElementID

    END TRY

    BEGIN CATCH
        EXECUTE usp_GetErrorInfo;  
    END CATCH

CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR