You could follow below stord procedure to create a new table by dynamic.
create proc [dbo].[create_table] (
@TableName nvarchar(10),
@col2 nvarchar(10),
@col3 nvarchar(10),
@col4 nvarchar(10),
@col5 nvarchar(10))
as
begin
declare @s1 varchar(max)
set @s1 = 'if object_id('''+@TableName+''') is not null begin drop table ['+@TableName+'] end
CREATE TABLE [dbo].['+@TableName+'](
['+@col2+'][int]identity (1,1) primary key,
['+@col3+'][varchar](10) not null,
['+@col5+'][varchar](2) null)'
select @s1
exec (@s1)
end
Thanks.
Please mark the replies as answers if they help or unmark if not.
Feedback to us
martinaV
Member
1 Points
27 Posts
create a new table in sql by dynamic script
Aug 16, 2012 08:54 PM|LINK
Hello,
I need some script, which build any table in sql. I change only name of table and columns.
Sript create the table inclusive primary key, constrains ..
Thank
wmec
Contributor
6219 Points
3217 Posts
Re: create a new table in sql by dynamic script
Aug 17, 2012 03:02 AM|LINK
Use the way below
http://msdn.microsoft.com/en-us/library/ms188001.aspx
http://msdn.microsoft.com/en-us/library/ms173393%28v=sql.90%29.aspx
HuaMin Chen
Ajay2707
Contributor
4445 Points
858 Posts
Re: create a new table in sql by dynamic script
Aug 17, 2012 04:56 AM|LINK
hi friend,
see link.
http://stackoverflow.com/questions/21547/in-sql-server-how-do-i-generate-a-create-table-statement-for-a-given-table
Chen Yu - MS...
All-Star
21584 Points
2493 Posts
Microsoft
Re: create a new table in sql by dynamic script
Aug 20, 2012 09:13 AM|LINK
Hi martinaV,
You could follow below stord procedure to create a new table by dynamic.
create proc [dbo].[create_table] ( @TableName nvarchar(10), @col2 nvarchar(10), @col3 nvarchar(10), @col4 nvarchar(10), @col5 nvarchar(10)) as begin declare @s1 varchar(max) set @s1 = 'if object_id('''+@TableName+''') is not null begin drop table ['+@TableName+'] end CREATE TABLE [dbo].['+@TableName+']( ['+@col2+'][int]identity (1,1) primary key, ['+@col3+'][varchar](10) not null, ['+@col5+'][varchar](2) null)' select @s1 exec (@s1) endThanks.
Feedback to us
Develop and promote your apps in Windows Store