SELECT from table specified in another table - possible without dynamic SQL? - sql

I've a database which contains several tables for various tables of different products. These products have unique part numbers across all tables.
To search across all tables, I've created a view which uses UNION ALL across all common fields in the tables.
Once a part has been identified, I need to select all the columns depending on the table the data resides in. The view includes a field that specifies the table the data was found in.
I'm not sure of the way to accomplish the last part:
CASE statement (I'm leaning towards this one at the moment)
Dynamic SQL (prefer not to use this, would involve SELECT * and other nasties)
SELECT in client side (client needs to select from arbitrary tables, require additional privileges, bad design?)
Alternative solution?
EDIT: Actually, IF statement is the only one that makes sense. Client shouldn't need access to the tables directly. Since the columns are different in each table anyway, might as well have a seperate statement for each table.
(I'd mark the question as answered, but I don't have enough reputation for that)

I am not sure whether i understood your question correctly.. my understanding is you have views which is selecting data from diffrent tables using union all.. you can give table name while creating view only
select "table1",table1.a,table1.b.. from table1
union all
select "table2", table2.a,table2.b ..... from table2

Actually, IF statement is the only one that makes sense. Client shouldn't need access to the tables directly. Since the columns are different in each table anyway, might as well have a seperate statement for each table.

Related

SQLite data comparison on two tables with 2 million rows of data

I've been trying to find a way of comparing huge amount of data in two different tables but I am not sure if this is the correct approach.
So, this is why I am asking it here to understand a problem more and getting some clarity in order to solve it.
As title says I have two tables with less than 2 million rows of data and I need to a data comparison on them. So basically I only need to check if data in one table matches the data from other tables. Each table comes from separate database and I've managed to create views in order to have the same column names.
Here is my approach which gives me differences from two tables.
SELECT db1.ADDRESS_ID, db1.address
FROM UAT_CUSTOMERS_DB1 db1
EXCEPT
SELECT db2.ADDRESS_ID, db2.address
FROM UAT_CUSTOMERS_DB2 db2;
I have two questions, so basically this works:
It seems pretty straightforward but can someone explain to me in a bit more depth how this query works with such speed ? Yes I know - read docs but I would really appreciate alternative answer.
How can I include all the columns from tables without specifying manually each column name ?

Merging database tables containing identical fields

This is a follow-up question to this one: Planning a database describing a multi-versioned product (no need to read it to answer this one).
To recap, I need to define multiple tables describing multiple versions of the same entity, namely print job log entry. These versions stem from different printer models that produce them. The thing is, all these different versions will always contain some number of common fields, e.g. [Copies Printed], regardless of the printer model, and others that are unique to a certain model (and thus, table version).
I'm working in MS Access.
I'm currently leaning towards the solution in which there is a table for each version. Let's say for simplicity's sake that there are only two printer models, and thus two tables, each one containing log entries produced by that model. In both of them there are some identical fields (same name, same type, same meaning). One of the queries that I will have to run will be to select rows from BOTH tables (e.g. select all entries with more than 10 copies in June 2014 run on any printer).
My question: how to write such a query, and how to define these tables, so that the DB knows that we're talking about the same [CopiesPrinted] field? A regular SELECT on such two tables (let's call them PrinterLog1 and PrinterLog2) will return another table, in which there will be two distinct columns, [PrinterLog1.CopiesPrinted] and [PrinterLog2.CopiesPrinted]. What needs to be done so that there is only one column [CopiesPrinted] in the resulting table, that will contain the data from the corresponding printer log table? In other words, how do I merge these two tables into one in a smart way? "JOIN ... ON PL1.CP=PL2.CP" queries won't help either, because the contents of [PrinterLog1.CopiesPrinted] and [PrinterLog2.CopiesPrinted] are supposed to be different.
Thanks!
Edit 1:
Thanks everyone, UNION seems to do the job. However, it seems to change column type for some fields. For instance it changed Yes/No (in the source table) to Number (in the result table) and put -1 and 0 for true and false respectively. Is there a way to remedy this?
Also, I can't seem to find anywhere on the web the list of SQL data types for Access. All I could find was the ones meant for Access users (Yes/No, Currency, etc.), whereas what I need is SQL-style types (VARCHAR, DECIMAL, etc.). Is there a list of SQL-style types for MS Access?
You would need to use the UNION statement.
For instance:
SELECT CopiesPrinted, Blahblah FROM PrinterLog1
UNION ALL
SELECT CopiesPrinted, Blahblah FROM PrinterLog2
More reading:
Combine the results of several select queries by using a union query
Union Query Overview - UNION vs UNION ALL

Efficient way to query similarly-named tables with identical column names

I'm building a report in SSRS which takes data from several tables with similar names. There are three different 'sets' of tables - i.e., 123xxx, 456xxx, and 789xxx. Within these groups, the only difference in the table names is a three-digit code for a worksite, so, for example, we might have a table called 123001, 123010, and 123011. Within each set of tables, the columns have the same names.
The problem is that there are about 15 different sites, and I'm taking several columns from each site and each group of tables. Is there a more efficient way to write that query than to write out the name of every single column?
I don't believe there is but I feel like the use of aliases on your tables would make it much easier to undestand/follow your query building.
Also, if you aren't comparing values on the tables at all, then maybe a union between each table select would help make sense too.
I would give each table an alias.
SELECT s1t1.name
FROM Site1Table1 as s1t1;

DYnamic SQL examples

I have lately learned what is dynamic sql and one of the most interesting features of it to me is that we can use dynamic columns names and tables. But I cannot think about useful real life examples. The only one that came into my mind is statistical table.
Let`s say that we have table with name, type and created_data. Then we want to have a table that in columns are years from created_data column and in row type and number of names created in years. (sorry for my English)
What can be other useful real life examples of using dynamic sql with column and table as parameters? How do you use it?
Thanks for any suggestions and help :)
regards
Gabe
/edit
Thx for replies, I am particulary interested in examples that do not contain administrative things or database convertion or something like that, I am looking for examples where the code in example java is more complicated than using a dynamic sql in for example stored procedure.
An example of dynamic SQL is to fix a broken schema and make it more usable.
For example if you have hundreds of users and someone originally decided to create a new table for each user, you might want to redesign the database to have only one table. Then you'd need to migrate all the existing data to this new system.
You can query the information schema for table names with a certain naming pattern or containing certain columns then use dynamic SQL to select all the data from each of those tables then put it into a single table.
INSERT INTO users (name, col1, col2)
SELECT 'foo', col1, col2 FROM user_foo
UNION ALL
SELECT 'bar', col1, col2 FROM user_bar
UNION ALL
...
Then hopefully after doing this once you will never need to touch dynamic SQL again.
Long-long ago I have worked with appliaction where users uses their own tables in common database.
Imagine, each user can create their own table in database from UI. To get the access to data from these tables, developer needs to use the dynamic SQL.
I once had to write an Excel import where the excel sheet was not like a csv file but layed out like a matrix. So I had to deal with a unknown number of columns for 3 temporary tables (columns, rows, "infield"). The rows were also a short form of tree. Sounds weird, but was a fun to do.
In SQL Server there was no chance to handle this without dynamic SQL.
Another example from a situation I recently came up against. A MySQL database of about 250 tables, all in MyISAM engine and no database design schema, chart or other explanation at all - well, except the not so helpful table and column names.
To plan for conversion to InnoDB and find possible foreign keys, we either had to manually check all queries (and the conditions used in JOIN and WHERE clauses) created from the web frontend code or make a script that uses dynamic SQL and checks all combinations of columns with compatible datatype and compares the data stored in those columns combinations (and then manually accept or reject these possibilities).

Multiple aliases for a Sybase table?

I am working on a team trying to phase out a legacy system. As this is a rather large system with multiple integrations, the database will live on, even after the legacy system is replaced.
Now, the problem is that all the table names in the database have numerical names: "RT001", "RT002", "RT003", etc. With well over 100 tables it gets harder to know what each table is, and how can be joined to get hold of specific data.
Is there a way to define a global table alias in sybase so that sybase knows that the SQL"select * from Order, OrderItems where ..." is referring to tables RT035 and RT036 ? This way I can keep the original table names as RT035, while having an alias like "Order", or even "RT035_Order" refer to it.
As far as I know there is no such thing as a "synonym" (that's what it's called in an Oracle database) in Sybase ASE. But you could still use simple view to do basically the same thing:
CREATE VIEW Order AS SELECT * FROM RT035;