473,785 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Union dynamic tables

1 New Member
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 "UnionEverythin gFoundIn", and I don't know how to have SQL loop through the list to dynamically build the unions like "Applicatio ns1 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 2324

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

Similar topics

10
12843
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 SQL commands will return the same result but one of my friends don't think so. He said "QUERY 1" will return 1 unsorted record (ROWNUM < 2 ) first then sort the result (ORDER BY COL1 ASC,
3
3575
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 from letdata UNION SELECT as ID FROM MEMODATA; I get an ODBC error. The same query runs when the backend files are MDB files and it runs with MYSQL if I only combine 2 tables.
2
10752
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 results. No duplicates are being eliminated in the union. To verify, I ran SQL with union statements against system tables.
3
10548
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 10 fields that have like data (for instance both have an item description field, an item price field, a general notes field, etc.) but with different field names. The tables have approximately 20/40 other fields that are dissimilar and not needed...
1
7779
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 Amount number (should be 4 records)
2
4342
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 Count 1234 1 2468 1 1234 1 2468 1
3
2224
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 2 tables with a standard LEFT JOIN. One field of the query is calculated, looking for a NULL in one table, and then using a field from the second table in that case. One query looks like this: PARAMETERS Text ( 255 ); SELECT...
5
3847
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 table, we try separate this big table into twelve tables and create a view
2
3673
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 calenders on the front end. Now i want to write a query which would dynamically Union all the tables which lie between the selected dates of calenders and give me only those records in the union which satisfy any particular condition. Hope you would...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.