473,320 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to Union dynamic tables

Hi,
I need to query on some data. The data is from SMS2003, stored in SQL2005, all on a Windows2003 server. I write a few queries, but I consider myself an SQL beginner at best. Normally, I write simple queries like "select column1, column2, column4 from a_table where column2 = 'something'". That's about 90% of my writing ability.

Here's the challenge:
I need a query that can query a table and select a list of other tables and then list all or part of those tables. It also needs to be able to know not to list one of those tables. The schema for the first table is different from the rest, naturally, but the rest of the tables are all the same layout.

Here's an example:
Expand|Select|Wrap|Line Numbers
  1. MasterTable
  2. FriendlyName     TrueName
  3. ===============================
  4. Applications     Applications0
  5. Applications     Applications1
  6. Applications     Applications2
  7. Systems          Systems0
  8. Applications     Applications3
  9. Applications     Applications4
  10. Applications     Applications5
  11. Systems          Systems1
  12. ===============================
  13.  
  14. Applicaitions(x)     <- (x) is a number, like 0, or 1, or 2, etc.
  15. AppName     ProgName         Version
  16. ==========================================
  17. Word        winword.exe      10.5.6.7
  18. Notepad     notepad.exe      2.4.5.6
  19. ==========================================
I want to write something like:
Expand|Select|Wrap|Line Numbers
  1. Select AppName, ProgName
  2. From UnionEverythingFoundIn
  3.       (
  4.       Select Truename
  5.       From MasterTable 
  6.       Where 
  7.             FriendlyName = 'Applications'
  8.             And
  9.             TrueName <> 'Applications0'
  10.       )
The problem is that I don't know of a command for "UnionEverythingFoundIn", and I don't know how to have SQL loop through the list to dynamically build the unions like "Applications1 Union Applications2 Union...etc".

We've done this in TQL like:
Expand|Select|Wrap|Line Numbers
  1. USE SMS_DB
  2. GO
  3. DECLARE get_TableNames CURSOR FOR 
  4. SELECT TrueName FROM MasterTable
  5. Declare @Tablename as varchar(25)
  6. OPEN get_TableNames
  7. FETCH NEXT From get_TableNames into @TableName
  8. WHILE @@FETCH_STATUS = 0
  9. BEGIN
  10.       Exec('SELECT * From ' + @TableName)
  11.       FETCH NEXT FROM get_TableNames into @TableName
  12. END
  13. CLOSE get_TableNames
  14. DEALLOCATE get_TableNames
  15. RETURN
This works in the SQL Analyser but it's not "normal SQL" enough for SMS to handle it as a query.

My options, in order of preference, are:
1. A query that SMS can handle that does it all.
2. A View or Proceedure that does the hard part, but can be seen or called by SMS one demand.
3. A Job or Proceedure that SQL can run on a schedule that creates yet another table that SMS can query simply (like "Select * from new_table")

I'm putting max points on this because we need it and haven't been able to figure it out ourselves. Hopefully it will be a bit of a challenge, but not so much that you can't solve it.

Thanks,
Alan
Feb 22 '07 #1
0 2309

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: KENNY L. CHEN | last post by:
Dear experts, I have two tables in my Oracle 8i database: TEST (COL1,COl2,REC_NO) and TEST1 (COL1,COL2,REC_NO). Both tables are unique-indexed on (COL1,COL2,REC_NO). I think the following...
3
by: Paradigm | last post by:
I am using Access 2K as a front end to a MYSQL database. I am trying to run a Union query on the MYSQL database. The query is (much simplified) SELECT as ID from faxdata UNION SELECT as ID ...
2
by: Gerry | last post by:
I am relatively new to DB2 and having a problem with a simple union statement. Running Db2UDB version 8.1.1 on Aix 5.1 The union and union all SQL statements I am running produce the same...
3
by: Dalan | last post by:
From reading Access 97 help text, it seems that to do what I need to do will require a Union Query. As this would be my first, I think I might require a little guidance. I have two tables with...
1
by: CrystalDBA | last post by:
I usually design applications in SQL Server and Crystal Reports. I now need to create a crystal report on an MS Access database. I have two tables: Services: Date datetime Entry text...
2
by: mattytee123 | last post by:
I have about 20 tables, of which I would like to do a union query and count of how many of each different code there is? The simplified verson of the table is structured like this. Code ...
3
by: mikes | last post by:
I have 2 separate queries, which effectively are the same except they draw data from separate tables. Both tables are (design-wise) identical, only the data is different. for each query, there are...
5
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
2
by: MasterBlaster | last post by:
Hi I am developing an application in C# with MS SQL at backend . My database has tables corressponding to every month named JAN2008 ,FEB2008 . I allow user to enter From Date and To Date from two...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.