Hi,
I maintain the back end of a dodgy 3rd party app that I can't get the source to.
It frequently has problems with delimiter characters in string fields- quotes, commas, etc. Since I can't add verification at the front end, I need to clean it at the back.
The problem isn't limited to one or two major fields/tables, so I've ruled out triggers (performance and maintenance would be too much of a nightmare). That leaves a database crawler that can be run overnight that cleans these characters up (some are removed, some are replaced by spaces).
I've written a proc that finds char fields, then finds and removes single quotes, double quotes and commas.It works, but it's slow (4 hours- char fields aren't indexed much) and multiple passes are needed (ie it only removes one instance of the character each time).
Here's my code. Anything come to mind?
----------------------------------
/*
This code crawls the database and replaces problem characters in tables with good ones.
@strSQLUpdate is set each time due to quirks with literal quotes and space trimming.
*/
declare @intcount int
Declare @strSQLUpdate varchar(250)
declare @dirtytable varchar(20)
declare @dirtyfield varchar(20)
declare @BadChar char(1)
declare @GoodChar char(1)
select @intcount = 1
While @intcount < 4
BEGIN
DECLARE curStringField CURSOR FOR
select o.name as chartable, c.name as charfield
from sysobjects o, syscolumns c
where o.type = 'U'
and c.id = o.id
and c.xtype = 175
order by o.name, c.name
OPEN curStringField
FETCH NEXT FROM curStringField into @dirtytable, @dirtyfield
WHILE @@FETCH_STATUS = 0
BEGIN
select @BadChar = case @intcount
when 1 then """"
when 2 then "'"
when 3 then ","
end
select @GoodChar = ""
select @strSQLUpdate = case @intcount
when 1 then "UPDATE " + @dirtytable + " SET " + @dirtyfield + " = left(" + @dirtyfield + ",charindex('" + @BadChar + "'," + @DirtyField + "
-1) + '" + ltrim(@Goodchar) + "' + substring(" + @dirtyfield + ",charindex('" + @Badchar + "'," + @dirtyfield + "
+1,len(" + @dirtyfield + "
) WHERE charindex('" + @BadChar + "'," + @DirtyField + "
<> 0"
when 2 then "UPDATE " + @dirtytable + " SET " + @dirtyfield + " = left(" + @dirtyfield + ",charindex(""" + @BadChar + """," + @DirtyField + "
-1) + '" + ltrim(@Goodchar) + "' + substring(" + @dirtyfield + ",charindex(""" + @Badchar + """," + @dirtyfield + "
+1,len(" + @dirtyfield + "
) WHERE charindex(""" + @BadChar + """," + @DirtyField + "
<> 0"
when 3 then "UPDATE " + @dirtytable + " SET " + @dirtyfield + " = left(" + @dirtyfield + ",charindex('" + @BadChar + "'," + @DirtyField + "
-1) + '" + @Goodchar + "' + substring(" + @dirtyfield + ",charindex('" + @Badchar + "'," + @dirtyfield + "
+1,len(" + @dirtyfield + "
) WHERE charindex('" + @BadChar + "'," + @DirtyField + "
<> 0"
end
EXECUTE (@strSQLUpdate)
FETCH NEXT FROM curStringField into @dirtytable, @dirtyfield
END
CLOSE curStringField
DEALLOCATE curStringField
select @intcount = @intcount + 1
END
---------------------------
I maintain the back end of a dodgy 3rd party app that I can't get the source to.
It frequently has problems with delimiter characters in string fields- quotes, commas, etc. Since I can't add verification at the front end, I need to clean it at the back.
The problem isn't limited to one or two major fields/tables, so I've ruled out triggers (performance and maintenance would be too much of a nightmare). That leaves a database crawler that can be run overnight that cleans these characters up (some are removed, some are replaced by spaces).
I've written a proc that finds char fields, then finds and removes single quotes, double quotes and commas.It works, but it's slow (4 hours- char fields aren't indexed much) and multiple passes are needed (ie it only removes one instance of the character each time).
Here's my code. Anything come to mind?
----------------------------------
/*
This code crawls the database and replaces problem characters in tables with good ones.
@strSQLUpdate is set each time due to quirks with literal quotes and space trimming.
*/
declare @intcount int
Declare @strSQLUpdate varchar(250)
declare @dirtytable varchar(20)
declare @dirtyfield varchar(20)
declare @BadChar char(1)
declare @GoodChar char(1)
select @intcount = 1
While @intcount < 4
BEGIN
DECLARE curStringField CURSOR FOR
select o.name as chartable, c.name as charfield
from sysobjects o, syscolumns c
where o.type = 'U'
and c.id = o.id
and c.xtype = 175
order by o.name, c.name
OPEN curStringField
FETCH NEXT FROM curStringField into @dirtytable, @dirtyfield
WHILE @@FETCH_STATUS = 0
BEGIN
select @BadChar = case @intcount
when 1 then """"
when 2 then "'"
when 3 then ","
end
select @GoodChar = ""
select @strSQLUpdate = case @intcount
when 1 then "UPDATE " + @dirtytable + " SET " + @dirtyfield + " = left(" + @dirtyfield + ",charindex('" + @BadChar + "'," + @DirtyField + "
when 2 then "UPDATE " + @dirtytable + " SET " + @dirtyfield + " = left(" + @dirtyfield + ",charindex(""" + @BadChar + """," + @DirtyField + "
when 3 then "UPDATE " + @dirtytable + " SET " + @dirtyfield + " = left(" + @dirtyfield + ",charindex('" + @BadChar + "'," + @DirtyField + "
end
EXECUTE (@strSQLUpdate)
FETCH NEXT FROM curStringField into @dirtytable, @dirtyfield
END
CLOSE curStringField
DEALLOCATE curStringField
select @intcount = @intcount + 1
END
---------------------------