| Author |
Topic |
|
doran_doran
Posting Yak Master
179 Posts |
Posted - 2009-11-03 : 14:28:45
|
| I have about 30 tables and each table has 50+ columns.I need to generate 50+ select statement with all the column names in each table. It will be impossible to type in each column name. ( I know i can do select * from table). But I need the actual column names in my statement.Is there a sql statement that I can run for each table with will give me the column names seperated by comma. for example,tblSales Id intsalesDate DatetimesalesPersonID intsaleAmount DoublesaleDiscount doubleI would like this column name to be like this after running the statmentid, salesdate, salespersonid, saleamount, salediscount etc... |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
doran_doran
Posting Yak Master
179 Posts |
Posted - 2009-11-03 : 14:40:35
|
| I am going to copy and paste this to create views. |
 |
|
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2009-11-03 : 14:48:15
|
| why not right click on table and then Script table as then SELECT to then New Query ..unless you are trying to do this dynamically?<><><><><><><><><><><><><><><><><><><><><><><><><>If you don't have the passion to help people, you have no passion |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2009-11-03 : 14:51:37
|
quote: Originally posted by doran_doran I am going to copy and paste this to create views.
To what end?So the Users can't see the table names?That's like an oooooooooooold DB2 trickwhat are you going to name the views?Are you just going to cut and paste them in SSMS?I went down this road before I knew what you wantedUSE [master]GODECLARE @str varchar(max)SELECT @str = COALESCE(@str + ', ', '')+COLUMN_NAME FROM INFORMATION_SCHEMA.ColumnsWHERE TABLE_NAME = 'spt_values'SELECT @str Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
doran_doran
Posting Yak Master
179 Posts |
Posted - 2009-11-03 : 14:53:07
|
| GREAT. I COULD DO THAT TOO. I am definitely not thinking today. Thanks Yosiasz.However, following is a good one too...SET NOCOUNT ONSET ANSI_WARNINGS OFFDECLARE @colname sysname, @cols varchar(1000)SET @cols = ''DECLARE c CURSOR FAST_FORWARD FOR SELECT b.name FROM sysobjects a JOIN syscolumns b ON b.id = a.id WHERE a.name = 'myLocalTableName' AND a.type = 'U' ORDER BY b.colorderOPEN cFETCH FROM c INTO @colnameWHILE @@Fetch_Status = 0BEGIN SET @cols = @cols + ', ' + @colname FETCH FROM c INTO @colnameENDCLOSE cDEALLOCATE cSELECT cols = Substring(@cols, 3, 1000) |
 |
|
|
doran_doran
Posting Yak Master
179 Posts |
Posted - 2009-11-03 : 14:55:13
|
| Brett,Your work too. Thanks everyone for the input. USE [master]GODECLARE @str varchar(max)SELECT @str = COALESCE(@str + ', ', '')+COLUMN_NAME FROM INFORMATION_SCHEMA.ColumnsWHERE TABLE_NAME = 'spt_values'SELECT @str |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2009-11-03 : 15:02:47
|
I really Think you want this, then just cut and paste the DDLSELECT SQL FROM (SELECT ' CREATE VIEW AS v_' + TABLE_NAME + CHAR(13)+CHAR(10)+ ' AS ' AS SQL, TABLE_NAME, 1 AS SQL_Group, 1 AS ORDINAL_POSITION FROM INFORMATION_SCHEMA.TablesUNION ALLSELECT ' SELECT ' + COLUMN_NAME AS SQL, TABLE_NAME, 2 AS SQL_Group, 1 AS ORDINAL_POSITION FROM INFORMATION_SCHEMA.Columns WHERE ORDINAL_POSITION = 1UNION ALLSELECT ' , ' + COLUMN_NAME AS SQL, TABLE_NAME, 2 AS SQL_Group, ORDINAL_POSITION FROM INFORMATION_SCHEMA.Columns WHERE ORDINAL_POSITION <> 1UNION ALLSELECT ' FROM ' + TABLE_NAME AS SQL, TABLE_NAME, 3 AS SQL_Group, 1 AS ORDINAL_POSITION FROM INFORMATION_SCHEMA.TablesUNION ALLSELECT 'GO' AS SQL, TABLE_NAME, 4 AS SQL_Group, 1 AS ORDINAL_POSITION FROM INFORMATION_SCHEMA.Tables) AS XXXORDER BY TABLE_NAME, SQL_Group, ORDINAL_POSITION Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
doran_doran
Posting Yak Master
179 Posts |
Posted - 2009-11-03 : 15:16:13
|
| WOW. Brett. THIS IS GOING TO SAVE ME ALL DAY WORK. I LIKE IT. THANKS............... |
 |
|
|
doran_doran
Posting Yak Master
179 Posts |
Posted - 2009-11-03 : 15:59:04
|
| Brett,I had to modify it (just a little). Here is the complete version. I took out the AS after CREATE VIEW.SELECT SQL FROM (SELECT ' CREATE VIEW view' + TABLE_NAME + CHAR(13)+CHAR(10)+ 'AS ' AS SQL, TABLE_NAME, 1 AS SQL_Group, 1 AS ORDINAL_POSITION FROM INFORMATION_SCHEMA.TablesUNION ALLSELECT ' SELECT ' + COLUMN_NAME AS SQL, TABLE_NAME, 2 AS SQL_Group, 1 AS ORDINAL_POSITION FROM INFORMATION_SCHEMA.Columns WHERE ORDINAL_POSITION = 1UNION ALLSELECT ' , ' + COLUMN_NAME AS SQL, TABLE_NAME, 2 AS SQL_Group, ORDINAL_POSITION FROM INFORMATION_SCHEMA.Columns WHERE ORDINAL_POSITION <> 1UNION ALLSELECT ' FROM ' + TABLE_NAME AS SQL, TABLE_NAME, 3 AS SQL_Group, 1 AS ORDINAL_POSITION FROM INFORMATION_SCHEMA.TablesUNION ALLSELECT 'GO' AS SQL, TABLE_NAME, 4 AS SQL_Group, 1 AS ORDINAL_POSITION FROM INFORMATION_SCHEMA.Tables) AS XXXORDER BY TABLE_NAME, SQL_Group, ORDINAL_POSITION |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|