stacking two sql tables (2008) with different column names - sql

I checked this site for code to stack two tables (put the results from one table beneath the results from another) where the column names are different. I know that UNION ALL works when the names are the same.. and I KNOW THAT UNION ALL with an assignment for the column names that are missing from one table works when one table has more info than the other.. but what if the column names are different? like what if in one table the column name is CHP and in another it is "CHILD HEALTH PLUS" and I need those two columns to be stacked on top of one another?

A UNION can be used as long as the datatypes of the columns are the same. It doesn't matter if the column names are different.
SELECT column1
FROM Table1
UNION
SELECT column1
FROM Table2
If you want to know which table the records are coming from, then you can add another field that will distinguish the rows:
SELECT column1, 'Table1' as TableName
FROM Table1
UNION
SELECT column1, 'Table2' as TableName
FROM Table2

what sql language? mysql/sql server?
mysql doesn't require that the column names be the same, but you could always use 'AS' to match up the column names eg.
select column1 AS col1, column2 AS col2...
EDIT: i just spotted the '2008' in the post title - is that sql server 2008? in any case, sql server doesn't seem to care re. column names either.

Related

Selecting column names and table names of a select statement

How can I select the column name and table name from a SQL?
I tried something like this but it didn't work:
select column_name, table_name from (select * from users);
This might sound silly, but I have a list of different SQLs and I need to extract their columns and tables into a list. So some of the statements could me:
select username, password from users
select createdate from userlog
select * from dept
...
If I can select the column name and table name of a select statement, then I should get, say for the first statement, username and password for columns and users for table name. And createdate for column and userlog for table name in the second statement.
Then if it all works, I can then loop through the list of select statements and extract their column and table names.
The below query worked for Oracle database.
SELECT COLUMN_NAME,TABLE_NAME FROM ALL_TAB_COLUMNS
You can see more about information-schema
Edit:
You may try like this:
SELECT COLUMN_NAME,TABLE_NAME FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME IN (SELECT ColumnName FROM users)
You need to parse the SQL statement so the SQL engine figures out the columns and datatypes of the columns that the statement returns.
How you do it best depends on what environment you are using. In some programming languages when you create a SqlPreparedStatement or OraCommand or whatever the object may be called, that object may have a metadata collection populated with column information after parsing.
If you are doing it in the database itself, parsing your statement with DBMS_SQL can get you the information you need. See Example 8 in the documentation at this link:
http://docs.oracle.com/database/121/ARPLS/d_sql.htm#ARPLS68205
--
Oh, and this gives you column names of the select statement. The table names I do not know of any way to get easily.

Create Table Union Between Multiple Data Sources

I am working in Crystal Reports 2011 and I am trying to merge two tables from two different data sources into one command to use in a report.
I am having a hard time finding any solid examples online of how to accomplish this inside of a command. I would expect to use a UNION and it look something like:
SELECT *
FROM DSN1.Employee
UNION
SELECT *
FROM DSN2.Employee
The two tables are the exact same, just in different databases that I cannot alter or add to.
Any guidance would be a tremendous help. Thank you in advance.
Dont know exactly what you mean by having a hard time anyway try something like this ....
Explicitly mention the column names in both selects and use UNION ALL, use fully qualified names of your tables something like this....
SELECT Column1, Column2, Column3, ..... <Other Column Names>
FROM [Database_Name1].[Schema1].[Table_Name1]
UNION ALL
SELECT Column1, Column2, Column3, ..... <Other Column Names>
FROM [Database_Name2].[Schema1].[Table_Name2]
using this syntax eliminates most of the possible errors.

SQL select query with one or two column excluded

how to write a select query with one or two column excluded instead of list down required columns.
for example we have a table Table1 with 10 columns (col_1,col_2,col_2 ..... col_10)
and we just want select col_1 to col_8.
instead of writing like
Select col_1, col_2, col_3 .... col_8 from Table1
should we select like this
Select -(col_9, col_10) * from Table1
This is possible but you would have to use dynamic SQL or a stored procedure. It would still mean having to specify the start and end column using fixed column names and indexes and if the columns weren't in running order (colN, colN+1, etc.) then it would become messy.
So I suppose your answer it, just type it out. The benefit you will gain from doing something cleaver is small.
I hope this helps.
Any ideas?
I think we can select column name first then write into query.
1 Select column with excluded.
SHOW COLUMNS FROM _table WHERE FIELD NOT IN ('column1','column2')
2 Then, Use the column result we've got from above query to write to select query column.
SELECT {result_aboved} FROM _table WHERE 1
but I don't think it can be only once query for this case. you have to query twice times at least.

select all columns but make certain columns equal to a scalar in sql

Something like:
SELECT *, COLUMN1 = 3, COLUMN2 = 1 FROM TABLE1
However, I would like the order of the columns to be the same as when select only SELECT *
and not add any extra columns like column1 and column2 but replace the existing ones that are included in *. TABLE1 contains the columns COLUMN1 and COLUMN2.
Thanks.
The order of the columns is the order listed in the table, as determined by ORDINAL_POSITION in the table INFORMATION_SCHEMA.COLUMNS.
This is the same order that they are presented in SQL Server Management Studio. So, here is a trick. Go into a query window and type "select ". Then, go to the Object Explorer part of SQL Server Management Studio and open the databse, then the table, until you see the "Columns" header with a folder next to it.
Now, drag the folder right after the "select ".
Voila! The columns appear, separated by commas.
Fill in the rest of what you need for the query. Make constant what you want constant. And you are done.
Otherwise, you would have to use dynamic SQL to accomplish this.

TSQL - How to select some values from a table and some from SQL variables?

I want to return a table with columns names which are not defined somewhere else.
Under these columns names I want the records to contain data from different sources: a table, a previously declared variable and a const.
What is the right syntax to do so?
i.e.
columns names: "fromTable" (=select col1 from MyTable where id=1) , "fromVar" (=#MyVar), "fromConst"(=5).
thanks!
Simply
select <columns>,#myVar as FromVar, '5' as FromConst
from table