0

我有一个存储过程,它正在获取表的列详细信息。说这个表名是发票表。我有另一个表 Invoice_Staging 表,其中包含 varchar 格式的数据。现在我希望从 Invoice_staging 中获取数据,根据 Invoice 表中提到的数据类型转换值,然后批量插入 Invoice 表。现在,碰巧有一个值无法进行 Casting,在这种情况下,我不希望批量插入失败。而是将该特定字段设为 NULL 并将其插入 Invoice 表中。我坚持获取数据并根据需要进行转换。请告诉我一个方法,它可以解决我的问题。存储过程如下 -

create PROCEDURE usp_stagingToProduction
@staging_tableName varchar(200),
@master_tableName varchar(200)  

AS BEGIN SET NOCOUNT ON;

Declare @count int, -- for counting the total number of records in a table 
@RowCount int,
@columnName varchar(200),
@columnDataTypeName varchar(200),
@maxLength int,
@precision int,
@scale int,
@isNullable int,
@primaryKey int,
@sql varchar(5000)
Set @sql = ''

    create table #MyTemporaryTable
    (
        ColumnName varchar(500),
        DataType varchar(100),
        MaxLength int,
        [Precision] int,
        Scale int,
        IsNullable bit,
        PrimaryKey bit,
        flag bit default 0,
        id int identity(1,1)
    )

    insert into #MyTemporaryTable
    SELECT 
            c.name 'ColumnName',
            t.Name 'DataType',
            c.max_length  'MaxLength',
            c.precision 'Precision',
            c.scale 'Scale',
            c.is_nullable 'IsNullable',
            ISNULL(i.is_primary_key, 0) 'PrimaryKey',
            0
        FROM    
            sys.columns c
        INNER JOIN 
            sys.types t ON c.user_type_id = t.user_type_id
        LEFT OUTER JOIN 
            sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
        LEFT OUTER JOIN 
            sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
        WHERE
            c.object_id = OBJECT_ID('Invoice')

        -- select * from #MyTemporaryTable  -- To display the content of temporary table


        declare @column varchar(500)
        declare @datatype varchar(500)
        declare @id int
        Set @sql = @sql + 'Insert Into ' + @master_tableName
        Set @sql = @sql + ' Select '
        while exists (select Top 1 * from #MyTemporaryTable where flag  = 0)
        BEGIN
            Select @id = id, @datatype = DataType, @column = ColumnName from #MyTemporaryTable Where flag = 0
            Set @sql = @sql + ' Cast(' + @column + ' As ' + @datatype + '), ' -- building the cast query


            update #MyTemporaryTable
            set flag = 1
            where id = @id

            -- Select @sql -- to see whats happing
        END
        Select @sql = SubString(@sql, 1, LEN(@sql) - 1)
        Set @sql = @sql + ' From' +' ' + @staging_tableName  -- #MyTemporaryTable'

        Select @sql -- just to display query. nothing to do anything with the logic 

        -------------------
        select @count = count(*) from @staging_tableName -- getting column count from staging table
        declare @counter as int
        set @counter = 1
        if(@count >0)
             -- data found in staging table
             -- logic here for casting and inserting the data to the table

        else

结束

--usp_usp_stagingToProduction '发票'

4

0 回答 0