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. Order By

Order By

Scheduled Pinned Locked Moved Database
databasexmlquestion
4 Posts 2 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.
  • S Offline
    S Offline
    SatyaKeerthi15
    wrote on last edited by
    #1

    I Have two Tables like Code and Details... In Code(SrNo,Name) Table Records Like A,B,C etc In Details(SrNo,CodeName)Table looks like SrNo A B C 1 0 1 0 2 0 0 0 3 1 0 0 4 0 0 0 5 0 0 1 6 1 1 1 7 0 0 0 If I add another Name in Code Table Like 'D'. Select query for Details table also contain column 'D'. So Select query like this DECLARE @Code NVARCHAR(MAX) SET @Code = (SELECT ('CONVERT(VARCHAR,'+Name+ ')'''+Name+''',') FROM Code ORDER BY SrNo FOR XML PATH('')) SET @Code = SUBSTRING(@Code,0,LEN(@Code)) DECLARE @SQL VARCHAR(MAX) SET @SQL = 'SELECT '+@Code+' FROM Details' EXEC (@SQL) Output:I need to check any '1' in columns and srno in order by without mentioning column names. It has to be like below. How can I do in SqlServer2005 SrNo A B C 6 1 1 1 1 0 1 0 3 1 0 0 5 0 0 1 2 0 0 0 4 0 0 0 7 0 0 0

    R 1 Reply Last reply
    0
    • S SatyaKeerthi15

      I Have two Tables like Code and Details... In Code(SrNo,Name) Table Records Like A,B,C etc In Details(SrNo,CodeName)Table looks like SrNo A B C 1 0 1 0 2 0 0 0 3 1 0 0 4 0 0 0 5 0 0 1 6 1 1 1 7 0 0 0 If I add another Name in Code Table Like 'D'. Select query for Details table also contain column 'D'. So Select query like this DECLARE @Code NVARCHAR(MAX) SET @Code = (SELECT ('CONVERT(VARCHAR,'+Name+ ')'''+Name+''',') FROM Code ORDER BY SrNo FOR XML PATH('')) SET @Code = SUBSTRING(@Code,0,LEN(@Code)) DECLARE @SQL VARCHAR(MAX) SET @SQL = 'SELECT '+@Code+' FROM Details' EXEC (@SQL) Output:I need to check any '1' in columns and srno in order by without mentioning column names. It has to be like below. How can I do in SqlServer2005 SrNo A B C 6 1 1 1 1 0 1 0 3 1 0 0 5 0 0 1 2 0 0 0 4 0 0 0 7 0 0 0

      R Offline
      R Offline
      R Giskard Reventlov
      wrote on last edited by
      #2

      You can refer to columns by ordinal as select foo from bar order by 1, 2.

      "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

      S 1 Reply Last reply
      0
      • R R Giskard Reventlov

        You can refer to columns by ordinal as select foo from bar order by 1, 2.

        "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

        S Offline
        S Offline
        SatyaKeerthi15
        wrote on last edited by
        #3

        you mean to say by Srno. If so its not my requirement.

        R 1 Reply Last reply
        0
        • S SatyaKeerthi15

          you mean to say by Srno. If so its not my requirement.

          R Offline
          R Offline
          R Giskard Reventlov
          wrote on last edited by
          #4

          So elucidate as it did appear that's what you asked for. In any case you can use select foo from bar order by 2 or 3 or 1000

          "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

          modified on Monday, August 2, 2010 6:26 AM

          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