How can I pass a parameter to a parameterized query?












1















I need to get a query from a database table that contains a parameter. Then use that query to update another table but I need to be able to pass another parameter to that update statement.



declare @locnum int
set @locnum = 032

declare @tempPersonID int
set @tempPersonID = 10008

declare @passwordQuery varchar(max)
set @passwordQuery = (select passwordQuery from location where locationNum = @locnum)

select @passwordQuery

update tempPerson
set [password] = @passwordQuery
where tempPersonID = @tempPersonID

select *
from tempPerson


select @passwordQuery returns (select left(firstname,1) + left(lastname,1) + custom as [password] from tempPerson where tempPersonID = @tempPersonID). I need to able to use the @tempPersomID parameter in this query and the where statement.










share|improve this question























  • The only way to do that us using dynamic sql docs.microsoft.com/en-us/sql/relational-databases/…. Also you know you can assign a variable directly in a select e.g. select @passwordQuery = passwordQuery from location where locationNum = @locnum

    – Dale Burrell
    Dec 31 '18 at 21:14











  • @DaleBurrell I've been looking over the link there and I don't understand how to get the tempPersonID passed down to the @passwordQuery variable. I've not worked with dynamic sql before. Can you provide the application for this situation?

    – Bobby Zimmerman
    Dec 31 '18 at 21:50
















1















I need to get a query from a database table that contains a parameter. Then use that query to update another table but I need to be able to pass another parameter to that update statement.



declare @locnum int
set @locnum = 032

declare @tempPersonID int
set @tempPersonID = 10008

declare @passwordQuery varchar(max)
set @passwordQuery = (select passwordQuery from location where locationNum = @locnum)

select @passwordQuery

update tempPerson
set [password] = @passwordQuery
where tempPersonID = @tempPersonID

select *
from tempPerson


select @passwordQuery returns (select left(firstname,1) + left(lastname,1) + custom as [password] from tempPerson where tempPersonID = @tempPersonID). I need to able to use the @tempPersomID parameter in this query and the where statement.










share|improve this question























  • The only way to do that us using dynamic sql docs.microsoft.com/en-us/sql/relational-databases/…. Also you know you can assign a variable directly in a select e.g. select @passwordQuery = passwordQuery from location where locationNum = @locnum

    – Dale Burrell
    Dec 31 '18 at 21:14











  • @DaleBurrell I've been looking over the link there and I don't understand how to get the tempPersonID passed down to the @passwordQuery variable. I've not worked with dynamic sql before. Can you provide the application for this situation?

    – Bobby Zimmerman
    Dec 31 '18 at 21:50














1












1








1








I need to get a query from a database table that contains a parameter. Then use that query to update another table but I need to be able to pass another parameter to that update statement.



declare @locnum int
set @locnum = 032

declare @tempPersonID int
set @tempPersonID = 10008

declare @passwordQuery varchar(max)
set @passwordQuery = (select passwordQuery from location where locationNum = @locnum)

select @passwordQuery

update tempPerson
set [password] = @passwordQuery
where tempPersonID = @tempPersonID

select *
from tempPerson


select @passwordQuery returns (select left(firstname,1) + left(lastname,1) + custom as [password] from tempPerson where tempPersonID = @tempPersonID). I need to able to use the @tempPersomID parameter in this query and the where statement.










share|improve this question














I need to get a query from a database table that contains a parameter. Then use that query to update another table but I need to be able to pass another parameter to that update statement.



declare @locnum int
set @locnum = 032

declare @tempPersonID int
set @tempPersonID = 10008

declare @passwordQuery varchar(max)
set @passwordQuery = (select passwordQuery from location where locationNum = @locnum)

select @passwordQuery

update tempPerson
set [password] = @passwordQuery
where tempPersonID = @tempPersonID

select *
from tempPerson


select @passwordQuery returns (select left(firstname,1) + left(lastname,1) + custom as [password] from tempPerson where tempPersonID = @tempPersonID). I need to able to use the @tempPersomID parameter in this query and the where statement.







sql-server






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 31 '18 at 21:07









Bobby ZimmermanBobby Zimmerman

383




383













  • The only way to do that us using dynamic sql docs.microsoft.com/en-us/sql/relational-databases/…. Also you know you can assign a variable directly in a select e.g. select @passwordQuery = passwordQuery from location where locationNum = @locnum

    – Dale Burrell
    Dec 31 '18 at 21:14











  • @DaleBurrell I've been looking over the link there and I don't understand how to get the tempPersonID passed down to the @passwordQuery variable. I've not worked with dynamic sql before. Can you provide the application for this situation?

    – Bobby Zimmerman
    Dec 31 '18 at 21:50



















  • The only way to do that us using dynamic sql docs.microsoft.com/en-us/sql/relational-databases/…. Also you know you can assign a variable directly in a select e.g. select @passwordQuery = passwordQuery from location where locationNum = @locnum

    – Dale Burrell
    Dec 31 '18 at 21:14











  • @DaleBurrell I've been looking over the link there and I don't understand how to get the tempPersonID passed down to the @passwordQuery variable. I've not worked with dynamic sql before. Can you provide the application for this situation?

    – Bobby Zimmerman
    Dec 31 '18 at 21:50

















The only way to do that us using dynamic sql docs.microsoft.com/en-us/sql/relational-databases/…. Also you know you can assign a variable directly in a select e.g. select @passwordQuery = passwordQuery from location where locationNum = @locnum

– Dale Burrell
Dec 31 '18 at 21:14





The only way to do that us using dynamic sql docs.microsoft.com/en-us/sql/relational-databases/…. Also you know you can assign a variable directly in a select e.g. select @passwordQuery = passwordQuery from location where locationNum = @locnum

– Dale Burrell
Dec 31 '18 at 21:14













@DaleBurrell I've been looking over the link there and I don't understand how to get the tempPersonID passed down to the @passwordQuery variable. I've not worked with dynamic sql before. Can you provide the application for this situation?

– Bobby Zimmerman
Dec 31 '18 at 21:50





@DaleBurrell I've been looking over the link there and I don't understand how to get the tempPersonID passed down to the @passwordQuery variable. I've not worked with dynamic sql before. Can you provide the application for this situation?

– Bobby Zimmerman
Dec 31 '18 at 21:50












2 Answers
2






active

oldest

votes


















1














Firstly, as you are updating the record that you are selecting the information from you only need the following as your @passwordQuery:



left(firstname,1) + left(lastname,1)  + [custom]


Then the following code takes your code and adapts it for dynamic SQL:



declare @locnum int = 032, @tempPersonID int = 10008, @passwordQuery varchar(max), @sql nvarchar(max), @params nvarchar(max);

select @passwordQuery = passwordQuery from [location] where locationNum = @locnum;

-- select @passwordQuery

set @sql = 'update tempPerson set [password] = ' + @passwordQuery
+ ' where tempPersonID = @tempPersonID';
set @params = '@tempPersonID int';

execute sp_executesql @sql, @params, @tempPersonID = @tempPersonID;

select *
from tempPerson





share|improve this answer































    0














    You Can Execute Dynamic SQL Statements with parameter using either the EXEC or sp_ExecuteSQL Statements.



    In your Case, The sp_ExecuteSQLseems more suitable. The Syntax for the same is as below



    sp_executesql [ @stmt = ] statement  
    [
    { , [ @params = ] N'@parameter_name data_type [ OUT | OUTPUT ][ ,...n ]' }
    { , [ @param1 = ] 'value1' [ ,...n ] }
    ]


    So Your Entire Script can be re-written as below :



    DECLARE @locnum INT
    @tempPersonID INT,
    @passwordQuery VARCHAR(MAX),
    @params VARCHAR(500),
    @ParamOut VARCHAR(500)

    SELECT
    @locnum = 032,
    @tempPersonID = 10008,
    @params = N'@tempPersonID INT,@MyPwd VARCHAR(500) OUTPUT'

    SELECT
    @passwordQuery = 'SET @MyPwd = ('+passwordQuery+')'
    FROM Location
    WHERE LocationNum = @locnum


    sp_ExecuteSQL(@passwordQuery,@params,@tempPersonID,@MyPwd = @ParamOut OUTPUT )

    UPDATE tempPerson
    SET
    [password] = @ParamOut
    WHERE tempPersonID = @tempPersonID

    select *
    from tempPerson





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53991422%2fhow-can-i-pass-a-parameter-to-a-parameterized-query%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Firstly, as you are updating the record that you are selecting the information from you only need the following as your @passwordQuery:



      left(firstname,1) + left(lastname,1)  + [custom]


      Then the following code takes your code and adapts it for dynamic SQL:



      declare @locnum int = 032, @tempPersonID int = 10008, @passwordQuery varchar(max), @sql nvarchar(max), @params nvarchar(max);

      select @passwordQuery = passwordQuery from [location] where locationNum = @locnum;

      -- select @passwordQuery

      set @sql = 'update tempPerson set [password] = ' + @passwordQuery
      + ' where tempPersonID = @tempPersonID';
      set @params = '@tempPersonID int';

      execute sp_executesql @sql, @params, @tempPersonID = @tempPersonID;

      select *
      from tempPerson





      share|improve this answer




























        1














        Firstly, as you are updating the record that you are selecting the information from you only need the following as your @passwordQuery:



        left(firstname,1) + left(lastname,1)  + [custom]


        Then the following code takes your code and adapts it for dynamic SQL:



        declare @locnum int = 032, @tempPersonID int = 10008, @passwordQuery varchar(max), @sql nvarchar(max), @params nvarchar(max);

        select @passwordQuery = passwordQuery from [location] where locationNum = @locnum;

        -- select @passwordQuery

        set @sql = 'update tempPerson set [password] = ' + @passwordQuery
        + ' where tempPersonID = @tempPersonID';
        set @params = '@tempPersonID int';

        execute sp_executesql @sql, @params, @tempPersonID = @tempPersonID;

        select *
        from tempPerson





        share|improve this answer


























          1












          1








          1







          Firstly, as you are updating the record that you are selecting the information from you only need the following as your @passwordQuery:



          left(firstname,1) + left(lastname,1)  + [custom]


          Then the following code takes your code and adapts it for dynamic SQL:



          declare @locnum int = 032, @tempPersonID int = 10008, @passwordQuery varchar(max), @sql nvarchar(max), @params nvarchar(max);

          select @passwordQuery = passwordQuery from [location] where locationNum = @locnum;

          -- select @passwordQuery

          set @sql = 'update tempPerson set [password] = ' + @passwordQuery
          + ' where tempPersonID = @tempPersonID';
          set @params = '@tempPersonID int';

          execute sp_executesql @sql, @params, @tempPersonID = @tempPersonID;

          select *
          from tempPerson





          share|improve this answer













          Firstly, as you are updating the record that you are selecting the information from you only need the following as your @passwordQuery:



          left(firstname,1) + left(lastname,1)  + [custom]


          Then the following code takes your code and adapts it for dynamic SQL:



          declare @locnum int = 032, @tempPersonID int = 10008, @passwordQuery varchar(max), @sql nvarchar(max), @params nvarchar(max);

          select @passwordQuery = passwordQuery from [location] where locationNum = @locnum;

          -- select @passwordQuery

          set @sql = 'update tempPerson set [password] = ' + @passwordQuery
          + ' where tempPersonID = @tempPersonID';
          set @params = '@tempPersonID int';

          execute sp_executesql @sql, @params, @tempPersonID = @tempPersonID;

          select *
          from tempPerson






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 31 '18 at 21:59









          Dale BurrellDale Burrell

          3,22032551




          3,22032551

























              0














              You Can Execute Dynamic SQL Statements with parameter using either the EXEC or sp_ExecuteSQL Statements.



              In your Case, The sp_ExecuteSQLseems more suitable. The Syntax for the same is as below



              sp_executesql [ @stmt = ] statement  
              [
              { , [ @params = ] N'@parameter_name data_type [ OUT | OUTPUT ][ ,...n ]' }
              { , [ @param1 = ] 'value1' [ ,...n ] }
              ]


              So Your Entire Script can be re-written as below :



              DECLARE @locnum INT
              @tempPersonID INT,
              @passwordQuery VARCHAR(MAX),
              @params VARCHAR(500),
              @ParamOut VARCHAR(500)

              SELECT
              @locnum = 032,
              @tempPersonID = 10008,
              @params = N'@tempPersonID INT,@MyPwd VARCHAR(500) OUTPUT'

              SELECT
              @passwordQuery = 'SET @MyPwd = ('+passwordQuery+')'
              FROM Location
              WHERE LocationNum = @locnum


              sp_ExecuteSQL(@passwordQuery,@params,@tempPersonID,@MyPwd = @ParamOut OUTPUT )

              UPDATE tempPerson
              SET
              [password] = @ParamOut
              WHERE tempPersonID = @tempPersonID

              select *
              from tempPerson





              share|improve this answer




























                0














                You Can Execute Dynamic SQL Statements with parameter using either the EXEC or sp_ExecuteSQL Statements.



                In your Case, The sp_ExecuteSQLseems more suitable. The Syntax for the same is as below



                sp_executesql [ @stmt = ] statement  
                [
                { , [ @params = ] N'@parameter_name data_type [ OUT | OUTPUT ][ ,...n ]' }
                { , [ @param1 = ] 'value1' [ ,...n ] }
                ]


                So Your Entire Script can be re-written as below :



                DECLARE @locnum INT
                @tempPersonID INT,
                @passwordQuery VARCHAR(MAX),
                @params VARCHAR(500),
                @ParamOut VARCHAR(500)

                SELECT
                @locnum = 032,
                @tempPersonID = 10008,
                @params = N'@tempPersonID INT,@MyPwd VARCHAR(500) OUTPUT'

                SELECT
                @passwordQuery = 'SET @MyPwd = ('+passwordQuery+')'
                FROM Location
                WHERE LocationNum = @locnum


                sp_ExecuteSQL(@passwordQuery,@params,@tempPersonID,@MyPwd = @ParamOut OUTPUT )

                UPDATE tempPerson
                SET
                [password] = @ParamOut
                WHERE tempPersonID = @tempPersonID

                select *
                from tempPerson





                share|improve this answer


























                  0












                  0








                  0







                  You Can Execute Dynamic SQL Statements with parameter using either the EXEC or sp_ExecuteSQL Statements.



                  In your Case, The sp_ExecuteSQLseems more suitable. The Syntax for the same is as below



                  sp_executesql [ @stmt = ] statement  
                  [
                  { , [ @params = ] N'@parameter_name data_type [ OUT | OUTPUT ][ ,...n ]' }
                  { , [ @param1 = ] 'value1' [ ,...n ] }
                  ]


                  So Your Entire Script can be re-written as below :



                  DECLARE @locnum INT
                  @tempPersonID INT,
                  @passwordQuery VARCHAR(MAX),
                  @params VARCHAR(500),
                  @ParamOut VARCHAR(500)

                  SELECT
                  @locnum = 032,
                  @tempPersonID = 10008,
                  @params = N'@tempPersonID INT,@MyPwd VARCHAR(500) OUTPUT'

                  SELECT
                  @passwordQuery = 'SET @MyPwd = ('+passwordQuery+')'
                  FROM Location
                  WHERE LocationNum = @locnum


                  sp_ExecuteSQL(@passwordQuery,@params,@tempPersonID,@MyPwd = @ParamOut OUTPUT )

                  UPDATE tempPerson
                  SET
                  [password] = @ParamOut
                  WHERE tempPersonID = @tempPersonID

                  select *
                  from tempPerson





                  share|improve this answer













                  You Can Execute Dynamic SQL Statements with parameter using either the EXEC or sp_ExecuteSQL Statements.



                  In your Case, The sp_ExecuteSQLseems more suitable. The Syntax for the same is as below



                  sp_executesql [ @stmt = ] statement  
                  [
                  { , [ @params = ] N'@parameter_name data_type [ OUT | OUTPUT ][ ,...n ]' }
                  { , [ @param1 = ] 'value1' [ ,...n ] }
                  ]


                  So Your Entire Script can be re-written as below :



                  DECLARE @locnum INT
                  @tempPersonID INT,
                  @passwordQuery VARCHAR(MAX),
                  @params VARCHAR(500),
                  @ParamOut VARCHAR(500)

                  SELECT
                  @locnum = 032,
                  @tempPersonID = 10008,
                  @params = N'@tempPersonID INT,@MyPwd VARCHAR(500) OUTPUT'

                  SELECT
                  @passwordQuery = 'SET @MyPwd = ('+passwordQuery+')'
                  FROM Location
                  WHERE LocationNum = @locnum


                  sp_ExecuteSQL(@passwordQuery,@params,@tempPersonID,@MyPwd = @ParamOut OUTPUT )

                  UPDATE tempPerson
                  SET
                  [password] = @ParamOut
                  WHERE tempPersonID = @tempPersonID

                  select *
                  from tempPerson






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 1 at 6:01









                  Jayasurya SatheeshJayasurya Satheesh

                  5,9633926




                  5,9633926






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53991422%2fhow-can-i-pass-a-parameter-to-a-parameterized-query%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas