CREATE PROCEDURE CreatTable ( @tableName nvarchar(20), @data nvarchar(50))
AS
CREATE TABLE @tableName (
ProjectResource nvarchar(70) NOT NULL ,
primary key (ProjectResource));
insert @tableName
values(@data)
GO
可它说“@tableName”未定义!高手帮帮忙啊!
3 个解决方案
#1
表名不能用变量,试写成一个字串,然后执行他
#2
CREATE PROCEDURE createtable
@a varchar(100)
AS
exec('create table '+@a
+'(a int,b int)')
GO
@a varchar(100)
AS
exec('create table '+@a
+'(a int,b int)')
GO
#3
--应该使用动态语句来执行
CREATE PROCEDURE CreatTable ( @tableName nvarchar(20), @data nvarchar(50))
AS
DECLARE @STR_SQL varchar(8000)
SET @STR_SQL='CREATE TABLE '+@tableName+' (
ProjectResource nvarchar(70) NOT NULL ,
primary key (ProjectResource))'
EXEC(@STR_SQL)
SET @STR_SQL=
'insert '+@tableName+'values(@data)'
EXEC(@STR_SQL)
GO
CREATE PROCEDURE CreatTable ( @tableName nvarchar(20), @data nvarchar(50))
AS
DECLARE @STR_SQL varchar(8000)
SET @STR_SQL='CREATE TABLE '+@tableName+' (
ProjectResource nvarchar(70) NOT NULL ,
primary key (ProjectResource))'
EXEC(@STR_SQL)
SET @STR_SQL=
'insert '+@tableName+'values(@data)'
EXEC(@STR_SQL)
GO
#1
表名不能用变量,试写成一个字串,然后执行他
#2
CREATE PROCEDURE createtable
@a varchar(100)
AS
exec('create table '+@a
+'(a int,b int)')
GO
@a varchar(100)
AS
exec('create table '+@a
+'(a int,b int)')
GO
#3
--应该使用动态语句来执行
CREATE PROCEDURE CreatTable ( @tableName nvarchar(20), @data nvarchar(50))
AS
DECLARE @STR_SQL varchar(8000)
SET @STR_SQL='CREATE TABLE '+@tableName+' (
ProjectResource nvarchar(70) NOT NULL ,
primary key (ProjectResource))'
EXEC(@STR_SQL)
SET @STR_SQL=
'insert '+@tableName+'values(@data)'
EXEC(@STR_SQL)
GO
CREATE PROCEDURE CreatTable ( @tableName nvarchar(20), @data nvarchar(50))
AS
DECLARE @STR_SQL varchar(8000)
SET @STR_SQL='CREATE TABLE '+@tableName+' (
ProjectResource nvarchar(70) NOT NULL ,
primary key (ProjectResource))'
EXEC(@STR_SQL)
SET @STR_SQL=
'insert '+@tableName+'values(@data)'
EXEC(@STR_SQL)
GO