{"id":1888,"date":"2012-02-08T09:30:49","date_gmt":"2012-02-08T14:30:49","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=1888"},"modified":"2012-02-08T09:48:14","modified_gmt":"2012-02-08T14:48:14","slug":"query-sql-server-stored-procdure-text-with-jdbc","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2012\/02\/08\/query-sql-server-stored-procdure-text-with-jdbc\/","title":{"rendered":"Query SQL Server stored procedure text with JDBC"},"content":{"rendered":"<p>I needed to quickly print the text associated with several stored procedures across multiple database servers to individual files.  I used what is below.<\/p>\n<pre lang=\"java\" line=\"1\">\r\nimport java.sql.*;\r\nimport com.microsoft.sqlserver.jdbc.*;\r\nimport java.io.*;\r\n\r\npublic class sqlProcedures {\r\n  public static void main(String[] args) {\r\n    String[] hosts = {\"host1\",\"host2\",\"host3\"};\r\n    for (int i = 0; i < hosts.length; i++) {\r\n      String connString = \"jdbc:sqlserver:\/\/\" + hosts[i] + \":1433;\";\r\n      try {\r\n        Connection con = DriverManager.getConnection(connString + \"databaseName=dbaDB;integratedSecurity=true;\");\r\n        PreparedStatement stm = con.prepareCall(\"select name,id from sysobjects where name like 'dbasp%' order by 1\");\r\n        ResultSet rst = stm.executeQuery();\r\n \r\n        PreparedStatement stm2 = con.prepareCall(\"select text from syscomments where id = ?\");\r\n        while (rst.next()) {\r\n          BufferedWriter out = new BufferedWriter(new FileWriter(hosts[i] + \"_\" + rst.getString(1) + \".sql\"));\r\n          stm2.setInt(1,rst.getInt(2));          \r\n          ResultSet rst2 = stm2.executeQuery();\r\n          while (rst2.next()) {\r\n            out.write(rst2.getString(1));\r\n          }\r\n          out.close();\r\n        }\r\n      }\r\n      catch (Exception e) {\r\n        e.printStackTrace();\r\n      }\r\n    }\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I needed to quickly print the text associated with several stored procedures across multiple database servers to individual files. I used what is below. import java.sql.*; import com.microsoft.sqlserver.jdbc.*; import java.io.*; public class sqlProcedures { public static void main(String[] args) {&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2012\/02\/08\/query-sql-server-stored-procdure-text-with-jdbc\/\">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":[19,25,34],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/1888"}],"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=1888"}],"version-history":[{"count":5,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/1888\/revisions"}],"predecessor-version":[{"id":1893,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/1888\/revisions\/1893"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1888"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}