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 |
|
Ambiance44
Starting Member
16 Posts |
Posted - 2009-04-22 : 17:41:33
|
| Hello everyone,I have a database with 44 tables that have the same structure (2 columns: enum_id INT PK, value VARCHAR(255)).I created two tables to consolidate the data in all the 44 tables, since they all have the same structure and they don't change much. The two tables I created are as follows:1: enum_value_type (enum_value_type_id INT PK, description VARCHAR(255))2: enum_value (enum_value_type_id INT FK, enum_id INT, value VARCHAR(255))In table 1, I have typed out the names of 44 tables in the description field, and given each of them an ID. I would like to periodically reload all data from each of these 44 tables into the enum_value table.Is it possible to create a query that will use the description field's value as the source table name and copy that source table's data into the enum_value table? |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2009-04-22 : 18:06:44
|
| Easiest to generate a script then copy that into an sp.something likeselect 'insert enum_value select ' + convert(varchar(20),enum_value_type_id) + ', enum_id, value from ' + descriptionfrom enum_value_typeif you want it to be dynamicdeclare @s varchar(max)select @s = @s + char(13) + char(10) + 'insert enum_value select ' + convert(varchar(20),enum_value_type_id) + ', enum_id, value from ' + descriptionfrom enum_value_typeexec (@s)==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Ambiance44
Starting Member
16 Posts |
Posted - 2009-04-23 : 10:23:49
|
| nr: You are a genius!! Thank you very much for your help. The first script works perfectly. I really appreciate it. |
 |
|
|
Ambiance44
Starting Member
16 Posts |
Posted - 2009-04-23 : 11:22:33
|
I tweaked nr's query a little bit and created a stored procedure. Below is the SP with comments.Thanks-- Holds the present enum_value_type_idDECLARE @id int-- Holds the present dynamically generated SQL queryDECLARE @sql VARCHAR(255)-- Temporary table to hold the dynamically generated SQL queriesCREATE TABLE #commands (id INT, cmd VARCHAR(255))-- Empty out the enum_value tableDELETE FROM enum_value-- Generate SQL queries and store in temporary tableINSERT #commands SELECT convert(varchar(20),enum_value_type_id) , 'insert enum_value select ' + convert(varchar(20),enum_value_type_id) + ', id, value from ' + description FROM enum_value_type-- Initially assign the minimum enum_value_type_id to @idSELECT @id = MIN(id) FROM #commands-- Iterate through all the SQL queries in the temporary table and execute themWHILE @id IS NOT NULLBEGIN SELECT @sql = cmd FROM #commands WHERE id = @id EXECUTE(@sql) SELECT @id = MIN(id) FROM #commands WHERE id > @idEND-- Drop the temporary tableDROP TABLE #commands |
 |
|
|
|
|
|
|
|