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. Stored Proc not returning my Output

Stored Proc not returning my Output

Scheduled Pinned Locked Moved Database
helpdatabaseannouncement
6 Posts 3 Posters 1 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.
  • M Offline
    M Offline
    matthias s 0
    wrote on last edited by
    #1

    Hi, I've written a stored procedure which should return a specific value and has an additional OUTPUT parameter. Here goes the code: CREATE PROCEDURE dbo.Update_AddresseInfo ( @UserID int, @DateModified DateTime, @ElementID int, @Street varchar(50), @City varchar(50), @ZIP varchar(50), @NewDateModified DateTime OUTPUT ) AS declare @RetVal int declare @DateNow DateTime set @DateNow = GetDate() -- do some stuff UPDATE dbo.AdresseInfo SET Street = @Street, ZIP = @ZIP, City = @City, DateModified = @DateNow WHERE ID = @ElementID if @@ERROR = 0 begin set @NewDateModified = @DateNow set @Retval = 0 end else begin set @Retval = 2 end -- log this event exec LogEvent @UserID, 1, 1 select @Retval RETURN Unfortunately the NewDateModified does not contain what I expect, eventhough I've created a SqlParameter object with the correct Direction. I guess the problem is somewhere in my SQL code. Any help is greatly appreceated. Matthias

    If eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs. (Lounge/David Wulff)

    www.emvoid.de

    M B 2 Replies Last reply
    0
    • M matthias s 0

      Hi, I've written a stored procedure which should return a specific value and has an additional OUTPUT parameter. Here goes the code: CREATE PROCEDURE dbo.Update_AddresseInfo ( @UserID int, @DateModified DateTime, @ElementID int, @Street varchar(50), @City varchar(50), @ZIP varchar(50), @NewDateModified DateTime OUTPUT ) AS declare @RetVal int declare @DateNow DateTime set @DateNow = GetDate() -- do some stuff UPDATE dbo.AdresseInfo SET Street = @Street, ZIP = @ZIP, City = @City, DateModified = @DateNow WHERE ID = @ElementID if @@ERROR = 0 begin set @NewDateModified = @DateNow set @Retval = 0 end else begin set @Retval = 2 end -- log this event exec LogEvent @UserID, 1, 1 select @Retval RETURN Unfortunately the NewDateModified does not contain what I expect, eventhough I've created a SqlParameter object with the correct Direction. I guess the problem is somewhere in my SQL code. Any help is greatly appreceated. Matthias

      If eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs. (Lounge/David Wulff)

      www.emvoid.de

      M Offline
      M Offline
      Michael P Butler
      wrote on last edited by
      #2

      At a guess, @@ERROR isn't being set to 0. Have you tried setting the value outside the IF to see if it gets returned? Michael CP Blog [^]

      M 1 Reply Last reply
      0
      • M Michael P Butler

        At a guess, @@ERROR isn't being set to 0. Have you tried setting the value outside the IF to see if it gets returned? Michael CP Blog [^]

        M Offline
        M Offline
        matthias s 0
        wrote on last edited by
        #3

        Hi Michael, thanks for your response. Yes, I've thought about this as well. But even putting it outside the IF doesn't yield correct results. I'm posting an extract of the code which is actually executing the SP here. Maybe I'm just blind... int nResult = 0; DateTime dtResult = new DateTime(0); SqlCommand cmd = new SqlCommand(ProcName, Open()); try { cmd.CommandType = System.Data.CommandType.StoredProcedure; -- do stuff and and all params... cmd.Parameters.Add(new SqlParameter("ElementID", ElementID)); cmd.Parameters.Add(new SqlParameter("DateModified", DateModified)); // add the DateTime output param SqlParameter prmNewDtModif = new SqlParameter("NewDateModified", dtResult); prmNewDtModif.Direction = System.Data.ParameterDirection.Output; cmd.Parameters.Add(prmNewDtModif); nResult = (Int32) cmd.ExecuteScalar(); } // catch stuff omitted... I've just put the stuff in which seems to be relevant. Variable names are selfexplanatory I guess. Any ideas? Matthias

        If eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs. (Lounge/David Wulff)

        www.emvoid.de

        M 1 Reply Last reply
        0
        • M matthias s 0

          Hi, I've written a stored procedure which should return a specific value and has an additional OUTPUT parameter. Here goes the code: CREATE PROCEDURE dbo.Update_AddresseInfo ( @UserID int, @DateModified DateTime, @ElementID int, @Street varchar(50), @City varchar(50), @ZIP varchar(50), @NewDateModified DateTime OUTPUT ) AS declare @RetVal int declare @DateNow DateTime set @DateNow = GetDate() -- do some stuff UPDATE dbo.AdresseInfo SET Street = @Street, ZIP = @ZIP, City = @City, DateModified = @DateNow WHERE ID = @ElementID if @@ERROR = 0 begin set @NewDateModified = @DateNow set @Retval = 0 end else begin set @Retval = 2 end -- log this event exec LogEvent @UserID, 1, 1 select @Retval RETURN Unfortunately the NewDateModified does not contain what I expect, eventhough I've created a SqlParameter object with the correct Direction. I guess the problem is somewhere in my SQL code. Any help is greatly appreceated. Matthias

          If eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs. (Lounge/David Wulff)

          www.emvoid.de

          B Offline
          B Offline
          bnieland
          wrote on last edited by
          #4

          Post your ADO code where you get NewDateModified out of the command object... DTrent

          M 1 Reply Last reply
          0
          • M matthias s 0

            Hi Michael, thanks for your response. Yes, I've thought about this as well. But even putting it outside the IF doesn't yield correct results. I'm posting an extract of the code which is actually executing the SP here. Maybe I'm just blind... int nResult = 0; DateTime dtResult = new DateTime(0); SqlCommand cmd = new SqlCommand(ProcName, Open()); try { cmd.CommandType = System.Data.CommandType.StoredProcedure; -- do stuff and and all params... cmd.Parameters.Add(new SqlParameter("ElementID", ElementID)); cmd.Parameters.Add(new SqlParameter("DateModified", DateModified)); // add the DateTime output param SqlParameter prmNewDtModif = new SqlParameter("NewDateModified", dtResult); prmNewDtModif.Direction = System.Data.ParameterDirection.Output; cmd.Parameters.Add(prmNewDtModif); nResult = (Int32) cmd.ExecuteScalar(); } // catch stuff omitted... I've just put the stuff in which seems to be relevant. Variable names are selfexplanatory I guess. Any ideas? Matthias

            If eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs. (Lounge/David Wulff)

            www.emvoid.de

            M Offline
            M Offline
            Michael P Butler
            wrote on last edited by
            #5

            Matthias Steinbart wrote: SqlParameter prmNewDtModif = new SqlParameter("NewDateModified", dtResult); I'm not au-fait with all the ins and outs of ADO.NET but shouldn't the Parameter name match the stored procedure? i.e @NewDateModified rather than NewDateModified Michael CP Blog [^]

            1 Reply Last reply
            0
            • B bnieland

              Post your ADO code where you get NewDateModified out of the command object... DTrent

              M Offline
              M Offline
              matthias s 0
              wrote on last edited by
              #6

              Hi DTrent! Thanks for your reply. Your sole proposal has answered my question ("out of the command object..."??? :)) I assumed, that the value gets automatically assigned once the SqlCommand.ExecuteScalar() call returns. I didn't know that I have to retrieve the SqlParameter.Value property and assign it then to the variable which I passed to that parameter. Seems kind of weird to me, in the end I'm asking why am I assigning the variable in the first place? For those interested, here the code: int nResult = 0; DateTime dtResult = new DateTime(0); SqlCommand cmd = new SqlCommand(ProcName, Open()); try { cmd.CommandType = System.Data.CommandType.StoredProcedure; -- do stuff and and all params... cmd.Parameters.Add(new SqlParameter("ElementID", ElementID)); cmd.Parameters.Add(new SqlParameter("DateModified", DateModified)); // add the DateTime output param SqlParameter prmNewDtModif = new SqlParameter("NewDateModified", dtResult); prmNewDtModif.Direction = System.Data.ParameterDirection.Output; cmd.Parameters.Add(prmNewDtModif); nResult = (Int32) cmd.ExecuteScalar(); } // catch stuff omitted... // To make the code work, uncomment the following line. // Omitting the following line, was actually the problem // but I thought, that the value (cause it got passed to the SqlParameter Ctor) // gets assigned automatically. // dtResult = (DateTime) prmNewDtModif.Value; // start using the dtResult variable if (dtResult < dtSomething) { // go do stuff... } Thanks for your help! Matthias

              If eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs. (Lounge/David Wulff)

              www.emvoid.de

              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