Busca cadena en SQL
Este pequeño código busca la cadena killwow1 dentro de una BBDD en SQL, muy bueno cuando hemos sufrido un SQLinjection y necesitamos hacer una limpieza.
declare @user varchar(255),
@table varchar(255),
@column varchar(255),
@sql nvarchar(2048),
@p nvarchar(255),
@n int;
declare c cursor for
select u.name theUser, o.name theTable, c.name theColumn
from sysobjects o
inner join syscolumns c
on o.id = c.id
inner join sysusers u
on o.uid = u.uid
where c.xtype in (167, 175, 231, 239)
and o.type = ‘u’;
open c;
fetch next from c into @user, @table, @column;
while (@@FETCH_STATUS = 0)
begin
select @sql = ‘select @n = count(*) from ‘ + @user + ‘.’ + @table + ‘ where ‘ + @column + ‘ like ”%killwow1%”’, @p = ‘@n int output’;
exec sp_executesql @sql, @p, @n = @n output;
if ((@n is not null) or (@n <> 0))
begin
print @user + ‘.’ + @table + ‘.’ + @column + ‘ = ‘ + cast(@n as varchar(10));
end;
fetch next from c into @user, @table, @column;
end;
close c;
deallocate c;