{"id":3980,"date":"2014-07-02T08:24:44","date_gmt":"2014-07-02T13:24:44","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=3980"},"modified":"2014-07-02T08:24:57","modified_gmt":"2014-07-02T13:24:57","slug":"crawling-all-sql-server-tables-and-columns-for-a-specific-value","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2014\/07\/02\/crawling-all-sql-server-tables-and-columns-for-a-specific-value\/","title":{"rendered":"Crawling all SQL Server tables and columns for a specific value"},"content":{"rendered":"<p>We had a quick need to search all columns in every table during a data discovery exercise.  This is by no means the most efficient, but it does work.<\/p>\n<p>It pulls 10,000 rows at a time from each table, and parses each row for all columns and checks the value for a particular string.<\/p>\n<p>It would be trivial to multi thread this by pushing the tables onto a stack and having multiple threads pop them off for processing.  The largest table would be the overall bottleneck.<\/p>\n<pre lang=\"java\">\r\nimport java.sql.*;\r\n\r\n\/\/c:\\>set classpath=c:\\sqljdbc4.jar;.\r\n\/\/c:\\>javac workedHours.java\r\n\/\/c:\\>java -Djava.library.path=\"C:\\Users\\showard\\Downloads\\sqljdbc_4.0\\enu\\auth\\x64\" workedHours\r\n\/\/create table sensordata (sitename varchar2(100),siteid varchar2(100),servername varchar2(100),serverdate timestamp,createdate timestamp,valuea number,valueb number) tablespace expanly_data;\r\n\r\npublic class wbTables {\r\n  public static void main (String args[]) throws Exception {\r\n    Connection conn = DriverManager.getConnection(\"jdbc:sqlserver:\/\/host:1433;databaseName=dbname;integratedSecurity=true;\");\r\n    String s = \"select quotename(schema_name(sobj.schema_id)) + '.' + quotename(sobj.name) as [tablename], sum(sdmvptns.row_count) as [rowcount] \" +\r\n                 \"from sys.objects as sobj inner join sys.dm_db_partition_stats as sdmvptns on sobj.object_id = sdmvptns.object_id \" +\r\n                 \"where sobj.type = 'u' and sobj.is_ms_shipped = 0x0 and sdmvptns.index_id < 2 \" +\r\n                 \"group by sobj.schema_id,sobj.name \" +\r\n                 \"having sum(sdmvptns.row_count) > 0\";\r\n    ResultSet rst = conn.createStatement().executeQuery(s);\r\n    while (rst.next()) {\r\n      Statement stm = conn.createStatement();\r\n      stm.setFetchSize(10000);\r\n      ResultSet rst2 = stm.executeQuery(\"select * from \" + rst.getString(1) + \" with (nolock)\");\r\n      ResultSetMetaData rsmd = rst2.getMetaData();\r\n      System.out.println(rst.getString(1) + \" \" + new java.util.Date());\r\n      while (rst2.next()) {\r\n   \tfor (int i = 1; i <= rsmd.getColumnCount(); i++) {\r\n          if (rst2.getString(i) != null &#038;&#038; rst2.getString(i).equals(\"2071\")) {\r\n            System.out.println(rsmd.getColumnName(i) + \" \" + rst2.getString(i));\r\n          }\r\n        }\r\n      }\r\n      rst2.close();\r\n      stm.close();\r\n    }\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>We had a quick need to search all columns in every table during a data discovery exercise. This is by no means the most efficient, but it does work. It pulls 10,000 rows at a time from each table, and&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2014\/07\/02\/crawling-all-sql-server-tables-and-columns-for-a-specific-value\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[25,34],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/3980"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=3980"}],"version-history":[{"count":3,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/3980\/revisions"}],"predecessor-version":[{"id":3984,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/3980\/revisions\/3984"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=3980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=3980"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=3980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}