I've created this query to filter possible sensible data in MySQL servers, now I have to do the same in SQL Server but I don't have the knowledge to do that.
Query:
select table_schema as 'Database', table_name as 'Table', column_name as 'Column', column_type as 'Type'
from information_schema.columns
where (column_name like '%cnpj%' or column_name like '%cpf%' or column_name like '%nom%' or column_name like '%fon%' or column_name like '%tel%' or column_name like '%mail%' or column_name like '%end%' or column_name like '%cel%' or column_name like '%gen%' or column_name like '%sex%' or column_name like '%elet%' or column_name like '%doc%' or column_name like '%mae%' or column_name like '%nasc%')
and (table_schema not in ('sys', 'information_schema', 'performance_schema', 'mysql'));
I've been doing some research to find the equivalent to information_schema
but no success. (example: sys.columns
, there's no information about schema or type there).
I'll be very grateful if anyone help me to convert this.
2条答案
按热度按时间1tu0hz3e1#
SQL Server has the
Information_Schema.Columns
view, but it has several differences from MySQL:sys.Columns
works the same way. SQL Server does not provide a way to show this for all databases. This was a deliberate choice (not an omission), and IMO the right choice, since there are security implications involved.This will get you part of the way there, but again: only for one database at a time:
The only option I've found to get around this is making a temp table, putting the query in a varchar variable as an INSERT/SELECT for the temp table with a
USE [?]
at the top, run it repeatedly usingsp_msforeachdb
, then select from the temp table:This takes advantage of a common pattern on SQL Server for skipping the system databases (
master
,model
,msdb
, andtempdb
), which seemed appropriate to the question.FWIW, this table-value constructor filter technique also works on MySQL. You have to prepend the
ROW
keyword for each entry, but it's still a lot shorter and more scalable than repeating theLIKE
expression over and over.t1qtbnec2#
There are couple of differencies for the query in SQL Server. In SQL Server the information_schema.columns contains info from the current database, not all databases. Secondly, the
column_type
as such does not exists. You need to combinedata_type
andcharacter_maximum_length
.