Create table with Select * into with different collation - sql

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)

Related

create view over 2 databases into a third one

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.

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 to compare table columns and match them in SQL Sever 2012

This is a bit of a HW assignment at work.
What I'm doing at work right now is database archiving. I take a source DB and move all (or specific portion) of data into a new archive DB using a stored procedure.
Problem is, not all of the columns in the source DB match the destination DB.
How can I compare the tables in the DBs for missing columns and then match them? So if source DB has Table 1 that has 4 columns and destination DB has Table 1 that only has 2 columns, how can I compare both Table 1's and then have it add/delete columns to match each other?
SQL Server 2012/SQL Management Studio
You can compare the table Columns by querying Information_Schema.Columns or sys.Columns.
The below query will provide number of missing columns
SELECT c2.table_name,c2.COLUMN_NAME
FROM archdb.[INFORMATION_SCHEMA].[COLUMNS] c2
WHERE table_name='archtable' and c2.COLUMN_NAME
not in (SELECT column_name
FROM orgdb.[INFORMATION_SCHEMA].[COLUMNS]
WHERE table_name='orgtable')
This link will provide information about number of ways and tools available to compare sql server tables

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.

How to merge Access DB table into a SQL Server table?

I have a SQL Server table that has a unique field which matches a unique field in an Access DB table.
I need to get the Access DB table into the SQL Server table based on that field.
How do I go about doing this?
I want to say a JOIN, but a JOIN is just a temporary query, isn't it? I want this to be permanent. I want the Access DB table data merged into SQL Server table based on this unique field.
What's the best way to do this?
Thanks.
The reason I need this separate is that a software is reading this SQL Server data, so I need it all in the same table.
B,
I believe what you would be looking for is Union
With the two tables being
HumanResources.Employees and HumanResources.Employees2
SELECT ID, Name FROM HumanResources.Employees
UNION
SELECT ID, Name FROM HumanResources.Employees2