Click here to Skip to main content
15,899,679 members
Home / Discussions / Database
   

Database

 
QuestionSelect statement: ordering by column name. Pin
Septimus Hedgehog24-Jun-13 0:06
Septimus Hedgehog24-Jun-13 0:06 
AnswerRe: Select statement: ordering by column name. Pin
Eddy Vluggen24-Jun-13 0:32
professionalEddy Vluggen24-Jun-13 0:32 
GeneralRe: Select statement: ordering by column name. Pin
Septimus Hedgehog24-Jun-13 0:55
Septimus Hedgehog24-Jun-13 0:55 
AnswerRe: Select statement: ordering by column name. Pin
Mycroft Holmes24-Jun-13 1:41
professionalMycroft Holmes24-Jun-13 1:41 
GeneralRe: Select statement: ordering by column name. Pin
Septimus Hedgehog24-Jun-13 1:55
Septimus Hedgehog24-Jun-13 1:55 
AnswerRe: Select statement: ordering by column name. Pin
GuyThiebaut24-Jun-13 2:10
professionalGuyThiebaut24-Jun-13 2:10 
GeneralRe: Select statement: ordering by column name. Pin
Tim Carmichael24-Jun-13 4:23
Tim Carmichael24-Jun-13 4:23 
GeneralRe: Select statement: ordering by column name. Pin
GuyThiebaut24-Jun-13 5:03
professionalGuyThiebaut24-Jun-13 5:03 
You're right - here's the code for all tables using the while version:

SQL
set rowcount 1

declare @table as nvarchar(max)
declare @tbl_rowcount as int
declare @col_rowcount as int
declare @col nvarchar(max)
declare @qry as nvarchar(max)

select @table = table_name
from information_schema.tables
order by table_name 

select @tbl_rowcount = @@rowcount

while (@tbl_rowcount != 0)
begin

  set @qry = 'select ' + '''' + @table + '''' + ' as tableName, ' 
   
  select @col = column_name
  from information_schema.columns
  where table_name = @table
  order by column_name
  
  select @col_rowcount = @@rowcount
   
  while (@col_rowcount != 0)
  begin
      
      set @qry += '['+@col + '],'
      
      select @col = column_name
      from information_schema.columns
      where table_name = @table
      and column_name > @col
      order by column_name
      
      select @col_rowcount = @@rowcount
      
  end
     
  set @qry = substring(@qry,1,len(@qry)-1) + ' from ' + @table
  set rowcount 0
  exec(@qry)
  set rowcount 1

  select @table = table_name
  from information_schema.tables
  where table_name > @table
  order by table_name 
  
  set @tbl_rowcount = @@rowcount

end

set rowcount 0

“That which can be asserted without evidence, can be dismissed without evidence.”

― Christopher Hitchens


modified 25-Jun-13 11:37am.

AnswerRe: Select statement: ordering by column name. Pin
David Mujica24-Jun-13 2:37
David Mujica24-Jun-13 2:37 
AnswerRe: Select statement: ordering by column name. Pin
Ralph D. Wilson II10-Jul-13 7:40
Ralph D. Wilson II10-Jul-13 7:40 
GeneralRe: Select statement: ordering by column name. Pin
Septimus Hedgehog10-Jul-13 20:11
Septimus Hedgehog10-Jul-13 20:11 
GeneralRe: Select statement: ordering by column name. Pin
Ralph D. Wilson II11-Jul-13 7:15
Ralph D. Wilson II11-Jul-13 7:15 
GeneralRe: Select statement: ordering by column name. Pin
Septimus Hedgehog11-Jul-13 10:27
Septimus Hedgehog11-Jul-13 10:27 
GeneralRe: Select statement: ordering by column name. Pin
Ralph D. Wilson II31-Jul-13 5:59
Ralph D. Wilson II31-Jul-13 5:59 
GeneralRe: Select statement: ordering by column name. Pin
Septimus Hedgehog31-Jul-13 7:46
Septimus Hedgehog31-Jul-13 7:46 
QuestionSQL Database Security Pin
Zeyad Jalil22-Jun-13 0:04
professionalZeyad Jalil22-Jun-13 0:04 
AnswerRe: SQL Database Security Pin
Eddy Vluggen22-Jun-13 7:24
professionalEddy Vluggen22-Jun-13 7:24 
AnswerRe: SQL Database Security Pin
Mycroft Holmes22-Jun-13 13:40
professionalMycroft Holmes22-Jun-13 13:40 
Generalmysql or postgresql for Ubuntu + Java trading system? Pin
crunchor21-Jun-13 20:41
crunchor21-Jun-13 20:41 
GeneralRe: mysql or postgresql for Ubuntu + Java trading system? Pin
Rutvik Dave21-Jun-13 21:20
professionalRutvik Dave21-Jun-13 21:20 
GeneralRe: mysql or postgresql for Ubuntu + Java trading system? Pin
Matthew Faithfull21-Jun-13 21:51
Matthew Faithfull21-Jun-13 21:51 
GeneralRe: mysql or postgresql for Ubuntu + Java trading system? Pin
crunchor21-Jun-13 23:56
crunchor21-Jun-13 23:56 
GeneralRe: mysql or postgresql for Ubuntu + Java trading system? Pin
Bernhard Hiller23-Jun-13 21:25
Bernhard Hiller23-Jun-13 21:25 
QuestionGenerating Custom XML from multiple rows in SQL server Pin
ONeil Tomlinson19-Jun-13 5:10
ONeil Tomlinson19-Jun-13 5:10 
AnswerRe: Generating Custom XML from multiple rows in SQL server Pin
Richard MacCutchan19-Jun-13 7:00
mveRichard MacCutchan19-Jun-13 7:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.