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. Dynamic sort

Dynamic sort

Scheduled Pinned Locked Moved Database
helpdatabasequestion
7 Posts 5 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.
  • V Offline
    V Offline
    vanikanc
    wrote on last edited by
    #1

    Hello, I want to pass the sort as an input parameter to the stored proc. I am constructing a dynamic sql and executing it. The problem is more like a syntax issue, where in I an checking for certain value in sql. ---- Set @sql1 = ' SELECT distinct sd.storeno storeno, sd.storename storename FROM test_table sd where sd.status='N' ORDER by' set @sql1 = @sql1 + ' ' + @sort --------- How would I check for additional conditions in this sql? I tried to enclose N in double still did not work. Many thanks!

    H L J 3 Replies Last reply
    0
    • V vanikanc

      Hello, I want to pass the sort as an input parameter to the stored proc. I am constructing a dynamic sql and executing it. The problem is more like a syntax issue, where in I an checking for certain value in sql. ---- Set @sql1 = ' SELECT distinct sd.storeno storeno, sd.storename storename FROM test_table sd where sd.status='N' ORDER by' set @sql1 = @sql1 + ' ' + @sort --------- How would I check for additional conditions in this sql? I tried to enclose N in double still did not work. Many thanks!

      H Offline
      H Offline
      Herman T Instance
      wrote on last edited by
      #2

      you should use '''N'''

      In Word you can only store 2 bytes. That is why I use Writer.

      V 1 Reply Last reply
      0
      • H Herman T Instance

        you should use '''N'''

        In Word you can only store 2 bytes. That is why I use Writer.

        V Offline
        V Offline
        vanikanc
        wrote on last edited by
        #3

        Thank you for your response. It should be within two single quotes, like ''N''

        C 1 Reply Last reply
        0
        • V vanikanc

          Thank you for your response. It should be within two single quotes, like ''N''

          C Offline
          C Offline
          Corporal Agarn
          wrote on last edited by
          #4

          That would be ''''N'''' try:

          declare @sql1 varchar(2000)
          Set @sql1 = '
          SELECT distinct
          sd.storeno storeno,
          sd.storename storename
          FROM
          test_table sd
          where sd.status=''''N''''
          ORDER by'

          print @sql1

          Output: SELECT distinct sd.storeno storeno, sd.storename storename FROM test_table sd where sd.status=''N'' ORDER by

          1 Reply Last reply
          0
          • V vanikanc

            Hello, I want to pass the sort as an input parameter to the stored proc. I am constructing a dynamic sql and executing it. The problem is more like a syntax issue, where in I an checking for certain value in sql. ---- Set @sql1 = ' SELECT distinct sd.storeno storeno, sd.storename storename FROM test_table sd where sd.status='N' ORDER by' set @sql1 = @sql1 + ' ' + @sort --------- How would I check for additional conditions in this sql? I tried to enclose N in double still did not work. Many thanks!

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            When you wish to embed single quotes within dynamic strings, use two consecutive single quotes, like this:

            Set @sql1 = '
            SELECT distinct
            sd.storeno storeno,
            sd.storename storename
            FROM
            test_table sd
            where sd.status=''N''
            ORDER by'

            set @sql1 = @sql1 + ' ' + @sort

            EDIT: Corrected a mistake.

            modified on Monday, August 1, 2011 2:49 PM

            V 1 Reply Last reply
            0
            • L Lost User

              When you wish to embed single quotes within dynamic strings, use two consecutive single quotes, like this:

              Set @sql1 = '
              SELECT distinct
              sd.storeno storeno,
              sd.storename storename
              FROM
              test_table sd
              where sd.status=''N''
              ORDER by'

              set @sql1 = @sql1 + ' ' + @sort

              EDIT: Corrected a mistake.

              modified on Monday, August 1, 2011 2:49 PM

              V Offline
              V Offline
              vanikanc
              wrote on last edited by
              #6

              I don't know but just the double single quotes worked for me. ''N''

              1 Reply Last reply
              0
              • V vanikanc

                Hello, I want to pass the sort as an input parameter to the stored proc. I am constructing a dynamic sql and executing it. The problem is more like a syntax issue, where in I an checking for certain value in sql. ---- Set @sql1 = ' SELECT distinct sd.storeno storeno, sd.storename storename FROM test_table sd where sd.status='N' ORDER by' set @sql1 = @sql1 + ' ' + @sort --------- How would I check for additional conditions in this sql? I tried to enclose N in double still did not work. Many thanks!

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

                Seems to be some confusion in the back and forth in the answers above. A SQL text literal is expressed like the following example.

                'abc'

                If you want to embed a single tick in the above literal you MUST escape it. The standard SQL (ANSI) idiom for that is as follows (but some databases offer other ways.)

                'a''bc'

                So two single ticks become one. Three ticks is NEVER correct.

                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