How to do...well...anything...in DB2 SQL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Knut Stolze

    Re: How to do...well...any thing...in DB2 SQL

    <posted & mailed>

    J. Moreno wrote:
    [color=blue]
    > Knut Stolze <stolze@de.ibm. com> wrote:[color=green]
    >> planb@newsreade rs.com wrote:
    >>[/color]
    > -snip-[color=green][color=darkred]
    >> > So, how do I declare the variable so that DB2 will accept it?
    >> >
    >> > (Sorry for the late reply, but I read through the thread and didn't
    >> > find an answer I understood).[/color]
    >>
    >> If your actual SQL statement is a query, you could use a common table
    >> expression to have just the original two parameter markers:
    >>
    >> WITH parms(p1, p2) AS ( VALUES (CAST(? AS INT), CAST(? AS INT))
    >> SELECT ...
    >> FROM <table>, parms
    >> WHERE table.col1 < parms.p1 AND table.col1 > parms.p2 AND ...[/color]
    >
    > That sounds good -- but I can't get it to work....
    >
    > Trying to turn the above into a simple query that works, I tried
    >
    > WITH params(p1, p2) AS ( VALUES (CAST('1' AS INT), CAST('2' AS INT) ) )
    > SELECT 'a' AS answer FROM params[/color]

    This works quite nicely:

    $ db2 "WITH params(p1, p2) AS ( VALUES (CAST('1' AS INT), CAST('' AS
    INT) ) ) SELECT 'a' AS answer FROM params"

    ANSWER
    ------
    a

    1 record(s) selected.
    [color=blue]
    > as well as
    >
    > WITH params(p1, p2) AS
    > ( SELECT '1', '2' )
    > SELECT 'a' AS answer FROM params[/color]

    This can obviously not work because you have a syntax error in the common
    table expression. There is no valid SELECT statement specified there.
    [color=blue]
    > neither works. I get the same error message for both querys (Server: Msg
    > 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'WITH'.
    > -- from Query Analyzer, and "An unexpected token "(" was found following
    > '''. Expected tokens include: "IS <hexstring><cha rstring<graphst ring>".
    > SQLSTATE=46201 -- from report services when I run it against the DB2
    > database).[/color]

    What's the exact error message?

    I'd suspect that something else besides DB2 is screwing things up. Maybe
    you're not using the IBM ODBC/CLI driver?

    --
    Knut Stolze
    DB2 Information Integration Development
    IBM Germany

    Comment

    • J. B. Moreno

      Re: How to do...well...any thing...in DB2 SQL

      Knut Stolze <stolze@de.ibm. com> wrote:
      [color=blue]
      > J. Moreno wrote:
      >[color=green]
      > > Knut Stolze <stolze@de.ibm. com> wrote:[/color][/color]
      -snip-[color=blue][color=green][color=darkred]
      > >> If your actual SQL statement is a query, you could use a common table
      > >> expression to have just the original two parameter markers:
      > >>
      > >> WITH parms(p1, p2) AS ( VALUES (CAST(? AS INT), CAST(? AS INT))
      > >> SELECT ...
      > >> FROM <table>, parms
      > >> WHERE table.col1 < parms.p1 AND table.col1 > parms.p2 AND ...[/color]
      > >
      > > That sounds good -- but I can't get it to work....
      > >
      > > Trying to turn the above into a simple query that works, I tried
      > >
      > > WITH params(p1, p2) AS ( VALUES (CAST('1' AS INT), CAST('2' AS INT) ) )
      > > SELECT 'a' AS answer FROM params[/color]
      >
      > This works quite nicely:
      >
      > $ db2 "WITH params(p1, p2) AS ( VALUES (CAST('1' AS INT), CAST('' AS
      > INT) ) ) SELECT 'a' AS answer FROM params"
      >
      > ANSWER
      > ------
      > a
      >
      > 1 record(s) selected.[/color]
      -snip-[color=blue]
      > What's the exact error message?[/color]

      A cut/paste of your code gives this error message:
      An error occurred while executing the query.
      DB2[SQL0104N] An unexpected token "(" was found following '''. Expected
      tokens may include: "IS <HEXSTRING><CHA RSTRING><GRAPHS TRING>".
      SQLSTATE=42601

      If I switch to the Microsoft ODBC driver, I get this error message:
      An error occurred while executing the query.
      ERROR[42401][IBM][CLI Driver][DB2]SQL0104N An unexpected token "(" was
      found following ''''. Expected tokens may include: "IS
      <HEXSTRING><CHA RSTRING><GRAPHS TRING>". SQLSTATE=42601
      [color=blue]
      > I'd suspect that something else besides DB2 is screwing things up. Maybe
      > you're not using the IBM ODBC/CLI driver?[/color]

      I've tried the "IBM OLE DB Provider for DB2 Servers" and the
      "Microsoft OLE DB Provider for ODBC Drivers".

      Too bad I can't get this to work (it'd be ideal for the query that I'm
      working on, which involves two subquery's that are almost identical).

      --
      J. Moreno

      Comment

      • Serge Rielau

        Re: How to do...well...any thing...in DB2 SQL

        J. B. Moreno wrote:[color=blue]
        > Knut Stolze <stolze@de.ibm. com> wrote:
        >[color=green]
        >> J. Moreno wrote:
        >>[color=darkred]
        >>> Knut Stolze <stolze@de.ibm. com> wrote:[/color][/color]
        > -snip-[color=green][color=darkred]
        >>>> If your actual SQL statement is a query, you could use a common table
        >>>> expression to have just the original two parameter markers:
        >>>>
        >>>> WITH parms(p1, p2) AS ( VALUES (CAST(? AS INT), CAST(? AS INT))
        >>>> SELECT ...
        >>>> FROM <table>, parms
        >>>> WHERE table.col1 < parms.p1 AND table.col1 > parms.p2 AND ...
        >>> That sounds good -- but I can't get it to work....
        >>>
        >>> Trying to turn the above into a simple query that works, I tried
        >>>
        >>> WITH params(p1, p2) AS ( VALUES (CAST('1' AS INT), CAST('2' AS INT) ) )
        >>> SELECT 'a' AS answer FROM params[/color]
        >> This works quite nicely:
        >>
        >> $ db2 "WITH params(p1, p2) AS ( VALUES (CAST('1' AS INT), CAST('' AS
        >> INT) ) ) SELECT 'a' AS answer FROM params"
        >>
        >> ANSWER
        >> ------
        >> a
        >>
        >> 1 record(s) selected.[/color]
        > -snip-[color=green]
        >> What's the exact error message?[/color]
        >
        > A cut/paste of your code gives this error message:
        > An error occurred while executing the query.
        > DB2[SQL0104N] An unexpected token "(" was found following '''. Expected
        > tokens may include: "IS <HEXSTRING><CHA RSTRING><GRAPHS TRING>".
        > SQLSTATE=42601
        >
        > If I switch to the Microsoft ODBC driver, I get this error message:
        > An error occurred while executing the query.
        > ERROR[42401][IBM][CLI Driver][DB2]SQL0104N An unexpected token "(" was
        > found following ''''. Expected tokens may include: "IS
        > <HEXSTRING><CHA RSTRING><GRAPHS TRING>". SQLSTATE=42601
        >[color=green]
        >> I'd suspect that something else besides DB2 is screwing things up. Maybe
        >> you're not using the IBM ODBC/CLI driver?[/color]
        >
        > I've tried the "IBM OLE DB Provider for DB2 Servers" and the
        > "Microsoft OLE DB Provider for ODBC Drivers".
        >
        > Too bad I can't get this to work (it'd be ideal for the query that I'm
        > working on, which involves two subquery's that are almost identical).
        >[/color]
        J.B.,

        What platform are you on. Knut is referring to DB2 for LUW.
        DB2 for iSeries or zOS do not support WITH clause yet.

        Cheers
        Serge
        --
        Serge Rielau
        DB2 Solutions Development
        IBM Toronto Lab

        Comment

        • J. Moreno

          Re: How to do...well...any thing...in DB2 SQL

          Serge Rielau <srielau@ca.ibm .com> wrote:[color=blue]
          > J. B. Moreno wrote:[color=green]
          > > Knut Stolze <stolze@de.ibm. com> wrote:
          > >[color=darkred]
          > >> J. Moreno wrote:
          > >>
          > >>> Knut Stolze <stolze@de.ibm. com> wrote:[/color]
          > > -snip-[color=darkred]
          > >>>> If your actual SQL statement is a query, you could use a common
          > >>>> table expression to have just the original two parameter markers:
          > >>>>
          > >>>> WITH parms(p1, p2) AS ( VALUES (CAST(? AS INT), CAST(? AS INT))
          > >>>> SELECT ...
          > >>>> FROM <table>, parms
          > >>>> WHERE table.col1 < parms.p1 AND table.col1 > parms.p2 AND ...
          > >>> That sounds good -- but I can't get it to work....[/color][/color][/color]
          -snip-[color=blue][color=green]
          > > Too bad I can't get this to work (it'd be ideal for the query that I'm
          > > working on, which involves two subquery's that are almost identical).[/color]
          >
          > J.B.,
          >
          > What platform are you on. Knut is referring to DB2 for LUW.
          > DB2 for iSeries or zOS do not support WITH clause yet.[/color]

          That's it -- I'm on zOS. Guess I'm stuck with the repeated subqueries...

          --
          J. Moreno

          Comment

          Working...