Create Table Union Between Multiple Data Sources - sql

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.

Related

Create Microsoft SQL Temp Tables (without declaring columns – like Informix)?

I recently changed positions, and came from an Informix database environment, where I could use SQL statements to select one or more columns ... and direct the output to a temporary table. In Informix, for temp tables, I neither had to declare the column names, nor the column lengths (only the name of a temp table) - I could simply write:
select [columnname1, columnname2, columnname3 ..] from
[database.tablename] where... etc. into temp tablename1 with no log;
Note that in Informix, the temp table stores the column names by default... as well as the data types [by virtue of the data-type being stored in the temp table]. So, if the above statement was executed, then a developer could merely write:
select columname1, columnname2, etc. from tablename1
In my experience, I found this method was very useful - for numerous reasons ('slicing/dicing' the data, using various data sources, etc.)... as well as tremendously fast and efficient.
However, now I am using Microsoft SQL Server, I have not found a way (yet) do the same. In SQL Server, I must declare each column, along with its length:
Create table #tablename1 ( column1 numeric(13,0) );
insert into #tablename1(column1) select [column] from
[database.tablename] where …
[Then use the info, as needed]:
select * from #tablename1 [ and do something...]
Drop table #tablename1
Does anyone know of how I could do this and/or set-up this capability in Microsoft SQL Server? I looked at anonymous tables (i.e. Table-Value constructors: http://technet.microsoft.com/en-us/library/dd776382.aspx)... but the guidance stated that declaring the columns was still necessary.
Thanks ahead of time
- jrd
The syntax is :
select [columnname1], [columnname2], [columnname3] into tablename1 from [database].[schema].[tablename] where...
prefix tablename1 with # if you want the table to be temporary
It should be noted that, while you can use the syntax below:
SELECT col1, col2...
INTO #tempTable1
FROM TABLEA
You should also give your calculated columns names as well.
Such that you get:
SELECT col1, col2...,AVG(col9) AS avgCol9
INTO #tempTable1
FROM TABLEA
Its very simple in sql server as well all you have to do is
SELECT Column1, Column2, Column3,...... INTO #Temp
FROM Table_Name
This statement will Create a Temp Table on fly copying Data and DataType all over to the Temp Table. # sign makes this table a temporary table , you can also Create a Table by using the same syntax but with out the # sign, something like this
SELECT Column1, Column2, Column3,...... INTO New_Table_Name
FROM Table_Name

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.

stacking two sql tables (2008) with different column names

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.

Is it legitimate to combine INSERT and SELECT statements in SQL?

Is it legitimate to use a SELECT and INSERT simultaneously? Suppose we want to move old records from one table to another table with the same columns -- can I do this in a single statement? I feel like it should be possible with a stored procedure and it would be interesting to see how that could be done (from my perspective it would seem easier to manage this sort of thing in a programmatic context.) I'm actually primarily curious about whether it would legitimate to write a statement with both a SELECT from one table and use that information to INSERT into another, and hopefully an example of what that might look like.
Yes.
INSERT INTO TargetTable (<column names>)
SELECT <column names> FROM SourceTable
WHERE blah blah blah...
yes...
insert into newtable(col1, col2, ...)
select col1, col2, ... from othertable
Check out the SELECT INTO SQL statement. Sounds like that's what you're looking for.

How to Unselect The Field in select Query using sql

hi in my database i am store more than 50 field with primarykey (Auto increment) i am not sure about the fields name but i wants to select the entire data in that table , i am using
SELECT * FROM tablename
i want to select all the fields except that ID but this query populate the entire table so is there is possible to unselect the particular field in the select query. Can anyone have an idea please guide me. Thanks in Advance
The * indicates that you want to select ALL fields from a given table. If you want to select only a few fields, or all but one, then you will need to specify the ones you want manually:
select field1,field2,field3 from tablename
The SQL standard does not offer an "except" notation. It would be neat if we could
select t.* -t.ID
from some_table t
/
but it is not supported.
On the other hand, SELECT * is a dangerous construct. It is always better to explicitly list the columns we want in any given situation.