How To Transfer an Existing Table from One Schema to Another Schema in MS SQL Server?
Submitted by: AdministratorIf you want to move an existing table from one schema to another schema, you can use the "ALTER SCHEMA ... TRANSFER ..." statement as shown in the tutorial exercise below:
-- Login with "sa"
USE GlobalGuideLineDatabase;
GO
-- Confirming that "ggl_links" is in "dbo"
SELECT t.name AS table_name, t.type_desc,
s.name AS schema_name
FROM sys.tables t, sys.schemas s
WHERE t.schema_id = s.schema_id
AND t.name = 'ggl_links';
GO
<pre>table_name type_desc schema_name
----------- ----------- ------------
ggl_links USER_TABLE dbo</pre>
-- Moving a table to a new schema
ALTER SCHEMA ggl TRANSFER ggl_links;
GO
-- Confirming that "ggl_links" is moved to "ggl"
SELECT t.name AS table_name, t.type_desc,
s.name AS schema_name
FROM sys.tables t, sys.schemas s
WHERE t.schema_id = s.schema_id
AND t.name = 'ggl_links';
GO
<pre>table_name type_desc schema_name
----------- ----------- ------------
ggl_links USER_TABLE ggl</pre>
The last query confirms that table "ggl_links" is now in schema "ggl".
Submitted by: Administrator
-- Login with "sa"
USE GlobalGuideLineDatabase;
GO
-- Confirming that "ggl_links" is in "dbo"
SELECT t.name AS table_name, t.type_desc,
s.name AS schema_name
FROM sys.tables t, sys.schemas s
WHERE t.schema_id = s.schema_id
AND t.name = 'ggl_links';
GO
<pre>table_name type_desc schema_name
----------- ----------- ------------
ggl_links USER_TABLE dbo</pre>
-- Moving a table to a new schema
ALTER SCHEMA ggl TRANSFER ggl_links;
GO
-- Confirming that "ggl_links" is moved to "ggl"
SELECT t.name AS table_name, t.type_desc,
s.name AS schema_name
FROM sys.tables t, sys.schemas s
WHERE t.schema_id = s.schema_id
AND t.name = 'ggl_links';
GO
<pre>table_name type_desc schema_name
----------- ----------- ------------
ggl_links USER_TABLE ggl</pre>
The last query confirms that table "ggl_links" is now in schema "ggl".
Submitted by: Administrator
Read Online MS SQL Server Job Interview Questions And Answers
Top MS SQL Server Questions
☺ | How to download Microsoft SQL Server 2005 Express Edition? |
☺ | How To Create a Testing Table with Test Data in MS SQL Server? |
☺ | What Happens If Strings Are Casted into Wrong Code Pages in MS SQL Server? |
☺ | PHP MSSQL - How To Display a Past Time in Days, Hours and Minutes? |
☺ | How To Create a Large Table with Random Data for Index Testing in MS SQL Server? |
Top Databases Programming Categories
☺ | RDBMS Interview Questions. |
☺ | SQL Interview Questions. |
☺ | SSRS Interview Questions. |
☺ | Database Administrator (DBA) Interview Questions. |
☺ | Sybase Interview Questions. |