How to insert asc/desc in addParameter for sorting in SQL query



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have this code



dBCommand.AddParameter("@SORT", "asc");



Below is my query.



 SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME @SORT


How can I place an asc and desc in AddParameter? Because it throws an error I guess because it is not in the right format?










share|improve this question






















  • You can't do that - modify query instead.

    – Reniuz
    Nov 16 '18 at 14:41












  • You would have to use dynamic sql to do this.

    – Sean Lange
    Nov 16 '18 at 14:42











  • Any other workaround that is close to that code? I just wanna replace that @SORT. because I wanna order by asc and desc.

    – beginnerlaravelvue
    Nov 16 '18 at 14:42











  • Maybe I should make 2 queries that have asc and desc.

    – beginnerlaravelvue
    Nov 16 '18 at 14:44






  • 1





    You could use two queries and an IF statement. But be careful, that can lead some performance issues. sqlinthewild.co.za/index.php/2009/09/15/…

    – Sean Lange
    Nov 16 '18 at 14:44


















1















I have this code



dBCommand.AddParameter("@SORT", "asc");



Below is my query.



 SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME @SORT


How can I place an asc and desc in AddParameter? Because it throws an error I guess because it is not in the right format?










share|improve this question






















  • You can't do that - modify query instead.

    – Reniuz
    Nov 16 '18 at 14:41












  • You would have to use dynamic sql to do this.

    – Sean Lange
    Nov 16 '18 at 14:42











  • Any other workaround that is close to that code? I just wanna replace that @SORT. because I wanna order by asc and desc.

    – beginnerlaravelvue
    Nov 16 '18 at 14:42











  • Maybe I should make 2 queries that have asc and desc.

    – beginnerlaravelvue
    Nov 16 '18 at 14:44






  • 1





    You could use two queries and an IF statement. But be careful, that can lead some performance issues. sqlinthewild.co.za/index.php/2009/09/15/…

    – Sean Lange
    Nov 16 '18 at 14:44














1












1








1








I have this code



dBCommand.AddParameter("@SORT", "asc");



Below is my query.



 SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME @SORT


How can I place an asc and desc in AddParameter? Because it throws an error I guess because it is not in the right format?










share|improve this question














I have this code



dBCommand.AddParameter("@SORT", "asc");



Below is my query.



 SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME @SORT


How can I place an asc and desc in AddParameter? Because it throws an error I guess because it is not in the right format?







c# sql sql-server






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 14:39









beginnerlaravelvuebeginnerlaravelvue

528




528












  • You can't do that - modify query instead.

    – Reniuz
    Nov 16 '18 at 14:41












  • You would have to use dynamic sql to do this.

    – Sean Lange
    Nov 16 '18 at 14:42











  • Any other workaround that is close to that code? I just wanna replace that @SORT. because I wanna order by asc and desc.

    – beginnerlaravelvue
    Nov 16 '18 at 14:42











  • Maybe I should make 2 queries that have asc and desc.

    – beginnerlaravelvue
    Nov 16 '18 at 14:44






  • 1





    You could use two queries and an IF statement. But be careful, that can lead some performance issues. sqlinthewild.co.za/index.php/2009/09/15/…

    – Sean Lange
    Nov 16 '18 at 14:44


















  • You can't do that - modify query instead.

    – Reniuz
    Nov 16 '18 at 14:41












  • You would have to use dynamic sql to do this.

    – Sean Lange
    Nov 16 '18 at 14:42











  • Any other workaround that is close to that code? I just wanna replace that @SORT. because I wanna order by asc and desc.

    – beginnerlaravelvue
    Nov 16 '18 at 14:42











  • Maybe I should make 2 queries that have asc and desc.

    – beginnerlaravelvue
    Nov 16 '18 at 14:44






  • 1





    You could use two queries and an IF statement. But be careful, that can lead some performance issues. sqlinthewild.co.za/index.php/2009/09/15/…

    – Sean Lange
    Nov 16 '18 at 14:44

















You can't do that - modify query instead.

– Reniuz
Nov 16 '18 at 14:41






You can't do that - modify query instead.

– Reniuz
Nov 16 '18 at 14:41














You would have to use dynamic sql to do this.

– Sean Lange
Nov 16 '18 at 14:42





You would have to use dynamic sql to do this.

– Sean Lange
Nov 16 '18 at 14:42













Any other workaround that is close to that code? I just wanna replace that @SORT. because I wanna order by asc and desc.

– beginnerlaravelvue
Nov 16 '18 at 14:42





Any other workaround that is close to that code? I just wanna replace that @SORT. because I wanna order by asc and desc.

– beginnerlaravelvue
Nov 16 '18 at 14:42













Maybe I should make 2 queries that have asc and desc.

– beginnerlaravelvue
Nov 16 '18 at 14:44





Maybe I should make 2 queries that have asc and desc.

– beginnerlaravelvue
Nov 16 '18 at 14:44




1




1





You could use two queries and an IF statement. But be careful, that can lead some performance issues. sqlinthewild.co.za/index.php/2009/09/15/…

– Sean Lange
Nov 16 '18 at 14:44






You could use two queries and an IF statement. But be careful, that can lead some performance issues. sqlinthewild.co.za/index.php/2009/09/15/…

– Sean Lange
Nov 16 '18 at 14:44













4 Answers
4






active

oldest

votes


















1














Within the query, you have an if statement which orders the results based on the @sort parameter value.



something like:



if @sort = 'ascending'
select ... order by transtime asc
else
select ... order by transtime desc





share|improve this answer

























  • it doesn't look like a stored proc to me... but: the exact same approach could be used with/without stored proc

    – Marc Gravell
    Nov 16 '18 at 14:46











  • @MarcGravell yes that makes sense, thanks.

    – user1666620
    Nov 16 '18 at 14:49


















1














The sort direction is part of the query itself and cannot be parameterized. You would need to either construct the query to embed ASC or DESC into the SQL (essentially string.Format or concatenation, but just of the ASC/DESC part - not of the input values), or have 2 different completed queries that you issue.






share|improve this answer























  • Alright. Thanks!

    – beginnerlaravelvue
    Nov 16 '18 at 14:46


















1














It is unfortunately not possible. You can set if-else structure to handle this issue like:



if(orderParameter == "asc")

SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME ASC

else

SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME DESC



Or you can use String.Format(..) to set your query like



string orderParameter = "asc";
string sql = String.Format("SELECT TOP 1 RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME]
FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME 0",orderParameter);





share|improve this answer






























    1














    If by chance TRANSTIME has a datatype of datetime you can convert to a float and then apply a factor.



    HOWEVER, I suspect a conditional approach would be more performant.



     SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
    WHERE TRANSDATE = @TRANSDATE
    AND RECEIPTID != ''
    AND STORE = @STORE
    AND TERMINAL = @TERMINAL
    ORDER BY convert(float,TRANSTIME) * IIF(@Sort='asc',1,-1)





    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%2f53339959%2fhow-to-insert-asc-desc-in-addparameter-for-sorting-in-sql-query%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Within the query, you have an if statement which orders the results based on the @sort parameter value.



      something like:



      if @sort = 'ascending'
      select ... order by transtime asc
      else
      select ... order by transtime desc





      share|improve this answer

























      • it doesn't look like a stored proc to me... but: the exact same approach could be used with/without stored proc

        – Marc Gravell
        Nov 16 '18 at 14:46











      • @MarcGravell yes that makes sense, thanks.

        – user1666620
        Nov 16 '18 at 14:49















      1














      Within the query, you have an if statement which orders the results based on the @sort parameter value.



      something like:



      if @sort = 'ascending'
      select ... order by transtime asc
      else
      select ... order by transtime desc





      share|improve this answer

























      • it doesn't look like a stored proc to me... but: the exact same approach could be used with/without stored proc

        – Marc Gravell
        Nov 16 '18 at 14:46











      • @MarcGravell yes that makes sense, thanks.

        – user1666620
        Nov 16 '18 at 14:49













      1












      1








      1







      Within the query, you have an if statement which orders the results based on the @sort parameter value.



      something like:



      if @sort = 'ascending'
      select ... order by transtime asc
      else
      select ... order by transtime desc





      share|improve this answer















      Within the query, you have an if statement which orders the results based on the @sort parameter value.



      something like:



      if @sort = 'ascending'
      select ... order by transtime asc
      else
      select ... order by transtime desc






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 16 '18 at 14:46

























      answered Nov 16 '18 at 14:45









      user1666620user1666620

      4,3501224




      4,3501224












      • it doesn't look like a stored proc to me... but: the exact same approach could be used with/without stored proc

        – Marc Gravell
        Nov 16 '18 at 14:46











      • @MarcGravell yes that makes sense, thanks.

        – user1666620
        Nov 16 '18 at 14:49

















      • it doesn't look like a stored proc to me... but: the exact same approach could be used with/without stored proc

        – Marc Gravell
        Nov 16 '18 at 14:46











      • @MarcGravell yes that makes sense, thanks.

        – user1666620
        Nov 16 '18 at 14:49
















      it doesn't look like a stored proc to me... but: the exact same approach could be used with/without stored proc

      – Marc Gravell
      Nov 16 '18 at 14:46





      it doesn't look like a stored proc to me... but: the exact same approach could be used with/without stored proc

      – Marc Gravell
      Nov 16 '18 at 14:46













      @MarcGravell yes that makes sense, thanks.

      – user1666620
      Nov 16 '18 at 14:49





      @MarcGravell yes that makes sense, thanks.

      – user1666620
      Nov 16 '18 at 14:49













      1














      The sort direction is part of the query itself and cannot be parameterized. You would need to either construct the query to embed ASC or DESC into the SQL (essentially string.Format or concatenation, but just of the ASC/DESC part - not of the input values), or have 2 different completed queries that you issue.






      share|improve this answer























      • Alright. Thanks!

        – beginnerlaravelvue
        Nov 16 '18 at 14:46















      1














      The sort direction is part of the query itself and cannot be parameterized. You would need to either construct the query to embed ASC or DESC into the SQL (essentially string.Format or concatenation, but just of the ASC/DESC part - not of the input values), or have 2 different completed queries that you issue.






      share|improve this answer























      • Alright. Thanks!

        – beginnerlaravelvue
        Nov 16 '18 at 14:46













      1












      1








      1







      The sort direction is part of the query itself and cannot be parameterized. You would need to either construct the query to embed ASC or DESC into the SQL (essentially string.Format or concatenation, but just of the ASC/DESC part - not of the input values), or have 2 different completed queries that you issue.






      share|improve this answer













      The sort direction is part of the query itself and cannot be parameterized. You would need to either construct the query to embed ASC or DESC into the SQL (essentially string.Format or concatenation, but just of the ASC/DESC part - not of the input values), or have 2 different completed queries that you issue.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 16 '18 at 14:44









      Marc GravellMarc Gravell

      795k19821622567




      795k19821622567












      • Alright. Thanks!

        – beginnerlaravelvue
        Nov 16 '18 at 14:46

















      • Alright. Thanks!

        – beginnerlaravelvue
        Nov 16 '18 at 14:46
















      Alright. Thanks!

      – beginnerlaravelvue
      Nov 16 '18 at 14:46





      Alright. Thanks!

      – beginnerlaravelvue
      Nov 16 '18 at 14:46











      1














      It is unfortunately not possible. You can set if-else structure to handle this issue like:



      if(orderParameter == "asc")

      SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
      WHERE TRANSDATE = @TRANSDATE
      AND RECEIPTID != ''
      AND STORE = @STORE
      AND TERMINAL = @TERMINAL
      ORDER BY TRANSTIME ASC

      else

      SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
      WHERE TRANSDATE = @TRANSDATE
      AND RECEIPTID != ''
      AND STORE = @STORE
      AND TERMINAL = @TERMINAL
      ORDER BY TRANSTIME DESC



      Or you can use String.Format(..) to set your query like



      string orderParameter = "asc";
      string sql = String.Format("SELECT TOP 1 RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME]
      FROM RetailTransactionTable
      WHERE TRANSDATE = @TRANSDATE
      AND RECEIPTID != ''
      AND STORE = @STORE
      AND TERMINAL = @TERMINAL
      ORDER BY TRANSTIME 0",orderParameter);





      share|improve this answer



























        1














        It is unfortunately not possible. You can set if-else structure to handle this issue like:



        if(orderParameter == "asc")

        SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
        WHERE TRANSDATE = @TRANSDATE
        AND RECEIPTID != ''
        AND STORE = @STORE
        AND TERMINAL = @TERMINAL
        ORDER BY TRANSTIME ASC

        else

        SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
        WHERE TRANSDATE = @TRANSDATE
        AND RECEIPTID != ''
        AND STORE = @STORE
        AND TERMINAL = @TERMINAL
        ORDER BY TRANSTIME DESC



        Or you can use String.Format(..) to set your query like



        string orderParameter = "asc";
        string sql = String.Format("SELECT TOP 1 RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME]
        FROM RetailTransactionTable
        WHERE TRANSDATE = @TRANSDATE
        AND RECEIPTID != ''
        AND STORE = @STORE
        AND TERMINAL = @TERMINAL
        ORDER BY TRANSTIME 0",orderParameter);





        share|improve this answer

























          1












          1








          1







          It is unfortunately not possible. You can set if-else structure to handle this issue like:



          if(orderParameter == "asc")

          SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
          WHERE TRANSDATE = @TRANSDATE
          AND RECEIPTID != ''
          AND STORE = @STORE
          AND TERMINAL = @TERMINAL
          ORDER BY TRANSTIME ASC

          else

          SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
          WHERE TRANSDATE = @TRANSDATE
          AND RECEIPTID != ''
          AND STORE = @STORE
          AND TERMINAL = @TERMINAL
          ORDER BY TRANSTIME DESC



          Or you can use String.Format(..) to set your query like



          string orderParameter = "asc";
          string sql = String.Format("SELECT TOP 1 RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME]
          FROM RetailTransactionTable
          WHERE TRANSDATE = @TRANSDATE
          AND RECEIPTID != ''
          AND STORE = @STORE
          AND TERMINAL = @TERMINAL
          ORDER BY TRANSTIME 0",orderParameter);





          share|improve this answer













          It is unfortunately not possible. You can set if-else structure to handle this issue like:



          if(orderParameter == "asc")

          SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
          WHERE TRANSDATE = @TRANSDATE
          AND RECEIPTID != ''
          AND STORE = @STORE
          AND TERMINAL = @TERMINAL
          ORDER BY TRANSTIME ASC

          else

          SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
          WHERE TRANSDATE = @TRANSDATE
          AND RECEIPTID != ''
          AND STORE = @STORE
          AND TERMINAL = @TERMINAL
          ORDER BY TRANSTIME DESC



          Or you can use String.Format(..) to set your query like



          string orderParameter = "asc";
          string sql = String.Format("SELECT TOP 1 RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME]
          FROM RetailTransactionTable
          WHERE TRANSDATE = @TRANSDATE
          AND RECEIPTID != ''
          AND STORE = @STORE
          AND TERMINAL = @TERMINAL
          ORDER BY TRANSTIME 0",orderParameter);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 14:48









          Eray BalkanliEray Balkanli

          4,58852347




          4,58852347





















              1














              If by chance TRANSTIME has a datatype of datetime you can convert to a float and then apply a factor.



              HOWEVER, I suspect a conditional approach would be more performant.



               SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
              WHERE TRANSDATE = @TRANSDATE
              AND RECEIPTID != ''
              AND STORE = @STORE
              AND TERMINAL = @TERMINAL
              ORDER BY convert(float,TRANSTIME) * IIF(@Sort='asc',1,-1)





              share|improve this answer



























                1














                If by chance TRANSTIME has a datatype of datetime you can convert to a float and then apply a factor.



                HOWEVER, I suspect a conditional approach would be more performant.



                 SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
                WHERE TRANSDATE = @TRANSDATE
                AND RECEIPTID != ''
                AND STORE = @STORE
                AND TERMINAL = @TERMINAL
                ORDER BY convert(float,TRANSTIME) * IIF(@Sort='asc',1,-1)





                share|improve this answer

























                  1












                  1








                  1







                  If by chance TRANSTIME has a datatype of datetime you can convert to a float and then apply a factor.



                  HOWEVER, I suspect a conditional approach would be more performant.



                   SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
                  WHERE TRANSDATE = @TRANSDATE
                  AND RECEIPTID != ''
                  AND STORE = @STORE
                  AND TERMINAL = @TERMINAL
                  ORDER BY convert(float,TRANSTIME) * IIF(@Sort='asc',1,-1)





                  share|improve this answer













                  If by chance TRANSTIME has a datatype of datetime you can convert to a float and then apply a factor.



                  HOWEVER, I suspect a conditional approach would be more performant.



                   SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
                  WHERE TRANSDATE = @TRANSDATE
                  AND RECEIPTID != ''
                  AND STORE = @STORE
                  AND TERMINAL = @TERMINAL
                  ORDER BY convert(float,TRANSTIME) * IIF(@Sort='asc',1,-1)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 14:55









                  John CappellettiJohn Cappelletti

                  47.4k62647




                  47.4k62647



























                      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%2f53339959%2fhow-to-insert-asc-desc-in-addparameter-for-sorting-in-sql-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

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      政党

                      天津地下鉄3号線