create view over 2 databases into a third one - sql

I have rwo Databases, let's say DB1 and DB2 which DB2 is a copy of DB1 and exactly the same.I also have an empty third Database named Main.How can i create a view within the Main database from the tables of DB1 and DB2.For example if [person].[person] is a table in DB1 and DB2,something like this:
CREATE VIEW v1 AS
SELECT * FROM [DB1].[person].[person]
UNION
SELECT * FROM [DB2].[person].[person];
P.S. All 3 databases are on the same server!

For creating view, you are not needed to have 3rd database as view is nothing but virtual table which is the combination of 2 or more different tables in same or different database or server.
For better understanding of SQL views, please refer SQL Views.

Related

How to copy table from one Data Base to another?

I want to copy some tables from my DB1 to my DB2. Tables in DB1 are same as tables on DB2 but data in the tables are different. I want to copy tables from DB1 to DB2 but to keep old tables and data on DB2. How I can do this with Microsoft SQL Server Management Studio? I tried to right click and do the export but before I have to click on Finish button looks like that will just copt all data from DB1 to DB2 and I do not want that. If anyone can help with this please let me know. Thank you.
You can export the tables from DB1 with another name to DB2 if you don't want to modify them. In the export wizard just change the name of the destination table.
So you want to merge the schema AND data from DB1 into DB2?
You should list out the exact requirements, the question is still vague even with that info.
What data do you want to keep, what is ok to blow out?
What schema do you want to keep, are you archiving the old tables? Changing table names?
If you are literally trying to merge db1 into db2 your issue is going to be in managing the relationship ids which will be getting reassigned since DB2 could already be using IDs that are present in DB1.
If you want to keep old data in the destination table (or juste update it), so you might use a Merge:
MERGE INTO db2.dbo.table1 B
USING (
SELECT *
FROM db1.dbo.table1) A
ON (A.Column1 = B.Column1 )
WHEN MATCHED THEN
-- update
WHEN NOT MATCHED THEN
-- insert new rows
USE db2;
CREATE TABLE table2 LIKE db1.table1;
INSERT INTO table2
SELECT * FROM db1.table1;
This is also a way to copy a table with its records to another database.

How virtual views are not stored on database

AS we know major difference between view and materialized view is- views are not stored on database and materialized views are stored physically on database.
But it will give output in below example.
e.g I have base table emp having 5 fields and I created v_emp view based on this table with only 3 required fields. and I run below query it gives correct output
desc v_emp;
My question is: if views are not stored anywhere on database from where it fetches data.
Note: I am using Oracle database
AFAIK, view is nothing but stored/saved query. So it's doesn't store any data and every time you run the view saying select * from v_emp behind the scene it run the underline select query saying select fld1, fld2, fld3 from employee;
Views ARE stored in the database. There are two main components of a view:
1) The definitions of the view... as in the select statement(for both types of views).
2) The physical storage of actual rows in the database (materialized views only).

Query to Access Sharded Tables

I am querying a vendors database that has data that is sharded between multiple tables. The tables are named Events_1, Events_2, Events_3, Events_4 etc.
It appears that a new table is automatically created when the table hits 10,000 records. I am looking to write a query that would be able union the results from all the tables (without having to manually add the tables as they are created) as well as a query to only look at the 2 newest tables.
Any suggestions on the best way to go about this?
The database is Microsoft SQL Server 2008
You can do this with dynamic SQL such that you query sysobjects for table name Events_* and build a SELECT UNION from the results.
Better, though perhaps more complicated, would be a DDL trigger that would dynamically update you UNION VIEW to append the new table whenever a table Events_* was created.

Create table with Select * into with different collation

I have a database (DB1) with one collation and many tables.
I've got another database (DB2) with a different collation (on another server).
I want to create copies of the tables from DB1 on DB2, but with the collation of DB2.
The tables have many columns and there are a lot of tables so I don't want to have to stick COLLATE .... on each column. I'd much rather be able to do SELECT * INTO ... FROM ....
How can I change the collation of the columns of the new table without specifying them all individually?
I'm running MS SQL 2005.
as what I have understood this problem .. solution is -
you can use DB link .... try to link all the desired tables from DB1 and create synonym in DB2
(obviously you have to give synonym name other then the table name .. because tables with same name will be there also in DB2)
now try to create new tables with union to DB2 tables (in other word you can say I am saying to merge the tables)

SSRS ..How to create drop down menu for different databases in same server

Please help me in this....
There are few databases DB1,DB2,DB3,DB4,DB5....
each database has same tables T1,T2,T3.....Tn. (each table has same columns C1, C2, C3......Cn... but data in those tables are different)
Requirement: query: select C1, C2, C3 from T1 inner join T2 on T1.C4 = T2.C4
the query will be same for all databases.
First step is to create drop down menu for Databases DB1, DB2, DB3, DB4, DB5
Then select one or more databases after that the query should run for selected database or databeses.
What source should I select and how to create dataset for them?
Your datasource could be ANY of the databases, the master db, or a newly created database called say "DBA". Then after you have the source you'd create a stored procedure that would fill up your drop down list with database names:
select name from master..sysdatabases
A dataset in reporting services is simply either table direct or stored procedure. In your case, you should make that a stored procedure that pulls the names of all the databases.
A datasource in RS means where you are going to pull the data, in reality this doesnt matter because you can access any data from any database including linked servers. In the case of another database simply use the database name.owner.table name.