Topic
6 replies Latest Post - ‏2013-05-14T15:38:59Z by LukeNumrych
LukeNumrych
LukeNumrych
55 Posts
ACCEPTED ANSWER

Pinned topic How to check for existence of a global variable

‏2013-05-09T20:14:27Z |

How would one check if a global variable already exists in order to prevent trying to create it again or using it while it does not exist?

I looked at DB2 SQL reference, but my search skills are failing to retrieve anything relevant.

I am guessing I can just investigate one of the system views, but I was wondering if there exists a method or function that would tell me that.

Updated on 2013-05-09T20:21:55Z at 2013-05-09T20:21:55Z by LukeNumrych
  • nivanov1
    nivanov1
    231 Posts
    ACCEPTED ANSWER

    Re: How to check for existence of a global variable

    ‏2013-05-09T20:45:22Z  in response to LukeNumrych

    Check the SYSCAT.VARIABLES catalog view.

  • LukeNumrych
    LukeNumrych
    55 Posts
    ACCEPTED ANSWER

    Re: How to check for existence of a global variable

    ‏2013-05-14T14:03:18Z  in response to LukeNumrych

    That is definitely an easy way to check for existence of a variable...  turns out this did not really help my purposes, but this is the correct answer.

    • nivanov1
      nivanov1
      231 Posts
      ACCEPTED ANSWER

      Re: How to check for existence of a global variable

      ‏2013-05-14T14:42:13Z  in response to LukeNumrych

      So what are your purposes then?

      • LukeNumrych
        LukeNumrych
        55 Posts
        ACCEPTED ANSWER

        Re: How to check for existence of a global variable

        ‏2013-05-14T14:52:00Z  in response to nivanov1

        Well, I was trying to do something without creating a stored procedure.  While I can do a select on syscat.variables, making a decision based on the results requires a conditional statement, which in turn requires a procedure or a compound statement...  A compound statement will not help me because I don't think a global temporary variable can be declared within one.

        • nivanov1
          nivanov1
          231 Posts
          ACCEPTED ANSWER

          Re: How to check for existence of a global variable

          ‏2013-05-14T15:02:15Z  in response to LukeNumrych

          And CREATE OR REPLACE VARIABLE.. does not work for you? In a compound statement you would need to use EXECUTE IMMEDIATE to issue a DDL statement, which CREATE VARIABLE is.

          • LukeNumrych
            LukeNumrych
            55 Posts
            ACCEPTED ANSWER

            Re: How to check for existence of a global variable

            ‏2013-05-14T15:38:59Z  in response to nivanov1

            Ahh, of course... CREATE OR REPLACE definitely would work!

            Nothing better than a simple solution... Thanks!