Author |
Topic |
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2014-07-28 : 06:53:14
|
Hi. I have some table that will be created at run time.This will tell me what the table names are:select TempTableName from DBTempSyncScenario So what i want to do is get some columns from all the created tables.So let's suppose these tables have customer and providence:I want to do something like this:select Customer, provincefrom(select ts.Customer, ts.province from tempscenario tx,tempscenario2 ts)t But i need all the tables data into these 2 columns (so merged ts and tx)Now firstly i know this is wrong since it will duplicate records so the first question is how to fix this as to display all the record and not duplicates.The second question is where i draw the line.I have no idea how would i do it.Basically i would need something like this:select Customer, provincefrom(select dbs.Customer, dbs.province from (select TempTableName from DBTempSyncScenario) as dbs)t I am not using temp tables but i have no problem using them.What i suppose could be done is somehow merger all the tables TempTableName data into a temp table and go from there. So,something like this:select Customer,province into #newtable from( select * from tempscenariounion allselect * from tempscenario2union allselect * from tempscenariocsv) Aselect Customer, provincefrom #newtable Looks better i guess but again i have to loop the tables so i can put them in the union clause.So any clue?Thanks. |
|
djj55
Constraint Violating Yak Guru
352 Posts |
Posted - 2014-07-28 : 09:41:27
|
Are tempscenario, tempscenario2, and tempscenariocsv always the names?djj |
|
|
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2014-07-28 : 10:12:40
|
Hi.No the table names are selected from another table(select TempTableName from DBTempSyncScenario) that in contrast does not always have the same names |
|
|
djj55
Constraint Violating Yak Guru
352 Posts |
Posted - 2014-07-28 : 13:20:20
|
If the table names are not the same then some sort of dynamic SQL is going to be needed as T-SQL does not allow for table name variables.djj |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2014-07-28 : 15:21:32
|
Here's a framework. Due to a proxy server at my work, I have to add extra spaces in certain command words in my posts. Please remove those.d eclare @dsql nvarchar(4000), @id smallint, @rc smallintset @id = 1d eclare @temp1 table (Customer varchar(1000), province varchar(1000))d eclare @DBTempSyncScenario table (TempTableName sysname)insert into @DBTempSyncScenario values ('table1')insert into @DBTempSyncScenario values ('table2')insert into @DBTempSyncScenario values ('table3')select identity(smallint, 1, 1) as TempTableId, 'select Customer, province from ' + TempTableName as TempTableQueryinto #temp2from @DBTempSyncScenarioset @rc = @@rowcountwhile @id <= @rcbegin select @dsql = TempTableQuery from #temp2 where TempTableId = @id insert into @temp1 e xec (@dsql) set @id = @id + 1endselect Customer, provincefrom @temp1d rop table @DBTempSyncScenariod rop table @temp1d rop table #temp2 Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2014-07-28 : 17:09:24
|
Hey, thanks i will have a look at it tomorrow. Looks promising! |
|
|
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2014-07-28 : 18:13:14
|
Hey.As a quick check before i go to bed i get Incorrect syntax near '@DBTempSyncScenario' it's on drop table @DBTempSyncScenario and on drop table @table1 |
|
|
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2014-07-29 : 02:39:29
|
So tested at work today. This works great! Kudos! I removed the drop @DBtempsyncscenario and @drop @table1.Am i correct to think that since they are not actual table object in the db, they don't need to be dropped? Thanks. |
|
|
djj55
Constraint Violating Yak Guru
352 Posts |
Posted - 2014-07-29 : 08:46:13
|
Technically they do not need dropped, but cleaning up is a good habit.djj |
|
|
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2014-07-29 : 10:02:46
|
Well, i don't know the technique to drop them since it's giving me the error mentioned above. So i was guessing it's just memory objects at runtime and get dropped or garbage collected automatically after the query execution.I'm not sure, just guessing here. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2014-07-29 : 12:43:05
|
You don't even need the @BDTemp.. object as you have a permanent table with the info. Remember I provided just a framework, basically pseudocode. Revise as needed. Yes the drop will error for that one. I didn't do syntax checks, etc.Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2014-07-30 : 07:50:45
|
Thanks. |
|
|
|