{"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":"

It’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

This is a query includes SQL variables<\/p>\n

    \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  already exists', '', @ProductId)\r\n  END \r\n<\/pre>\nAll SQL varaibles found in that SQL script<\/p>\n\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>\nEven more, General SQL Parser can find variable used throughout the whole stored procedure to extract a lineage.<\/p>\nTake this SQL for example<\/p>\n\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>\nAnalyzed result will like this:<\/p>\n\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>\nDownload this demo: C# version<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"It’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, ‘Please see the insert in this bill …’ END ELSE BEGIN PRINT REPLACE(‘product already exists’, ”, @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":"\nHelp you to make better use of General SQL Parser<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n