{"id":86,"date":"2019-08-22T02:22:42","date_gmt":"2019-08-22T02:22:42","guid":{"rendered":"https:\/\/snowflake.pavlik.us\/?p=86"},"modified":"2020-01-26T19:26:17","modified_gmt":"2020-01-26T19:26:17","slug":"executing-multiple-sql-statements-in-a-stored-procedure","status":"publish","type":"post","link":"https:\/\/snowflake.pavlik.us\/index.php\/2019\/08\/22\/executing-multiple-sql-statements-in-a-stored-procedure\/","title":{"rendered":"Executing Multiple SQL Statements in a Stored Procedure"},"content":{"rendered":"\n<p>A classic DBA technique to run a large number of SQL statements is to create them using a concatenated select statement.  Suppose you need to delete all tables that end with &#8220;TEST&#8221;. You can list them in Snowflake&#8217;s INFORMATION_SCHEMA using:<\/p>\n\n\n\n<p>select &#8220;TABLE_NAME&#8221; from INFORMATION_SCHEMA.TABLES where &#8220;TABLE_NAME&#8221; ilike &#8216;%TEST&#8217;;<\/p>\n\n\n\n<p>If you need to drop a handful of tables that way, a list if enough. If there are dozens or hundreds, this works better:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nselect &#039;drop table &#039; || &quot;TABLE_NAME&quot; || &#039;;&#039; from INFORMATION_SCHEMA.TABLES where &quot;TABLE_NAME&quot; ilike &#039;%TEST&#039;;\n<\/pre><\/div>\n\n\n<p>The above example generates a drop statement for each table in a database that ends with TEST. You can use this technique to generate bulk SQL to perform a wide range of management tasks. For example you may want to change the ownership of tables in bulk like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT &#039;grant ownership on table &#039; || \n       table_name || \n       &#039; to role my_new_role copy grants;&#039; \n       AS SQL_COMMAND\nFROM INFORMATION_SCHEMA.TABLE_PRIVILEGES \nWHERE grantor = &#039;old_grant_role&#039;;\n<\/pre><\/div>\n\n\n<p>Executing this SQL will generate rows that look like this:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>Row<\/code><\/td><td><code>SQL_COMMAND<\/code><\/td><\/tr><tr><td><code>1<\/code><\/td><td><code>grant ownership on table CUSTOMER to role my_new_role copy grants;<\/code><\/td><\/tr><tr><td><code>2<\/code><\/td><td><code>grant ownership on table LINEITEM to role my_new_role copy grants;<\/code><\/td><\/tr><tr><td><code>3<\/code><\/td><td><code>grant ownership on table NATION to role my_new_role copy grants;<\/code><\/td><\/tr><tr><td><code>4<\/code><\/td><td><code>grant ownership on table ORDERS to role my_new_role copy grants;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>You can see where this is heading. A SQL statement generates statements to run, and then someone &#8212; or preferably some<strong>thing<\/strong> by automation &#8212; runs them. <\/p>\n\n\n\n<p>Here is that something. The Snowflake stored procedure below will:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Accept a string parameter that is a SQL statement designed to generate rows of SQL statements to execute.<\/li><li>Execute the input SQL statement to generate a list of SQL statements to run.<\/li><li>Run all statements identified by the &#8220;SQL_COMMAND&#8221; column one at a time.<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nuse database TEST;\nuse warehouse TEST;\n\ncreate or replace procedure RunBatchSQL(sqlCommand String)\n    returns string\n    language JavaScript\nas\n$$\n\/**\n * Stored procedure to execute multiple SQL statements generated from a SQL query\n * Note that this procedure will always use the column named &quot;SQL_COMMAND&quot;\n *\n * @param {String} sqlCommand: The SQL query to run to generate one or more SQL commands \n * @return {String}: A string containing all the SQL commands executed, each separated by a newline. \n *\/\n      cmd1_dict = {sqlText: SQLCOMMAND};\n      stmt = snowflake.createStatement(cmd1_dict);\n   \n      rs = stmt.execute();\n\n      var s = &#039;&#039;;\n\n      while (rs.next())  {\n          cmd2_dict = {sqlText: rs.getColumnValue(&quot;SQL_COMMAND&quot;)};\n          stmtEx = snowflake.createStatement(cmd2_dict);\n          stmtEx.execute();\n          s += rs.getColumnValue(1) + &quot;\\n&quot;;\n          }\n          \n      return s;\n      \n$$\n;\n\n-- This is a select query that will generate a list of SQL commands to excute, in this case some grant statements. \n-- This SQL will generate rows to grant select on all tables for the DBA role (change to specify another role). \nselect distinct (&#039;grant select on table &#039; || table_schema || &#039;.&#039; || table_name || &#039; to role DBA;&#039;) AS SQL_COMMAND\nfrom INFORMATION_SCHEMA.TABLE_PRIVILEGES\nwhere TABLE_SCHEMA &lt;&gt; &#039;AUDIT&#039;\norder by SQL_COMMAND;\n\n-- As a convienience, this grabs the last SQL run so that it&#039;s easier to insert into the parameter used to call the stored procedure. \nset query_text = (  select QUERY_TEXT\n                    from table(information_schema.query_history(result_limit =&gt; 2))\n                    where SESSION_ID = Current_Session() and QUERY_TYPE = &#039;SELECT&#039; order by START_TIME desc);\n\n-- Confirm that the query_text variable has the correct SQL query to generate our SQL commands (grants in this case) to run.\nselect $query_text;\n\n-- Run the stored procedure. Note that to view its output better, double click on the output to see it in multi-line format,\nCall RunBatchSQL($query_text);\n\n--Check the last several queries run to make sure it worked.\nselect QUERY_TEXT\nfrom table(information_schema.query_history(result_limit =&gt; 100))\nwhere SESSION_ID = Current_Session() order by START_TIME desc;\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>A classic DBA technique to run a large number of SQL statements is to create them using a concatenated select statement. Suppose you need to delete all tables that end with &#8220;TEST&#8221;. You can list them in Snowflake&#8217;s INFORMATION_SCHEMA using: select &#8220;TABLE_NAME&#8221; from INFORMATION_SCHEMA.TABLES where &#8220;TABLE_NAME&#8221; ilike &#8216;%TEST&#8217;; If you need to drop a handful [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,13],"tags":[],"class_list":["post-86","post","type-post","status-publish","format-standard","hentry","category-sql","category-stored-procedures-sql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\r\n<title>Executing Multiple SQL Statements in a Stored Procedure - Snowflake in the Carolinas<\/title>\r\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/snowflake.pavlik.us\/index.php\/2019\/08\/22\/executing-multiple-sql-statements-in-a-stored-procedure\/\" \/>\r\n<meta property=\"og:locale\" content=\"en_US\" \/>\r\n<meta property=\"og:type\" content=\"article\" \/>\r\n<meta property=\"og:title\" content=\"Executing Multiple SQL Statements in a Stored Procedure - Snowflake in the Carolinas\" \/>\r\n<meta property=\"og:description\" content=\"A classic DBA technique to run a large number of SQL statements is to create them using a concatenated select statement. Suppose you need to delete all tables that end with &#8220;TEST&#8221;. You can list them in Snowflake&#8217;s INFORMATION_SCHEMA using: select &#8220;TABLE_NAME&#8221; from INFORMATION_SCHEMA.TABLES where &#8220;TABLE_NAME&#8221; ilike &#8216;%TEST&#8217;; If you need to drop a handful [&hellip;]\" \/>\r\n<meta property=\"og:url\" content=\"https:\/\/snowflake.pavlik.us\/index.php\/2019\/08\/22\/executing-multiple-sql-statements-in-a-stored-procedure\/\" \/>\r\n<meta property=\"og:site_name\" content=\"Snowflake in the Carolinas\" \/>\r\n<meta property=\"article:published_time\" content=\"2019-08-22T02:22:42+00:00\" \/>\r\n<meta property=\"article:modified_time\" content=\"2020-01-26T19:26:17+00:00\" \/>\r\n<meta name=\"author\" content=\"Greg Pavlik\" \/>\r\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\r\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Greg Pavlik\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\r\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/index.php\\\/2019\\\/08\\\/22\\\/executing-multiple-sql-statements-in-a-stored-procedure\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/index.php\\\/2019\\\/08\\\/22\\\/executing-multiple-sql-statements-in-a-stored-procedure\\\/\"},\"author\":{\"name\":\"Greg Pavlik\",\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/#\\\/schema\\\/person\\\/019455f4675665b6cf5edea31ec44d7b\"},\"headline\":\"Executing Multiple SQL Statements in a Stored Procedure\",\"datePublished\":\"2019-08-22T02:22:42+00:00\",\"dateModified\":\"2020-01-26T19:26:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/index.php\\\/2019\\\/08\\\/22\\\/executing-multiple-sql-statements-in-a-stored-procedure\\\/\"},\"wordCount\":226,\"commentCount\":0,\"articleSection\":[\"SnowSQL\",\"Stored Procedures\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/snowflake.pavlik.us\\\/index.php\\\/2019\\\/08\\\/22\\\/executing-multiple-sql-statements-in-a-stored-procedure\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/index.php\\\/2019\\\/08\\\/22\\\/executing-multiple-sql-statements-in-a-stored-procedure\\\/\",\"url\":\"https:\\\/\\\/snowflake.pavlik.us\\\/index.php\\\/2019\\\/08\\\/22\\\/executing-multiple-sql-statements-in-a-stored-procedure\\\/\",\"name\":\"Executing Multiple SQL Statements in a Stored Procedure - Snowflake in the Carolinas\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/#website\"},\"datePublished\":\"2019-08-22T02:22:42+00:00\",\"dateModified\":\"2020-01-26T19:26:17+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/#\\\/schema\\\/person\\\/019455f4675665b6cf5edea31ec44d7b\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/index.php\\\/2019\\\/08\\\/22\\\/executing-multiple-sql-statements-in-a-stored-procedure\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/snowflake.pavlik.us\\\/index.php\\\/2019\\\/08\\\/22\\\/executing-multiple-sql-statements-in-a-stored-procedure\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/index.php\\\/2019\\\/08\\\/22\\\/executing-multiple-sql-statements-in-a-stored-procedure\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/snowflake.pavlik.us\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Executing Multiple SQL Statements in a Stored Procedure\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/#website\",\"url\":\"https:\\\/\\\/snowflake.pavlik.us\\\/\",\"name\":\"Snowflake in the Carolinas\",\"description\":\"Random thoughts on all things Snowflake in the Carolinas\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/snowflake.pavlik.us\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/#\\\/schema\\\/person\\\/019455f4675665b6cf5edea31ec44d7b\",\"name\":\"Greg Pavlik\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d81df729eebf37a042922b17d4a4c834b1e0ccfa9fea1c2c78cb8e95c7e91701?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d81df729eebf37a042922b17d4a4c834b1e0ccfa9fea1c2c78cb8e95c7e91701?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d81df729eebf37a042922b17d4a4c834b1e0ccfa9fea1c2c78cb8e95c7e91701?s=96&d=mm&r=g\",\"caption\":\"Greg Pavlik\"},\"description\":\"Greg is a Senior Sales Engineer at Snowflake Computing, in the Raleigh-Durham area. He's been in data management and security for the twenty years.\"}]}<\/script>\r\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Executing Multiple SQL Statements in a Stored Procedure - Snowflake in the Carolinas","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/snowflake.pavlik.us\/index.php\/2019\/08\/22\/executing-multiple-sql-statements-in-a-stored-procedure\/","og_locale":"en_US","og_type":"article","og_title":"Executing Multiple SQL Statements in a Stored Procedure - Snowflake in the Carolinas","og_description":"A classic DBA technique to run a large number of SQL statements is to create them using a concatenated select statement. Suppose you need to delete all tables that end with &#8220;TEST&#8221;. You can list them in Snowflake&#8217;s INFORMATION_SCHEMA using: select &#8220;TABLE_NAME&#8221; from INFORMATION_SCHEMA.TABLES where &#8220;TABLE_NAME&#8221; ilike &#8216;%TEST&#8217;; If you need to drop a handful [&hellip;]","og_url":"https:\/\/snowflake.pavlik.us\/index.php\/2019\/08\/22\/executing-multiple-sql-statements-in-a-stored-procedure\/","og_site_name":"Snowflake in the Carolinas","article_published_time":"2019-08-22T02:22:42+00:00","article_modified_time":"2020-01-26T19:26:17+00:00","author":"Greg Pavlik","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Greg Pavlik","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/snowflake.pavlik.us\/index.php\/2019\/08\/22\/executing-multiple-sql-statements-in-a-stored-procedure\/#article","isPartOf":{"@id":"https:\/\/snowflake.pavlik.us\/index.php\/2019\/08\/22\/executing-multiple-sql-statements-in-a-stored-procedure\/"},"author":{"name":"Greg Pavlik","@id":"https:\/\/snowflake.pavlik.us\/#\/schema\/person\/019455f4675665b6cf5edea31ec44d7b"},"headline":"Executing Multiple SQL Statements in a Stored Procedure","datePublished":"2019-08-22T02:22:42+00:00","dateModified":"2020-01-26T19:26:17+00:00","mainEntityOfPage":{"@id":"https:\/\/snowflake.pavlik.us\/index.php\/2019\/08\/22\/executing-multiple-sql-statements-in-a-stored-procedure\/"},"wordCount":226,"commentCount":0,"articleSection":["SnowSQL","Stored Procedures"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/snowflake.pavlik.us\/index.php\/2019\/08\/22\/executing-multiple-sql-statements-in-a-stored-procedure\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/snowflake.pavlik.us\/index.php\/2019\/08\/22\/executing-multiple-sql-statements-in-a-stored-procedure\/","url":"https:\/\/snowflake.pavlik.us\/index.php\/2019\/08\/22\/executing-multiple-sql-statements-in-a-stored-procedure\/","name":"Executing Multiple SQL Statements in a Stored Procedure - Snowflake in the Carolinas","isPartOf":{"@id":"https:\/\/snowflake.pavlik.us\/#website"},"datePublished":"2019-08-22T02:22:42+00:00","dateModified":"2020-01-26T19:26:17+00:00","author":{"@id":"https:\/\/snowflake.pavlik.us\/#\/schema\/person\/019455f4675665b6cf5edea31ec44d7b"},"breadcrumb":{"@id":"https:\/\/snowflake.pavlik.us\/index.php\/2019\/08\/22\/executing-multiple-sql-statements-in-a-stored-procedure\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/snowflake.pavlik.us\/index.php\/2019\/08\/22\/executing-multiple-sql-statements-in-a-stored-procedure\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/snowflake.pavlik.us\/index.php\/2019\/08\/22\/executing-multiple-sql-statements-in-a-stored-procedure\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/snowflake.pavlik.us\/"},{"@type":"ListItem","position":2,"name":"Executing Multiple SQL Statements in a Stored Procedure"}]},{"@type":"WebSite","@id":"https:\/\/snowflake.pavlik.us\/#website","url":"https:\/\/snowflake.pavlik.us\/","name":"Snowflake in the Carolinas","description":"Random thoughts on all things Snowflake in the Carolinas","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/snowflake.pavlik.us\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/snowflake.pavlik.us\/#\/schema\/person\/019455f4675665b6cf5edea31ec44d7b","name":"Greg Pavlik","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d81df729eebf37a042922b17d4a4c834b1e0ccfa9fea1c2c78cb8e95c7e91701?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d81df729eebf37a042922b17d4a4c834b1e0ccfa9fea1c2c78cb8e95c7e91701?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d81df729eebf37a042922b17d4a4c834b1e0ccfa9fea1c2c78cb8e95c7e91701?s=96&d=mm&r=g","caption":"Greg Pavlik"},"description":"Greg is a Senior Sales Engineer at Snowflake Computing, in the Raleigh-Durham area. He's been in data management and security for the twenty years."}]}},"_links":{"self":[{"href":"https:\/\/snowflake.pavlik.us\/index.php\/wp-json\/wp\/v2\/posts\/86","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/snowflake.pavlik.us\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/snowflake.pavlik.us\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/snowflake.pavlik.us\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/snowflake.pavlik.us\/index.php\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":16,"href":"https:\/\/snowflake.pavlik.us\/index.php\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"predecessor-version":[{"id":277,"href":"https:\/\/snowflake.pavlik.us\/index.php\/wp-json\/wp\/v2\/posts\/86\/revisions\/277"}],"wp:attachment":[{"href":"https:\/\/snowflake.pavlik.us\/index.php\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/snowflake.pavlik.us\/index.php\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/snowflake.pavlik.us\/index.php\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}