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. Parameter names of Stored Procedures

Parameter names of Stored Procedures

Scheduled Pinned Locked Moved Database
helptutorialquestion
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.
  • D Offline
    D Offline
    Dewald
    wrote on last edited by
    #1

    Hi all, I've often wondered about this and always simply worked my way around the issue but I would be curious to know what the correct approach is. Say I have a stored proc like so:

    CREATE PROCEDURE myStoredProc
    @myFirstParam INT = 0,
    @mySecondParam INT = 0
    AS
    BEGIN
    -- Do something
    END

    Now I want to call this proc, let's say from another proc where there is a local variable with the name @myLocalVar. I want to pass the value of that local variable to the second parameter of the stored proc so I call it as follows:

    EXEC myStoredProc @mySecondParam = @myLocalVar

    So far so good, but what if the name of that local variable was @mySecondParam (the same as the parameter name of the proc)? The following won't work:

    EXEC myStoredProc @mySecondParam = @mySecondParam

    So do I really have to create a new local variable with a different name so that I can pass it to the proc?

    DECLARE @myNewVariable INT;
    SET @myNewVariable = @mySecondParam;
    EXEC myStoredProc @mySecondParam = @myNewVariable

    Or is there a way I can pass a variable to a proc if the variable has the same name as the parameter name of the proc? PS. I'm deliberately using an example where the first parameter of the proc is ommitted because I'm specifically interested in the scenario where the parameter names of a stored proc has to be specified (i.e. not inferred by their order).

    M J 2 Replies Last reply
    0
    • D Dewald

      Hi all, I've often wondered about this and always simply worked my way around the issue but I would be curious to know what the correct approach is. Say I have a stored proc like so:

      CREATE PROCEDURE myStoredProc
      @myFirstParam INT = 0,
      @mySecondParam INT = 0
      AS
      BEGIN
      -- Do something
      END

      Now I want to call this proc, let's say from another proc where there is a local variable with the name @myLocalVar. I want to pass the value of that local variable to the second parameter of the stored proc so I call it as follows:

      EXEC myStoredProc @mySecondParam = @myLocalVar

      So far so good, but what if the name of that local variable was @mySecondParam (the same as the parameter name of the proc)? The following won't work:

      EXEC myStoredProc @mySecondParam = @mySecondParam

      So do I really have to create a new local variable with a different name so that I can pass it to the proc?

      DECLARE @myNewVariable INT;
      SET @myNewVariable = @mySecondParam;
      EXEC myStoredProc @mySecondParam = @myNewVariable

      Or is there a way I can pass a variable to a proc if the variable has the same name as the parameter name of the proc? PS. I'm deliberately using an example where the first parameter of the proc is ommitted because I'm specifically interested in the scenario where the parameter names of a stored proc has to be specified (i.e. not inferred by their order).

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      This works for me, local and procedure variable is the same.

      DECLARE
      @ContentTypeID INT

      SET @ContentTypeID = 4

      EXEC dbo.ContentTypeSelect @ContentTypeID = @ContentTypeID -- int

      Never underestimate the power of human stupidity RAH

      D S 2 Replies Last reply
      0
      • M Mycroft Holmes

        This works for me, local and procedure variable is the same.

        DECLARE
        @ContentTypeID INT

        SET @ContentTypeID = 4

        EXEC dbo.ContentTypeSelect @ContentTypeID = @ContentTypeID -- int

        Never underestimate the power of human stupidity RAH

        D Offline
        D Offline
        Dewald
        wrote on last edited by
        #3

        How silly of me. I simply assumed that EXEC MyProc @MyParam = @MyParam wouldn't work. Just tested it and no problem at all. :doh: Thanks.

        1 Reply Last reply
        0
        • D Dewald

          Hi all, I've often wondered about this and always simply worked my way around the issue but I would be curious to know what the correct approach is. Say I have a stored proc like so:

          CREATE PROCEDURE myStoredProc
          @myFirstParam INT = 0,
          @mySecondParam INT = 0
          AS
          BEGIN
          -- Do something
          END

          Now I want to call this proc, let's say from another proc where there is a local variable with the name @myLocalVar. I want to pass the value of that local variable to the second parameter of the stored proc so I call it as follows:

          EXEC myStoredProc @mySecondParam = @myLocalVar

          So far so good, but what if the name of that local variable was @mySecondParam (the same as the parameter name of the proc)? The following won't work:

          EXEC myStoredProc @mySecondParam = @mySecondParam

          So do I really have to create a new local variable with a different name so that I can pass it to the proc?

          DECLARE @myNewVariable INT;
          SET @myNewVariable = @mySecondParam;
          EXEC myStoredProc @mySecondParam = @myNewVariable

          Or is there a way I can pass a variable to a proc if the variable has the same name as the parameter name of the proc? PS. I'm deliberately using an example where the first parameter of the proc is ommitted because I'm specifically interested in the scenario where the parameter names of a stored proc has to be specified (i.e. not inferred by their order).

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          You do know that you can just list the input values without specifying the parameter name right? Thus your example becomes.

          EXEC myStoredProc @mySecondParam

          1 Reply Last reply
          0
          • M Mycroft Holmes

            This works for me, local and procedure variable is the same.

            DECLARE
            @ContentTypeID INT

            SET @ContentTypeID = 4

            EXEC dbo.ContentTypeSelect @ContentTypeID = @ContentTypeID -- int

            Never underestimate the power of human stupidity RAH

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

            yap it will work

            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