鉴于select into语句的局限性(不能复制约束)
我尝试用储存过程来建立一个表。
要求此table的名字是动态赋予的,但是因为对储存过程的不了解,请求大家的帮助
主要是
1。N'[dbo].'+@tablename 写错了不知道怎么写
2。CREATE TABLE '[dbo].'+@tablename 也不知道怎么写
CREATE PROCEDURE [dbo].copyadminproducts
@tablename nvarchar(20)
AS
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].'+@tablename) and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table '[dbo].'+@tablename
CREATE TABLE '[dbo].'+@tablename (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[productsname] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[productsid] [int] NULL ,
[creteddate] [datetime] NULL
) ON [PRIMARY]
ALTER TABLE '[dbo].'+@tablename WITH NOCHECK ADD
CONSTRAINT [PK_@tablename] PRIMARY KEY CLUSTERED
(
[ID]
) ON [PRIMARY]
ALTER TABLE '[dbo].'+@tablename ADD
CONSTRAINT [DF_@tablename_creteddate] DEFAULT (getdate()) FOR [creteddate],
CONSTRAINT [IX_@tablename] UNIQUE NONCLUSTERED
(
[productsid]
) ON [PRIMARY] ,
CONSTRAINT [IX_@tablename_1] UNIQUE NONCLUSTERED
(
[productsname]
) ON [PRIMARY]
GO
我尝试用储存过程来建立一个表。
要求此table的名字是动态赋予的,但是因为对储存过程的不了解,请求大家的帮助
主要是
1。N'[dbo].'+@tablename 写错了不知道怎么写
2。CREATE TABLE '[dbo].'+@tablename 也不知道怎么写
CREATE PROCEDURE [dbo].copyadminproducts
@tablename nvarchar(20)
AS
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].'+@tablename) and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table '[dbo].'+@tablename
CREATE TABLE '[dbo].'+@tablename (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[productsname] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[productsid] [int] NULL ,
[creteddate] [datetime] NULL
) ON [PRIMARY]
ALTER TABLE '[dbo].'+@tablename WITH NOCHECK ADD
CONSTRAINT [PK_@tablename] PRIMARY KEY CLUSTERED
(
[ID]
) ON [PRIMARY]
ALTER TABLE '[dbo].'+@tablename ADD
CONSTRAINT [DF_@tablename_creteddate] DEFAULT (getdate()) FOR [creteddate],
CONSTRAINT [IX_@tablename] UNIQUE NONCLUSTERED
(
[productsid]
) ON [PRIMARY] ,
CONSTRAINT [IX_@tablename_1] UNIQUE NONCLUSTERED
(
[productsname]
) ON [PRIMARY]
GO