May 15, 2010 at 3:10 am
How to copy the table's structure of dbo schema to other schema
I am using the store procedure for creating the schema and transfer the table for dbo to new schema after transafering the tables dbo has no table ,I need tables in both dbo and new schema.
May 15, 2010 at 5:07 am
the best way is to script the table out;
you can use the INTO [newtable] with a SELECT, like the script below for something fast and easy, but you'll miss a lot of the structure; that will not copy defaults, check constraints, etc i think:
create schema bob
go
select *
into bob.test
from dbo.test
where 1 = 2 -- never true so no data
Lowell
May 15, 2010 at 5:36 am
Yes ,This miss many tables structures like relationship and othe contriants ,Can you tell any other way..........
May 15, 2010 at 5:44 am
why can't you script the table out? and change dbo to the new schema name?
explain a bit more why you need to clone the table(and the data?) more than one time with a stored proc...
why can't the procedure just have the CREATE TABLE statement and change the script with dynamic SQL?
Lowell
May 15, 2010 at 6:00 am
Yes ,But when running the database script on stroed procedure the 'Go'Statement can create problem,and Without Go statement the Relation ship and other contriants are not created...
May 15, 2010 at 6:06 am
apjchandresh (5/15/2010)
Yes ,But when running the database script on stroed procedure the 'Go'Statement can create problem,and Without Go statement the Relation ship and other contriants are not created...
dynamic sql....
declare @sql varchar(2000)
set @sql = 'CREATE SCHEMA newschema'
PRINT @sql
EXEC(@sql)
set @sql = 'CREATE TABLE newschema.WhateverTable ....'
PRINT @sql
EXEC(@sql)
Lowell
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply