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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to do a quick / generic pivot?

How to do a quick / generic pivot?

Scheduled Pinned Locked Moved Database
databasesql-servertutorialquestion
2 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    In MS SQL 2008 I'm often doing big SELECT * statements from a couple tables looking for data in various fields, and have to scroll sideways in the SSMS window. What I'd like to do is for every field in whatever table I'm selecting, to pivot so its name goes into FieldName, and its value into FieldValue. Then I can use sorts and quickly find what I'm looking for. I've seen some articles on PIVOTs but they are almost always hardcoded for a specific table, and use aggregates. I want something dynamic and simple. ie: SELECT * FROM AnyTable Column 1, Column 2, Column 3 Data 1, Data 2, Data 3 Into: Column 1, Data 1 Column 2, Data 2 Column 3, Data 3 I'm guessing some stored procedure is necessary but I haven't been able to find any. Does anyone have something simple in the toolbox?

    M 1 Reply Last reply
    0
    • L Lost User

      In MS SQL 2008 I'm often doing big SELECT * statements from a couple tables looking for data in various fields, and have to scroll sideways in the SSMS window. What I'd like to do is for every field in whatever table I'm selecting, to pivot so its name goes into FieldName, and its value into FieldValue. Then I can use sorts and quickly find what I'm looking for. I've seen some articles on PIVOTs but they are almost always hardcoded for a specific table, and use aggregates. I want something dynamic and simple. ie: SELECT * FROM AnyTable Column 1, Column 2, Column 3 Data 1, Data 2, Data 3 Into: Column 1, Data 1 Column 2, Data 2 Column 3, Data 3 I'm guessing some stored procedure is necessary but I haven't been able to find any. Does anyone have something simple in the toolbox?

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Ahh now I have to explain to my collegues why I am sitting here chuckling - quick, generic and pivot all in the same sentence. There is nothing quick or generic about pivot tables. I have an article[^] that uses dynamic sql to avoid the hard coding but it is not quick and generic.

      Never underestimate the power of human stupidity RAH

      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