How to use SELECT parameters assignments in MySQL queries?












3














I've been migrating MSSQL DB into MySQL and facing a syntax I cannot find anywhere and need this help.
All "MySQL parameters" question deals with "select * from mytable where id = @id" but I'm looking for the below syntax:



SELECT @MyParam = MyValue FROM MyTable


While "SELECT MyValue FROM MyTable" returns values, running the above @MyParam query returns NULLs.



From previous answers I see that as opposed to MSSQL, in MySQL the parameters aren't predefined with any datatype.



What am I missing here?



THX










share|improve this question





























    3














    I've been migrating MSSQL DB into MySQL and facing a syntax I cannot find anywhere and need this help.
    All "MySQL parameters" question deals with "select * from mytable where id = @id" but I'm looking for the below syntax:



    SELECT @MyParam = MyValue FROM MyTable


    While "SELECT MyValue FROM MyTable" returns values, running the above @MyParam query returns NULLs.



    From previous answers I see that as opposed to MSSQL, in MySQL the parameters aren't predefined with any datatype.



    What am I missing here?



    THX










    share|improve this question



























      3












      3








      3







      I've been migrating MSSQL DB into MySQL and facing a syntax I cannot find anywhere and need this help.
      All "MySQL parameters" question deals with "select * from mytable where id = @id" but I'm looking for the below syntax:



      SELECT @MyParam = MyValue FROM MyTable


      While "SELECT MyValue FROM MyTable" returns values, running the above @MyParam query returns NULLs.



      From previous answers I see that as opposed to MSSQL, in MySQL the parameters aren't predefined with any datatype.



      What am I missing here?



      THX










      share|improve this question















      I've been migrating MSSQL DB into MySQL and facing a syntax I cannot find anywhere and need this help.
      All "MySQL parameters" question deals with "select * from mytable where id = @id" but I'm looking for the below syntax:



      SELECT @MyParam = MyValue FROM MyTable


      While "SELECT MyValue FROM MyTable" returns values, running the above @MyParam query returns NULLs.



      From previous answers I see that as opposed to MSSQL, in MySQL the parameters aren't predefined with any datatype.



      What am I missing here?



      THX







      mysql parameters






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 10:51









      Rory McCrossan

      241k29206245




      241k29206245










      asked Nov 13 '18 at 10:45









      Dave GahanDave Gahan

      1169




      1169
























          2 Answers
          2






          active

          oldest

          votes


















          2














          You need to use := operator instead of =, when you are assigning value to a user-defined variable in the SELECT clause. Former is assignment operator; while the latter is comparison operator.



          SELECT @MyParam := MyValue FROM MyTable


          However, if you are using SET clause, then you can use either = or :=. From docs:




          For SET, either = or := can be used as the assignment operator.




          More from the docs:




          When making an assignment in this way (using SELECT), you must use
          := as the assignment operator; = is treated as the comparison operator
          in statements other than SET.







          share|improve this answer





















          • Excellent! That did the trick. I should have thought on this (though I'm MSSQL fan forced to move to MySQL..) Thx so much.
            – Dave Gahan
            Nov 13 '18 at 12:11








          • 1




            The := format with SELCECT will cause extra result set from the procedure. Use SELECT col INTO vVariable syntax instead.
            – slaakso
            Nov 13 '18 at 12:23



















          1














          When converting MS SQL Server procedures to MySQL procedures, you better use local variables declared with DECLARE.



          The @var variables are user-defined variable and the scope is the process. So if your procedures call other procedures, the variable value may change whereas local variables will not. Local variables also have a defined type.



          To convert MS SQL Server syntax



          SELECT @MyParam = MyValue FROM MyTable


          into MySQL, usee:



          DECLARE vMyParam int;
          SELECT MyValue INTO vMyParam FROM MyTable;


          The format:



          SELECT @MyParam := MyValue FROM MyTable


          will cause an extra result set from the procedure so that is probably something you do not want.



          You can use the := format when you use SET-command where you explicitly set a value for a variable.



          SET vMyParam = 10;





          share|improve this answer























          • thanks for clarifying this. Just tested it and it worked perfectly. Thx
            – Dave Gahan
            Nov 13 '18 at 12:36











          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%2f53279273%2fhow-to-use-select-parameters-assignments-in-mysql-queries%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









          2














          You need to use := operator instead of =, when you are assigning value to a user-defined variable in the SELECT clause. Former is assignment operator; while the latter is comparison operator.



          SELECT @MyParam := MyValue FROM MyTable


          However, if you are using SET clause, then you can use either = or :=. From docs:




          For SET, either = or := can be used as the assignment operator.




          More from the docs:




          When making an assignment in this way (using SELECT), you must use
          := as the assignment operator; = is treated as the comparison operator
          in statements other than SET.







          share|improve this answer





















          • Excellent! That did the trick. I should have thought on this (though I'm MSSQL fan forced to move to MySQL..) Thx so much.
            – Dave Gahan
            Nov 13 '18 at 12:11








          • 1




            The := format with SELCECT will cause extra result set from the procedure. Use SELECT col INTO vVariable syntax instead.
            – slaakso
            Nov 13 '18 at 12:23
















          2














          You need to use := operator instead of =, when you are assigning value to a user-defined variable in the SELECT clause. Former is assignment operator; while the latter is comparison operator.



          SELECT @MyParam := MyValue FROM MyTable


          However, if you are using SET clause, then you can use either = or :=. From docs:




          For SET, either = or := can be used as the assignment operator.




          More from the docs:




          When making an assignment in this way (using SELECT), you must use
          := as the assignment operator; = is treated as the comparison operator
          in statements other than SET.







          share|improve this answer





















          • Excellent! That did the trick. I should have thought on this (though I'm MSSQL fan forced to move to MySQL..) Thx so much.
            – Dave Gahan
            Nov 13 '18 at 12:11








          • 1




            The := format with SELCECT will cause extra result set from the procedure. Use SELECT col INTO vVariable syntax instead.
            – slaakso
            Nov 13 '18 at 12:23














          2












          2








          2






          You need to use := operator instead of =, when you are assigning value to a user-defined variable in the SELECT clause. Former is assignment operator; while the latter is comparison operator.



          SELECT @MyParam := MyValue FROM MyTable


          However, if you are using SET clause, then you can use either = or :=. From docs:




          For SET, either = or := can be used as the assignment operator.




          More from the docs:




          When making an assignment in this way (using SELECT), you must use
          := as the assignment operator; = is treated as the comparison operator
          in statements other than SET.







          share|improve this answer












          You need to use := operator instead of =, when you are assigning value to a user-defined variable in the SELECT clause. Former is assignment operator; while the latter is comparison operator.



          SELECT @MyParam := MyValue FROM MyTable


          However, if you are using SET clause, then you can use either = or :=. From docs:




          For SET, either = or := can be used as the assignment operator.




          More from the docs:




          When making an assignment in this way (using SELECT), you must use
          := as the assignment operator; = is treated as the comparison operator
          in statements other than SET.








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 10:46









          Madhur BhaiyaMadhur Bhaiya

          19.5k62236




          19.5k62236












          • Excellent! That did the trick. I should have thought on this (though I'm MSSQL fan forced to move to MySQL..) Thx so much.
            – Dave Gahan
            Nov 13 '18 at 12:11








          • 1




            The := format with SELCECT will cause extra result set from the procedure. Use SELECT col INTO vVariable syntax instead.
            – slaakso
            Nov 13 '18 at 12:23


















          • Excellent! That did the trick. I should have thought on this (though I'm MSSQL fan forced to move to MySQL..) Thx so much.
            – Dave Gahan
            Nov 13 '18 at 12:11








          • 1




            The := format with SELCECT will cause extra result set from the procedure. Use SELECT col INTO vVariable syntax instead.
            – slaakso
            Nov 13 '18 at 12:23
















          Excellent! That did the trick. I should have thought on this (though I'm MSSQL fan forced to move to MySQL..) Thx so much.
          – Dave Gahan
          Nov 13 '18 at 12:11






          Excellent! That did the trick. I should have thought on this (though I'm MSSQL fan forced to move to MySQL..) Thx so much.
          – Dave Gahan
          Nov 13 '18 at 12:11






          1




          1




          The := format with SELCECT will cause extra result set from the procedure. Use SELECT col INTO vVariable syntax instead.
          – slaakso
          Nov 13 '18 at 12:23




          The := format with SELCECT will cause extra result set from the procedure. Use SELECT col INTO vVariable syntax instead.
          – slaakso
          Nov 13 '18 at 12:23













          1














          When converting MS SQL Server procedures to MySQL procedures, you better use local variables declared with DECLARE.



          The @var variables are user-defined variable and the scope is the process. So if your procedures call other procedures, the variable value may change whereas local variables will not. Local variables also have a defined type.



          To convert MS SQL Server syntax



          SELECT @MyParam = MyValue FROM MyTable


          into MySQL, usee:



          DECLARE vMyParam int;
          SELECT MyValue INTO vMyParam FROM MyTable;


          The format:



          SELECT @MyParam := MyValue FROM MyTable


          will cause an extra result set from the procedure so that is probably something you do not want.



          You can use the := format when you use SET-command where you explicitly set a value for a variable.



          SET vMyParam = 10;





          share|improve this answer























          • thanks for clarifying this. Just tested it and it worked perfectly. Thx
            – Dave Gahan
            Nov 13 '18 at 12:36
















          1














          When converting MS SQL Server procedures to MySQL procedures, you better use local variables declared with DECLARE.



          The @var variables are user-defined variable and the scope is the process. So if your procedures call other procedures, the variable value may change whereas local variables will not. Local variables also have a defined type.



          To convert MS SQL Server syntax



          SELECT @MyParam = MyValue FROM MyTable


          into MySQL, usee:



          DECLARE vMyParam int;
          SELECT MyValue INTO vMyParam FROM MyTable;


          The format:



          SELECT @MyParam := MyValue FROM MyTable


          will cause an extra result set from the procedure so that is probably something you do not want.



          You can use the := format when you use SET-command where you explicitly set a value for a variable.



          SET vMyParam = 10;





          share|improve this answer























          • thanks for clarifying this. Just tested it and it worked perfectly. Thx
            – Dave Gahan
            Nov 13 '18 at 12:36














          1












          1








          1






          When converting MS SQL Server procedures to MySQL procedures, you better use local variables declared with DECLARE.



          The @var variables are user-defined variable and the scope is the process. So if your procedures call other procedures, the variable value may change whereas local variables will not. Local variables also have a defined type.



          To convert MS SQL Server syntax



          SELECT @MyParam = MyValue FROM MyTable


          into MySQL, usee:



          DECLARE vMyParam int;
          SELECT MyValue INTO vMyParam FROM MyTable;


          The format:



          SELECT @MyParam := MyValue FROM MyTable


          will cause an extra result set from the procedure so that is probably something you do not want.



          You can use the := format when you use SET-command where you explicitly set a value for a variable.



          SET vMyParam = 10;





          share|improve this answer














          When converting MS SQL Server procedures to MySQL procedures, you better use local variables declared with DECLARE.



          The @var variables are user-defined variable and the scope is the process. So if your procedures call other procedures, the variable value may change whereas local variables will not. Local variables also have a defined type.



          To convert MS SQL Server syntax



          SELECT @MyParam = MyValue FROM MyTable


          into MySQL, usee:



          DECLARE vMyParam int;
          SELECT MyValue INTO vMyParam FROM MyTable;


          The format:



          SELECT @MyParam := MyValue FROM MyTable


          will cause an extra result set from the procedure so that is probably something you do not want.



          You can use the := format when you use SET-command where you explicitly set a value for a variable.



          SET vMyParam = 10;






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 12:28

























          answered Nov 13 '18 at 12:21









          slaaksoslaakso

          2,946818




          2,946818












          • thanks for clarifying this. Just tested it and it worked perfectly. Thx
            – Dave Gahan
            Nov 13 '18 at 12:36


















          • thanks for clarifying this. Just tested it and it worked perfectly. Thx
            – Dave Gahan
            Nov 13 '18 at 12:36
















          thanks for clarifying this. Just tested it and it worked perfectly. Thx
          – Dave Gahan
          Nov 13 '18 at 12:36




          thanks for clarifying this. Just tested it and it worked perfectly. Thx
          – Dave Gahan
          Nov 13 '18 at 12:36


















          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%2f53279273%2fhow-to-use-select-parameters-assignments-in-mysql-queries%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

          Bressuire

          Vorschmack

          Quarantine