string - SQL Server将表内容转换为合并语句?

标签 string sql-server-2008 export

我有一个在“位置A”中已更改的查找数据表,我需要在“位置B”中更新相同的命名表。我在想,如果我有一种方法可以将表(80k行)逐行读取为格式正确的字符串,并创建带有合并语句的输出文件(带有“将表转换为CTE”),那么我可能会有一些滑溜溜的东西。

对此构想的所有构想(或已经做过类似工作的人的指点)都将受到赞赏。谢谢。我正在使用SQL Server Express 2012。

最佳答案

我使用此帮助程序脚本来管理环境之间的小型查找表。 80k行无论如何都不算​​巨大,但不是无关紧要的。康拉德(Conrad)为移动大量数据制定了坚实的计划。

如果您确实选择走生成的合并路线,则可以帮助您:

declare @TableName nvarchar(128) = 'dbo.YourTable';

--// do not modify
set nocount on; 

if  (   isnull(parsename(@TableName, 1), '') = '' or
        isnull(parsename(@TableName, 2), '') = ''
    )
begin
    raiserror('@TableName should be in form of owner.object (dbo.Yak). Cannot continue.', 16, 1, @TableName);
    return;
end
if object_id(@TableName) is null
begin
    raiserror('Table [%s] not found. Cannot continue.', 16, 1, @TableName);
    return;
end
if objectproperty(object_id(@TableName), N'TableHasPrimaryKey') = 0
begin
    raiserror('Table [%s] has no primary key defined. Cannot continue.', 16, 1, @TableName);
    return;
end

declare @output varchar(max)='', @return int, @sql varchar(max) = '', @list varchar(max) = '', @exec nvarchar(max), @Cols varchar(max)='',
        @sCols varchar(max)='', @aCols varchar(max)='', @pCols varchar(max)='', @uCols varchar(max)='', 
        @b char(1) = char(13), @t char(1) = char(9);

select  @exec = isnull(@exec + '+' + @b,'') + ''','' +' +
        case 
            when c.data_type in ('uniqueidentifier') 
                then @t + '''''+isnull('''''''' + convert(varchar(50),' + c.column_name + ') + '''''''',''null'')'
            when c.data_type in ('xml') 
                then @t + '''''+isnull('''''''' + convert(varchar(max),' + c.column_name + ') + '''''''',''null'')'
            when c.data_type in ('char', 'nchar', 'varchar', 'nvarchar', 'sysname') 
                then @t + '''''+isnull('''''''' + replace(' + c.column_name + ','''''''','''''''''''') + '''''''',''null'')'
            when c.data_type in ('datetime', 'date') 
                then @t + '''''+isnull('''''''' + convert(varchar,' + c.column_name + ',121)+'''''''',''null'') '
            when c.data_type in ('tinyint', 'int', 'float', 'numeric', 'decimal', 'smallmoney', 'money', 'bit', 'smallint', 'real', 'bigint') 
                then @t + '''''+isnull(convert(varchar,' + c.column_name + '),''null'')' 
            else ''' ** data type [' + c.data_type + '] not supported **'''
        end,
        @Cols = @Cols + ','+quotename(c.column_name, '['),
        @sCols = @sCols + ',s.'+quotename(c.column_name, '[')
from    information_schema.columns c
where   c.table_name = parsename(@TableName, 1) and
        c.table_schema = parsename(@TableName, 2)
order 
by      c.ordinal_position

-- stage primary key columns
declare @pks table (c varchar(100), o int);
insert into @pks (c,o)
    select  kcu.column_name, kcu.ordinal_position
    from    information_schema.table_constraints tc
    join    information_schema.key_column_usage kcu on
            tc.table_name = kcu.table_name and
            tc.constraint_name = kcu.constraint_name
    where   tc.table_name = parsename(@TableName, 1) and
            tc.table_schema = parsename(@TableName, 2) and
            tc.constraint_type = 'PRIMARY KEY';

-- build primary key columns (1=1 to ease support of composite PKs)
set @pCols = '1=1'           
select  @pCols = @pCols + ' and '+isnull('t.'+quotename(c, '[') + ' = ' + 's.'+quotename(c, '['), '')
from    @pks    
order
by      o;

-- build update columns, do not update identities or pks
select  @aCols = @aCols + ','+quotename(c.[name], '[') + ' = s.' + quotename(c.[name], '[')
from    sys.columns c
where   object_id = object_id(@TableName) and
        [name] not in (select c from @pks) and
        columnproperty(object_id(@TableName), c.[name], 'IsIdentity ') = 0;

-- script the data out as table value constructors
select  @exec = 'set nocount on; Select ' + @b + '''(' + ''' + ' + @b + stuff(@exec,1, 3, '') + '+'')''' + @b + 'from ' + @TableName,
        @Cols = stuff(@Cols,1, 1, ''),
        @sCols = stuff(@sCols,1, 1, ''),
        @aCols = stuff(@aCols,1, 1, '');

declare @tab table (val varchar(max));
declare @Values varchar(max);
insert into @tab
    exec(@exec);

if not exists(select 1 from @tab)
begin
    raiserror('Table %s is valid but empty. Populate it before running this helper.', 16, 1, @TableName);
    return
end

select @Values = stuff(cast((select ','+ @b + val from @tab for xml path('')) as xml).value('.', 'varchar(max)'),1,2,'');

-- build the merge statement
set @output +=  @b+'--'+@TableName+replicate('-', 98-len(@TableName))+@b+replicate('-', 100)+@b;
set @output +=  'set nocount on;'+@b
if objectproperty(object_id(@TableName), 'TableHasIdentity') = 1 
    set @output += 'set identity_insert ['+parsename(@TableName, 2)+'].[' + parsename(@TableName, 1) + '] on;'+@b;
set @output +=  ';with cte_data('+@Cols+')'+@b+'as (select * from (values'+@b+'--'+replicate('/', 98) + @b +@Values+ @b +'--'+replicate('\', 98)+ @b +')c('+@Cols+'))'+@b;
set @output +=  'merge' + @t + '['+parsename(@TableName, 2)+'].[' + parsename(@TableName, 1) + '] as t' + @b + 'using' + @t + 'cte_data as s'+@b;
set @output +=  'on' + replicate(@t, 2) + @pCols+@b;
set @output +=  'when matched then' + @b+@t + 'update set'+ @b+@t + @aCols+@b;
set @output +=  'when not matched by target then'+@b;
set @output +=  @t+'insert(' + @Cols + ')'+@b;
set @output +=  @t+'values(' + @sCols + ')'+@b;
set @output +=  'when not matched by source then' + @b+@t+ 'delete;'+@b;
if objectproperty(object_id(@TableName), 'TableHasIdentity') = 1 
    set @output += 'set identity_insert ['+parsename(@TableName, 2)+'].[' + parsename(@TableName, 1) + '] off;'

--output the statement as xml (to overcome mgmt studio limitations)
select s as [output] from (select @output)d(s) for xml path('');
return;

关于string - SQL Server将表内容转换为合并语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10489184/

相关文章:

c# - 如何在 C# 中复制数组的值 n 次

SQL Server 列的大小差异

excel - PostgreSQL 查询输出为 Excel 文件

jquery - 使用服务器端处理datatable jquery导出表的所有数据

导入 "not exported"类型时出现 typescript 错误,即使它已导出

C:根据文件名创建新的文件扩展名

java - 如何将数字、空格和逗号组成的字符串更改为整数数组列表

java - 为什么 Java 的 String#replace() 方法对字符和字符串的处理方式不同?

sql-server-2008 - 将 INT 转换为 VARCHAR

sql-server - 将 xml 字符串参数传递给 SQL Server 存储过程