Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Must declare the variable '@TheYear'

Must declare the variable '@TheYear'

Scheduled Pinned Locked Moved Database
databasehelp
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sasire18
    wrote on last edited by
    #1

    hi I copied this sql query from msdn I am getting the following

    Must declare the variable '@TheYear'

    If i declare the value for the variable @TheYear I am getting the same error .Whats the matter with query any idea the query is below

    SELECT o.customerid, od.orderid, SUM(od.quantity*od.unitprice) AS price
    FROM Orders o, [Order Details] od
    WHERE Year(o.orderdate) = @TheYear AND od.orderid=o.orderid
    GROUP BY o.customerid, od.orderid
    ORDER BY o.customerid

    DECLARE @TheYear int
    SET @TheYear = 1998

    SELECT
    CASE GROUPING(o.customerid) WHEN 0
    THEN o.customerid ELSE '(Total)' END AS MyCustomerID,
    CASE GROUPING(od.orderid) WHEN 0
    THEN od.orderid ELSE -1 END AS MyOrderID,
    SUM(od.quantity*od.unitprice) AS price
    FROM Orders o, [Order Details] od
    WHERE Year(orderdate) = @TheYear AND od.orderid=o.orderid
    GROUP BY o.customerid, od.orderid WITH ROLLUP
    ORDER BY o.customerid, price

    sasi

    P V 2 Replies Last reply
    0
    • S sasire18

      hi I copied this sql query from msdn I am getting the following

      Must declare the variable '@TheYear'

      If i declare the value for the variable @TheYear I am getting the same error .Whats the matter with query any idea the query is below

      SELECT o.customerid, od.orderid, SUM(od.quantity*od.unitprice) AS price
      FROM Orders o, [Order Details] od
      WHERE Year(o.orderdate) = @TheYear AND od.orderid=o.orderid
      GROUP BY o.customerid, od.orderid
      ORDER BY o.customerid

      DECLARE @TheYear int
      SET @TheYear = 1998

      SELECT
      CASE GROUPING(o.customerid) WHEN 0
      THEN o.customerid ELSE '(Total)' END AS MyCustomerID,
      CASE GROUPING(od.orderid) WHEN 0
      THEN od.orderid ELSE -1 END AS MyOrderID,
      SUM(od.quantity*od.unitprice) AS price
      FROM Orders o, [Order Details] od
      WHERE Year(orderdate) = @TheYear AND od.orderid=o.orderid
      GROUP BY o.customerid, od.orderid WITH ROLLUP
      ORDER BY o.customerid, price

      sasi

      P Offline
      P Offline
      Pankaj Kulkarni
      wrote on last edited by
      #2

      Hello

      sasire18 wrote:

      SELECT o.customerid, od.orderid, SUM(od.quantity*od.unitprice) AS price FROM Orders o, [Order Details] od WHERE Year(o.orderdate) = @TheYear AND od.orderid=o.orderid GROUP BY o.customerid, od.orderid ORDER BY o.customerid DECLARE @TheYear int SET @TheYear = 1998

      you have to declare "@TheYear" before the query but you declared it after the query. Pankaj Kulkarni

      1 Reply Last reply
      0
      • S sasire18

        hi I copied this sql query from msdn I am getting the following

        Must declare the variable '@TheYear'

        If i declare the value for the variable @TheYear I am getting the same error .Whats the matter with query any idea the query is below

        SELECT o.customerid, od.orderid, SUM(od.quantity*od.unitprice) AS price
        FROM Orders o, [Order Details] od
        WHERE Year(o.orderdate) = @TheYear AND od.orderid=o.orderid
        GROUP BY o.customerid, od.orderid
        ORDER BY o.customerid

        DECLARE @TheYear int
        SET @TheYear = 1998

        SELECT
        CASE GROUPING(o.customerid) WHEN 0
        THEN o.customerid ELSE '(Total)' END AS MyCustomerID,
        CASE GROUPING(od.orderid) WHEN 0
        THEN od.orderid ELSE -1 END AS MyOrderID,
        SUM(od.quantity*od.unitprice) AS price
        FROM Orders o, [Order Details] od
        WHERE Year(orderdate) = @TheYear AND od.orderid=o.orderid
        GROUP BY o.customerid, od.orderid WITH ROLLUP
        ORDER BY o.customerid, price

        sasi

        V Offline
        V Offline
        venkat2reddy
        wrote on last edited by
        #3

        DECLARE @TheYear INT SELECT o.customerid, od.orderid, SUM(od.quantity*od.unitprice) AS price FROM Orders o, [Order Details] od WHERE Year(o.orderdate) = @TheYear AND od.orderid=o.orderid GROUP BY o.customerid, od.orderid ORDER BY o.customerid SET @TheYear = 1998 SELECT CASE GROUPING(o.customerid) WHEN 0 THEN o.customerid ELSE '(Total)' END AS MyCustomerID, CASE GROUPING(od.orderid) WHEN 0 THEN od.orderid ELSE -1 END AS MyOrderID, SUM(od.quantity*od.unitprice) AS price FROM Orders o, [Order Details] od WHERE Year(orderdate) = @TheYear AND od.orderid=o.orderid GROUP BY o.customerid, od.orderid WITH ROLLUP ORDER BY o.customerid, price check now. it will work.

        C 1 Reply Last reply
        0
        • V venkat2reddy

          DECLARE @TheYear INT SELECT o.customerid, od.orderid, SUM(od.quantity*od.unitprice) AS price FROM Orders o, [Order Details] od WHERE Year(o.orderdate) = @TheYear AND od.orderid=o.orderid GROUP BY o.customerid, od.orderid ORDER BY o.customerid SET @TheYear = 1998 SELECT CASE GROUPING(o.customerid) WHEN 0 THEN o.customerid ELSE '(Total)' END AS MyCustomerID, CASE GROUPING(od.orderid) WHEN 0 THEN od.orderid ELSE -1 END AS MyOrderID, SUM(od.quantity*od.unitprice) AS price FROM Orders o, [Order Details] od WHERE Year(orderdate) = @TheYear AND od.orderid=o.orderid GROUP BY o.customerid, od.orderid WITH ROLLUP ORDER BY o.customerid, price check now. it will work.

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          No. It won't. You declare @TheYear, use @TheYear, and then SET @TheYear. What is the value of @TheYear in the first SELECT statement. As the question was already answered correctly, I'm left wondering why you also answered. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

          S 1 Reply Last reply
          0
          • C Colin Angus Mackay

            No. It won't. You declare @TheYear, use @TheYear, and then SET @TheYear. What is the value of @TheYear in the first SELECT statement. As the question was already answered correctly, I'm left wondering why you also answered. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

            S Offline
            S Offline
            sasire18
            wrote on last edited by
            #5

            I actually got my answer from Pankaj sasi

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups