Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Create table and column dynamically

Author  Topic 

sarvan
Starting Member

4 Posts

Posted - 2008-06-02 : 03:22:31
Hi,
There is a table exists in a database, I have to write a stored procedure to create the same table in different database, with the same column name and field. This should be done in runtime. Is it possible. The table will be passed as a parameter to the stored procedure.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-02 : 03:26:05
May be
USE [DestinationDB]
SELECT TOP 0 * INTO DestinationTable FROM SourceDB..SouceTable
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2008-06-02 : 03:27:48
create proc x
@TableName varchar(128) ,
@DBName varcgar(128)
as
declare @sql varchar(2000)
select @sql = 'select * into ' + @TableName + ' from ' + @DBName + '..' + @TableName + ' where 1 = 0'
exec (@sql)
go

That's if it's run in the destination database.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

sarvan
Starting Member

4 Posts

Posted - 2008-06-02 : 04:16:21
Thnks.

Go to Top of Page
   

- Advertisement -