Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ninjatill
Starting Member
3 Posts |
Posted - 2011-12-16 : 18:22:29
|
| I have an AIX mainframe that I replicate data from every night. The data gets dumped, unaltered, using a linked odbc connection to the AIX box. The problem I'm having is that the mainframe software automatically creates a new invoices table every year... it simply adds a character in the middle of the table name to differentiate. The developers stupidly also add that character to the column name of each column in the table. This makes it pretty hard to do a UNION ALL between the yearly tables and get one big table. Is it possible to loop through each table and dynamically rename them to remove the character.For example:Invoices for 2010 are stored in table: B1F_RECInvoices for 2011 are stored in table: B1G_RECB1F_REC has fields: B1F_INVNO, B1F_DATE, B1F_COST, B1F_SELL, etc.B1G_REC has fields: B1G_INVNO, B1G_DATE, B1G_COST, B1G_SELL, etc.Each table has the exact same fields but with that character. There are about 50 total fields. It would suck to hard-code a bunch of UNION ALL statements with column aliases when I have to add a new table every year. Right now we are using SQL Server 2010 Express but after we get this up and running, we're going upgrade. I hear the full version as SSIS which may be able to some data transformations but is there a way to just rename the columns in the tables for now?Any thoughts/suggestions appreciated. |
|
|
pduffin
Yak Posting Veteran
68 Posts |
Posted - 2011-12-16 : 19:47:33
|
| If you do a select * from .... in the union I believe the resulting output will only reference the field names from the first table or what you alias the first fields to be name.You could look at the execute comment and build and execute a sql command to insert all of the values into a table or to rename the fields in all the tables depending on how you want to go.Likes to run, hates the runs! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-17 : 01:46:58
|
quote: Originally posted by ninjatill I have an AIX mainframe that I replicate data from every night. The data gets dumped, unaltered, using a linked odbc connection to the AIX box. The problem I'm having is that the mainframe software automatically creates a new invoices table every year... it simply adds a character in the middle of the table name to differentiate. The developers stupidly also add that character to the column name of each column in the table. This makes it pretty hard to do a UNION ALL between the yearly tables and get one big table. Is it possible to loop through each table and dynamically rename them to remove the character.For example:Invoices for 2010 are stored in table: B1F_RECInvoices for 2011 are stored in table: B1G_RECB1F_REC has fields: B1F_INVNO, B1F_DATE, B1F_COST, B1F_SELL, etc.B1G_REC has fields: B1G_INVNO, B1G_DATE, B1G_COST, B1G_SELL, etc.Each table has the exact same fields but with that character. There are about 50 total fields. It would suck to hard-code a bunch of UNION ALL statements with column aliases when I have to add a new table every year. Right now we are using SQL Server 2010 Express but after we get this up and running, we're going upgrade. I hear the full version as SSIS which may be able to some data transformations but is there a way to just rename the columns in the tables for now?Any thoughts/suggestions appreciated.
yu can rename columns using sp_renamehttp://msdn.microsoft.com/en-us/library/ms188351.aspx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
ninjatill
Starting Member
3 Posts |
Posted - 2011-12-22 : 14:29:43
|
| I know about sp_rename but I did not know how to iterate the columns without actually referencing the column by name. I found some code on that pointed me in the right direction here: http://dushi.co.uk/15/07/2009/loop-through-database-tables-and-columns/Here is my code if anyone gets stuck in the future:CREATE PROCEDURE Test123 @mytablename nvarchar(256) = ''ASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Declare variables. DECLARE @TableName nvarchar(256) DECLARE @ColumnName nvarchar(128) DECLARE @NewColumnName nvarchar(128) DECLARE @FullColumnName nvarchar (256) -- Set the tablename from the passed variable. SET @TableName = @mytablename PRINT 'TableName: '+ CAST(@TableName AS CHAR) -- Set manually for testing. -- SET @TableName = '[dbo].[RAW_B1G_REC]' -- Test if tablename is not blank. IF @TableName IS NOT NULL AND @TableName <> '' BEGIN -- Initialize ColumnName to an empty string. SET @ColumnName = '' -- Begin looping through columns. WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL) BEGIN SET @NewColumnName = '' SET @FullColumnName = '' SET @ColumnName = ( SELECT MIN(QUOTENAME(COLUMN_NAME)) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = PARSENAME(@TableName, 2) AND TABLE_NAME = PARSENAME(@TableName, 1) /**You you want to go thourgh with certain type of data types**/ /**AND DATA_TYPE IN('char','varchar','nchar','nvarchar','text','ntext','image','xml','varbinary')**/ AND QUOTENAME(COLUMN_NAME) > @ColumnName ) -- PRINT 'ColumnName: '+ CAST(@ColumnName AS CHAR) -- PRINT LEFT(LEFT(@ColumnName,4),3) + RIGHT(@ColumnName,LEN(@ColumnName)-4) SET @NewColumnName = LEFT(LEFT(@ColumnName,4),3) + RIGHT(@ColumnName,LEN(@ColumnName)-4) SET @NewColumnName = LEFT(@NewColumnName, LEN(@NewColumnName)-1) SET @NewColumnName = RIGHT(@NewColumnName, LEN(@NewColumnName)-1) PRINT @NewColumnName SET @FullColumnName = @TableName + '.' + @ColumnName -- PRINT @FullColumnName IF @ColumnName IS NOT NULL BEGIN EXEC sp_rename @FullColumnName, @NewColumnName, 'COLUMN' END END ENDEND |
 |
|
|
ninjatill
Starting Member
3 Posts |
Posted - 2011-12-22 : 14:35:12
|
| Oh, I forgot. I have to do it this way because the tables do not have the same number of columns from year to year. I'm not sure why... I guess the developers make alterations at the users' request but don't go back and update past tables. Otherwise I would have tried just a UNION ALL as the pduffin suggested. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2011-12-22 : 14:55:26
|
so why not generate a view?SELECT SQL_CODE FROM (SELECT 'SELECT ' + '''' + SUBSTRING(COLUMN_NAME,1,3) + '''' + 'AS SOURCE ' AS SQL_CODE, TABLE_NAME, 2 AS SQL_ROW, 1 AS ORDINAL_POSITIONFROM INFORMATION_SCHEMA.ColumnsWHERE ORDINAL_POSITION = 1UNION ALLSELECT ' ' + COLUMN_NAME + ' AS ' + SUBSTRING(COLUMN_NAME,5,LEN(COLUMN_NAME) - 4), TABLE_NAME, 3 AS SQL_ROW, ORDINAL_POSITIONFROM INFORMATION_SCHEMA.ColumnsWHERE ORDINAL_POSITION = 1UNION ALLSELECT ' ' + COLUMN_NAME + ' AS ' + SUBSTRING(COLUMN_NAME,5,LEN(COLUMN_NAME) - 4), TABLE_NAME, 3 AS SQL_ROW, ORDINAL_POSITIONFROM INFORMATION_SCHEMA.ColumnsWHERE ORDINAL_POSITION <> 1UNION ALLSELECT 'UNION ALL ' AS SQL_CODE, TABLE_NAME, 4 AS SQL_ROW, 1 AS ORDINAL_POSITIONFROM INFORMATION_SCHEMA.TABLES) AS XXXWHERE TABLE_NAME IN ( 'B1F_REC' , 'B1G_REC')ORDER BY TABLE_NAME, SQL_ROW, ORDINAL_POSITION Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2011-12-23 : 00:26:40
|
| I'd use a VIEW too. SQL will then just see this as partitioned data, and process it in a very efficient manner (won't it? the column names being different is not an iossue is it for horizontal partition? and given that each table if for a specific year then a CHECK CONSTRAINT can be used for the horizontal partition) |
 |
|
|
|
|
|
|
|