Moving schema to another database - sql-server

I have to move an schema from one database to a new database to keep a centralized schema into the same server. The problem is that I already have many stored procedures that use some of these tables from the schema that I need to move.
Is there any workaround to do this and change all the objects that use this tables to be able to pointed to the new database? Can I use synonyms or link server?
I'm working on SQL Server 2008 R2
Thank you.

yep; synonyms are the way to go (stole this example from http://msdn.microsoft.com/en-us/library/ms177544.aspx):
USE tempdb;
GO
-- Create a synonym for the Product table in AdventureWorks2012.
CREATE SYNONYM MyProduct
FOR AdventureWorks2012.Production.Product;
GO
-- Query the Product table by using the synonym.
USE tempdb;
GO
SELECT ProductID, Name
FROM MyProduct
WHERE ProductID < 5;
GO
You could pretty easily generate synonym statements by searching the sys.tables view and identifying the tables which belong to the schema you want to move.

Related

Need SQL Query to compare the same table name should have the view created in a different database

We have a central database, and there were other databases with have year naming convention. We move the tables periodically to another database. After moving the table, we create a view with the same name in the source database.
For Example
Have Database name: ABC (which has the table’s daily load)
Have another database name: ABC2023Q1 (The tables we move from ABC to this database, and we create a view in ABC table with the same table name)
Is it the right approach to check with information_schema.tables and information_schema. Views with left join? Please let me know if you have any other approach.

SQL Server: same table, multiple names

I have some SQL Server database tables that need to be renamed.
Unfortunately, those tables are used by some libraries for which I have no source code.
I plan to rewrite those libraries, but it will take some time.
In the meantime I wonder if there's a way to create an "alias" for my tables, so that they can be referenced with two different names.
I could create views like SELECT * FROM OldName but I'm concerned about performance.
Create a SYNONYM for the old name, after you rename the table. In short:
--Create example table
CREATE TABLE dbo.YourTable (ID int);
GO
--Rename it
EXEC sys.sp_rename N'dbo.YourTable',N'MyTable';
GO
--Create a synonym for the new name, with the old name
CREATE SYNONYM dbo.YourTable FOR dbo.MyTable;
GO
--Try selecting from old name, it works!
SELECT *
FROM YourTable;
GO
--Clean up
DROP SYNONYM dbo.YourTable;
GO
DROP TABLE dbo.MyTable;
A view should cause no performance problem, as the query is converted by SQL. But synonyms are a nice option as well.

view created table in microsoft sql server

I am running part of a query in Microsoft SQL server management studio
Select Table1.Column1
into #Table2
from Table1
now it has created the table but I actually want to view this table with my eyes but I cannot seem to find where the table is stored. Please could someone help me find it?
That is a temporary table. It will be created in the tempdb system database and you can see it by going to tempdb -> Temporary Tables.
Any tables where its name start with # is a Temporary Table. Exactly as the name suggests, it's temporary, and only exists for the same time the connection that created it does (or it is dropped).
If you want to view the data from a temporary table, you would do so like any other table SELECT * FROM #Table2;. .
I imagine what your really after is to not use a temporary table, so drop the # from the name, and the new table will be created in the database you are connected to.

SQL Server - Create Copy of Multiple Tables in Same Database

I need to duplicate several tables in a single database. Basically we have an old dot net nuke portal that needs to be split and our host limits us to a single database. So we just want to take the existing tables but copy them with an exact duplicate but different table prefix name (e.g. EEPTL_Users to JBPTL_Users).
I can use 'script to - create' to make the proper table structure but how do I do the proper inserts when I need to specify the primary keys but want the column to be an auto-incrementing identiy?
Is there a tool for doing what I need?
SET IDENTITY_INSERT [table name] ON
INSERTS
SET IDENTITY_INSERT [table name] OFf

What is the equivalent of 'CREATE TABLE ... LIKE ..." in SQL Server

I am working with SQL Server (I am a SQL Server noob) and trying to alter a table. I want to CREATE TABLE LIKE to safely store data while I drop keys and constraints and all the other rigamorole that SQL Server seems to require when altering on the original table but I have not been able to find a match to that command...
you want to recreate the same structure?
how about this
SELECT *
into test
FROM myRealTable
where 0=1
no data will be inserted into the new table
You can do
SELECT * INTO #MyTable_tmp FROM MyTable
Then modify your MyTable, and copy your data back in. Other approaches I've seen is to create a new table call it Mytable_Tmp (Not a temp table), which will be your new table.
Then copy your data doing any migrations you need. Then you will drop the original table and do a rename on Mytable.
Or you can get one of the many excellant tools that compare databases and generate difference scripts or VSTS DB Edition (Comes with developer) and you can do a diff script from a project file to a DB.
Edit
When you run SELECT * INTO #MyTable FROM MyTable, SQL Server creates a new temporary table called #MyTable that matches each column and data type from your select clause. In this case we are selecting * so it will match MyTable. This only creates the columns it doesn't copy defaults, constraints indexes or anything else.

Resources