{"id":367,"date":"2010-07-21T22:40:49","date_gmt":"2010-07-22T03:40:49","guid":{"rendered":"http:\/\/www.dpriver.com\/blog\/?page_id=367"},"modified":"2011-11-07T02:24:49","modified_gmt":"2011-11-07T07:24:49","slug":"find-all-variables-in-sql-script","status":"publish","type":"page","link":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/find-all-variables-in-sql-script\/","title":{"rendered":"Find all variables in SQL script"},"content":{"rendered":"<p>It&#8217;s quite easy to use General SQL Parser to find out all SQL variables in a SQL script. It will not only find variables in declare statement but also find variables where it was used.<\/p>\n<p>This is a query includes SQL variables<\/p>\n<pre>\r\nDECLARE @ProductId BIGINT\r\nDECLARE @OTSProductId BIGINT\r\n\r\nSET @ProductId = 35768\r\nSET @OTSProductId = @ProductId\r\n\r\nIF NOT EXISTS(SELECT ProductId\r\n              FROM   PRODUCT\r\n              WHERE  PRODUCTID = @ProductId)\r\n  BEGIN\r\n      INSERT INTO [Product]\r\n                  ([ProductId],\r\n                   [Name])\r\n      SELECT @ProductId,\r\n             'Please see the insert in this bill ...'\r\n  END\r\nELSE\r\n  BEGIN\r\n      PRINT REPLACE('product <ProdId> already exists', '<ProdId>', @ProductId)\r\n  END \r\n<\/pre>\n<p>All SQL varaibles found in that SQL script<\/p>\n<pre>\r\n@ProductId\r\n@OTSProductId\r\n@ProductId\r\n@OTSProductId\r\n@ProductId\r\n@ProductId\r\n@ProductId\r\n@ProductId\r\n<\/pre>\n<p>Even more, General SQL Parser can find variable used throughout the whole stored procedure to extract a lineage.<\/p>\n<p>Take this SQL for example<\/p>\n<pre>\r\nCREATE PROCEDURE STGTRSODS.P_LOAD_DC_UNIVL_PLAN_R2_R3 \r\n( IN in_cycle_id CHAR(2))\r\n    RESULT SETS 1\r\n    LANGUAGE SQL\r\n    \r\nP1: BEGIN\r\n------------------------------------------------------------------------\r\n-- DECLARE SECTION\r\n------------------------------------------------------------------------\r\nDECLARE SQLSTATE CHAR(5);\r\nDECLARE wk_sqlstate             CHAR(5) DEFAULT '00000';\r\nDECLARE cs_VALUE            CHARACTER(100);\r\nDECLARE wk_fetch_status          CHAR(5) DEFAULT '00000';\r\nDECLARE cs_PLAN_NUM             CHARACTER(6);\r\nDECLARE cs_SUB_PLAN             CHARACTER(6);\r\n\r\n--CLARE cs_if_cursor CURSOR WITH HOLD WITH RETURN FOR cs_if_cursor;\r\n\r\nSET  wk_stg_cfe_r3 = ACCTLOAD.F_GET_ETL_TABLE(in_cycle_id,'ACCTLOAD.T_STG_CFE_$$$_R3');\r\nSET  wk_stg_cfe_r2 = ACCTLOAD.F_GET_ETL_TABLE(in_cycle_id,'ACCTLOAD.T_STG_CFE_$$$_R2');\r\n\r\n\r\nFETCH cs_if_cursor\r\n    INTO cs_PLAN_NUM, cs_SUB_PLAN, cs_KEY_WORD, cs_VALUE;\r\n\r\nWHILE (wk_fetch_status = '00000') DO\r\n    CASE cs_KEY_WORD\r\n        WHEN 'DSHR PARTICIPATING DATASHARE INDICATOR' THEN\r\n            IF EXISTS(SELECT 1 FROM SESSION.GLBLTMP_R3_R2 WHERE PLAN_NUM = cs_PLAN_NUM AND SUB_PLAN = cs_SUB_PLAN) THEN\r\n                UPDATE SESSION.GLBLTMP_R3_R2\r\n                    SET DSHR_PARTICIPATING_IND = SUBSTR(cs_VALUE,1,1)\r\n                 WHERE PLAN_NUM = cs_PLAN_NUM\r\n                   AND SUB_PLAN = cs_SUB_PLAN;\r\n            ELSE\r\n                INSERT INTO SESSION.GLBLTMP_R3_R2 (PLAN_NUM,SUB_PLAN, DSHR_PARTICIPATING_IND, DSHR_FUNCTION)\r\n                VALUES (cs_PLAN_NUM,cs_SUB_PLAN, SUBSTR(cs_VALUE,1,1), NULL);\r\n            END IF;\r\n            \r\n        WHEN 'DSHR DATASHARE FUNCTION' THEN\r\n            IF EXISTS(SELECT 1 FROM SESSION.GLBLTMP_R3_R2 WHERE PLAN_NUM = cs_PLAN_NUM AND SUB_PLAN = cs_SUB_PLAN) THEN\r\n                UPDATE SESSION.GLBLTMP_R3_R2\r\n                    SET DSHR_FUNCTION = SUBSTR(cs_VALUE,1,1)\r\n                 WHERE PLAN_NUM = cs_PLAN_NUM\r\n                   AND SUB_PLAN = cs_SUB_PLAN;\r\n            ELSE\r\n                INSERT INTO SESSION.GLBLTMP_R3_R2 (PLAN_NUM,SUB_PLAN, DSHR_PARTICIPATING_IND, DSHR_FUNCTION)\r\n                VALUES (cs_PLAN_NUM,cs_SUB_PLAN, NULL, SUBSTR(cs_VALUE,1,1));\r\n            END IF;\r\n             \r\n    END CASE;\r\nEND WHILE;\r\n\r\nEND P1\r\n<\/pre>\n<p>Analyzed result will like this:<\/p>\n<pre>\r\nprocedure name: STGTRSODS.P_LOAD_DC_UNIVL_PLAN_R2_R3\r\n\r\nVariable: wk_sqlstate         , Type: CHAR(5)\r\nVariable: cs_VALUE            , Type: CHARACTER(100)\r\nVariable: wk_fetch_status     , Type: CHAR(5)\r\nVariable: cs_PLAN_NUM         , Type: CHARACTER(6)\r\nVariable: cs_SUB_PLAN         , Type: CHARACTER(6)\r\n\r\nvariable: cs_VALUE\r\nupdate table: SESSION.GLBLTMP_R3_R2\r\ncolumn: DSHR_PARTICIPATING_IND\r\nvalue: SUBSTR(cs_VALUE,1,1)\r\n\r\nvariable: cs_PLAN_NUM\r\ninsert table: SESSION.GLBLTMP_R3_R2\r\ncolumn: PLAN_NUM\r\ninsert value: cs_PLAN_NUM\r\n\r\nvariable: cs_SUB_PLAN\r\ninsert table: SESSION.GLBLTMP_R3_R2\r\ncolumn: SUB_PLAN\r\ninsert value: cs_SUB_PLAN\r\n\r\nvariable: cs_VALUE\r\ninsert table: SESSION.GLBLTMP_R3_R2\r\ncolumn: DSHR_PARTICIPATING_IND\r\ninsert value: SUBSTR(cs_VALUE,1,1)\r\n\r\nvariable: cs_VALUE\r\nupdate table: SESSION.GLBLTMP_R3_R2\r\ncolumn: DSHR_FUNCTION\r\nvalue: SUBSTR(cs_VALUE,1,1)\r\n\r\nvariable: cs_PLAN_NUM\r\ninsert table: SESSION.GLBLTMP_R3_R2\r\ncolumn: PLAN_NUM\r\ninsert value: cs_PLAN_NUM\r\n\r\nvariable: cs_SUB_PLAN\r\ninsert table: SESSION.GLBLTMP_R3_R2\r\ncolumn: SUB_PLAN\r\ninsert value: cs_SUB_PLAN\r\n\r\nvariable: cs_VALUE\r\ninsert table: SESSION.GLBLTMP_R3_R2\r\ncolumn: DSHR_FUNCTION\r\ninsert value: SUBSTR(cs_VALUE,1,1)\r\n<\/pre>\n<p>Download this demo: <a href=\"http:\/\/www.dpriver.com\/gsp\/demos\/csharp\/findVariables\/findVariables.cs\">C# version<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s quite easy to use General SQL Parser to find out all SQL variables in a SQL script. It will not only find variables in declare statement but also find variables where it was used. This is a query includes SQL variables DECLARE @ProductId BIGINT DECLARE @OTSProductId BIGINT SET @ProductId = 35768 SET @OTSProductId = @ProductId IF NOT EXISTS(SELECT ProductId FROM PRODUCT WHERE PRODUCTID = @ProductId) BEGIN INSERT INTO [Product] ([ProductId], [Name]) SELECT @ProductId, &#8216;Please see the insert in this bill &#8230;&#8217; END ELSE BEGIN PRINT REPLACE(&#8216;product already exists&#8217;, &#8221;, @ProductId) END All SQL varaibles found in that SQL script\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":235,"menu_order":0,"comment_status":"closed","ping_status":"open","template":"gsp_feature_page_tt.php","meta":[],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":5}},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Help you to make better use of General SQL Parser<\/title>\n<meta name=\"description\" content=\"Find all variables in SQL script\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/find-all-variables-in-sql-script\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Help you to make better use of General SQL Parser\" \/>\n<meta property=\"og:description\" content=\"Find all variables in SQL script\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/find-all-variables-in-sql-script\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL and Data Blog\" \/>\n<meta property=\"article:modified_time\" content=\"2011-11-07T07:24:49+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.dpriver.com\/blog\/#organization\",\"name\":\"SQL and Data Blog\",\"url\":\"https:\/\/www.dpriver.com\/blog\/\",\"sameAs\":[],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.dpriver.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.dpriver.com\/blog\/wp-content\/uploads\/2022\/07\/sqlpp-character.png\",\"contentUrl\":\"https:\/\/www.dpriver.com\/blog\/wp-content\/uploads\/2022\/07\/sqlpp-character.png\",\"width\":251,\"height\":72,\"caption\":\"SQL and Data Blog\"},\"image\":{\"@id\":\"https:\/\/www.dpriver.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.dpriver.com\/blog\/#website\",\"url\":\"https:\/\/www.dpriver.com\/blog\/\",\"name\":\"SQL and Data Blog\",\"description\":\"SQL related blog for database professional\",\"publisher\":{\"@id\":\"https:\/\/www.dpriver.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.dpriver.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/find-all-variables-in-sql-script\/\",\"url\":\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/find-all-variables-in-sql-script\/\",\"name\":\"Help you to make better use of General SQL Parser\",\"isPartOf\":{\"@id\":\"https:\/\/www.dpriver.com\/blog\/#website\"},\"datePublished\":\"2010-07-22T03:40:49+00:00\",\"dateModified\":\"2011-11-07T07:24:49+00:00\",\"description\":\"Find all variables in SQL script\",\"breadcrumb\":{\"@id\":\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/find-all-variables-in-sql-script\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/find-all-variables-in-sql-script\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/find-all-variables-in-sql-script\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.dpriver.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"List of demos illustrate how to use general sql parser\",\"item\":\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Find all variables in SQL script\"}]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Help you to make better use of General SQL Parser","description":"Find all variables in SQL script","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:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/find-all-variables-in-sql-script\/","og_locale":"en_US","og_type":"article","og_title":"Help you to make better use of General SQL Parser","og_description":"Find all variables in SQL script","og_url":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/find-all-variables-in-sql-script\/","og_site_name":"SQL and Data Blog","article_modified_time":"2011-11-07T07:24:49+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/www.dpriver.com\/blog\/#organization","name":"SQL and Data Blog","url":"https:\/\/www.dpriver.com\/blog\/","sameAs":[],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.dpriver.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.dpriver.com\/blog\/wp-content\/uploads\/2022\/07\/sqlpp-character.png","contentUrl":"https:\/\/www.dpriver.com\/blog\/wp-content\/uploads\/2022\/07\/sqlpp-character.png","width":251,"height":72,"caption":"SQL and Data Blog"},"image":{"@id":"https:\/\/www.dpriver.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/www.dpriver.com\/blog\/#website","url":"https:\/\/www.dpriver.com\/blog\/","name":"SQL and Data Blog","description":"SQL related blog for database professional","publisher":{"@id":"https:\/\/www.dpriver.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dpriver.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/find-all-variables-in-sql-script\/","url":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/find-all-variables-in-sql-script\/","name":"Help you to make better use of General SQL Parser","isPartOf":{"@id":"https:\/\/www.dpriver.com\/blog\/#website"},"datePublished":"2010-07-22T03:40:49+00:00","dateModified":"2011-11-07T07:24:49+00:00","description":"Find all variables in SQL script","breadcrumb":{"@id":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/find-all-variables-in-sql-script\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/find-all-variables-in-sql-script\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/find-all-variables-in-sql-script\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.dpriver.com\/blog\/"},{"@type":"ListItem","position":2,"name":"List of demos illustrate how to use general sql parser","item":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/"},{"@type":"ListItem","position":3,"name":"Find all variables in SQL script"}]}]}},"_links":{"self":[{"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/pages\/367"}],"collection":[{"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/comments?post=367"}],"version-history":[{"count":5,"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/pages\/367\/revisions"}],"predecessor-version":[{"id":1281,"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/pages\/367\/revisions\/1281"}],"up":[{"embeddable":true,"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/pages\/235"}],"wp:attachment":[{"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/media?parent=367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}