Error Must declare the scalar variable "@Dn".? in SQL Server 2005

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Amit Rai
    New Member
    • Aug 2010
    • 5

    Error Must declare the scalar variable "@Dn".? in SQL Server 2005

    Code:
    /* Create a table */
    Create table Department (vDepartmentName char(25),vDepartmentHead char(25))
    
    /* Create two variables that would store the values returned by the fetch statement */
    
    
    DECLARE @DepartmentName char(25)
    DECLARE @DepartmentHead char(25)
    
    /* Define the cursor that can be used to access the records of the table,row by row */
    
    DECLARE curDepartment cursor for
    SELECT vDepartmentName,vDepartmentHead from Department
    
    -- Open the cursor
    
    OPEN curDepartment
    
    -- Fetch the rows into variables
    
    FETCH curDepartment into @DepartmentName,@DepartmentHead
    
    --Start a loop to display all the rows of the cursor
    
    WHILE(@@fetch_status=0)
    BEGIN
    Print 'Department Name =' + @DepartmentName
    Print 'Department Head =' + @DepartmentHead
    
    --Fetch the next row from the cursor
    FETCH curDepartment into @DepartmentName,@DepartmentHead
    END
    
    -- Close the cursor
    
    CLOSE curDepartment
    
    -- Deallocate the cursor
    
    DEALLOCATE curDepartment
    Last edited by NeoPa; Aug 2 '10, 12:39 PM. Reason: Please use the [CODE] tags provided
  • Jerry Winston
    Recognized Expert New Member
    • Jun 2008
    • 145

    #2
    Odd, but your code works just fine on my machine. Do you have any other errors, codes, ect to share?

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32662

      #3
      Amit, are you sure this is an exact copy of the code that was run when you received the error message in the title?

      I'm guessing that's supposed to be the question as it's not in the main post.

      Welcome to Bytes!

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32662

        #4
        Amit, you need to explain the situation here, not simply start a new thread when things appear complicated.

        Comment

        • ck9663
          Recognized Expert Specialist
          • Jun 2007
          • 2878

          #5
          You don't have a variable named @dn. I can't even find anything in your code with the letters "dn".

          ~~ CK

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32662

            #6
            FYI: The OP (Amit) posted a new question which seemed identical except the title reflected the full name of the variable instead of just @dn. I deleted that thread and explained to the OP what he needed to do to progress. We've heard nothing yet, but he may be home after a long day at work if he's working on the subcontinent.

            Comment

            Working...