我感觉到你的痛苦。我经常在 ETL 例程中执行一次性类型清理步骤。当您需要从导入中删除一些奇怪的东西(流氓分页符、空格等)时,我发现这样的脚本会有所帮助:
declare @tableName nvarchar(100) = 'dbo.YourTable';
declare @col nvarchar(max);
-- remove quotes and trim every column, kill page breaks, etc.
;with c_Col (colName)
as ( select c.name
from sys.tables t
join sys.columns c on
c.object_id = t.object_id
where t.object_id = object_id(@tableName)
)
select @col = stuff(a.n, 1, 1, '')
from ( select top 100 percent
',' + c.colName + '= nullif(replace(replace(replace(rtrim(ltrim('+c.colName+ ')), ''"'', ''''), char(13), ''''), char(10), ''''), '''') '
from c_col c
for xml path('')
) as a(n)
declare @cmd nvarchar(max)
set @cmd = 'update ' + @tableName + ' set ' + @col
print @cmd;
--exec(@cmd);