| Author |
Topic |
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2008-12-30 : 14:23:27
|
I was looking for a clever way to OUTPUT the column names of a query into a tmp tablesomething likeDeclare @T table (ID int)insert Into @Tselect 1 Union allselect 2Select a.ID,b.aColumnNameOUTPUT Into #Tmp --All the column names from the queryfrom@T ainner join T_MYTABLE bon a.ID = b.ID Obviously this isn't working code, but in a nut shell is there anyway to get the column names from a query into a tmp table w/o creating a table then selecting from INFORMATION_SCHEMA (Or other system tables)??I'm almost positive this is not possible, but never hurts to ask. Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-30 : 14:30:12
|
| didnt get what you're asking. do you just want the column names of resultset as values to other table? or do you just want to create blank table with column of query? |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2008-12-30 : 15:06:42
|
BasicallyI would like to have a single statement that would output the column names into a table.So if I had a table (MYNEWTABLE) that had columns A B C D E, I could run a query that would do this.Create table #TMPTABLE(CName Char(1))Select * OUTPUT COLUMNNAMES INTO #TMPTABLE --ALL THE COLUMN FromMyNewTableSelect * from #TMPTABLE--RESULTSCNAME------ABCDEAs stated this is likley not possible withought creating a view or table to for the Query itself then just query the INFORMATION_SCHEMA for the columns that belong to it, but I was hoping there was someway to utilize a output command in a select statement to somehow export the column names for the data being returned into a #TMPTABLE.Thanks Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
revdnrdy
Posting Yak Master
220 Posts |
Posted - 2008-12-30 : 15:55:04
|
| Would this work? Seems simple enough unless I miss the point.Your temp table is created automagically.select COLUMN_NAME into MyTempTable from INFORMATION_SCHEMA.COLUMNSwhere table_name ='My_Old_Table_Name' |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-31 : 02:51:49
|
| i would have also done in way revdnrdy had posted. Any reason why you cant have this approach Vinnie? |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2008-12-31 : 03:12:09
|
The reason why it was not ideal is because there are joins to more than 1 table (I tried to illustrate this with the original post). I have used that method referanced by rev and am very familiar with it (that is why I specifically stated that I was looking for a alternative to that method in my posts).I simply wanted to see if there was a alternative that would allow me to create the #TMP table with the column names w/o needing to first isolate the recordset into a table/view. It does not appear that there is a way to.I appriciate your time, and Thanks for your help. Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-31 : 03:38:12
|
SELECT TOP 0 *INTO #tmpFROM Table1 AS t1INNER JOIN Table2 AS t2 ON ...SELECT * FROM tempdb.information_schema.columns where table_name like '#Tmp_%' E 12°55'05.63"N 56°04'39.26" |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2008-12-31 : 03:43:54
|
| Hi Peso, select top 0 from table means what Happens in the query? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-31 : 04:01:55
|
quote: Originally posted by Nageswar9 Hi Peso, select top 0 from table means what Happens in the query?
it selects no data but at same time build new table based on column info from query. |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2008-12-31 : 04:04:43
|
| Ok, Thanks Visakh |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-31 : 04:06:28
|
It selects 0 (zero) records, but still created the table with proper column names. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-31 : 04:07:17
|
quote: Originally posted by Nageswar9 Ok, Thanks Visakh
welcome |
 |
|
|
|