sql存储过程,前辈们帮忙找个错

来源:百度知道 编辑:UC知道 时间:2024/09/23 22:32:29
create proc test
@tname nvarchar(10),@mainInfo nvarchar(50),@timeCode nvarchar(10)
as
if exists (select * from dbo.sysobjects where name=@tname)
begin
insert into @tname values(@mainInfo,@timeCode)
end
else
begin
create table @tname(
mainInfo nvarchar(50),
timeCode nvarchar(10)
)
insert into @tname values(@mainInfo,@timeCode)
end
go

错误显示:
服务器: 消息 137,级别 15,状态 2,过程 test,行 6
必须声明变量 '@tname'。
服务器: 消息 170,级别 15,状态 1,过程 test,行 10
第 10 行: '@tname' 附近有语法错误。
服务器: 消息 137,级别 15,状态 1,过程 test,行 14
必须声明变量 '@tname'。

变量“@tname”不能作为表明?

create proc test

@tname nvarchar(10),@mainInfo nvarchar(50),@timeCode nvarchar(10)
as

declare @sql1 varchar(800)

declare @sql2 varchar(800)

set @sql1 = 'insert into'+ @tname+ 'values ('+@mainInfo+','+@timeCode+')';

set @sql2 = 'create table '+ @tname+ '('+@mainInfo+' nvarchar(50),'+@timeCode+' nvarchar(10) )';

if exists (select * from dbo.sysobjects where name= @tname)

begin

exec(@sql1) ;

end

else

begin

exec(@sql2);

exec(@sql1);

end

定义变量少了DECLARE吧

@tname变量好像不能定义表名,
而且你定义变量是declare 变量名 类型(output)
再试试