请教一个从指定名称的临时表取出指定nid的protectid的存储过程的写法
有一个表
id pid
1 2
1 3
1 4
1 5
1 6
2 1
2 2
2 3
想有存储过程实现以下功能:从表中查询出指定的id的pid的记录插入到一个指定的临时表;临时表的主建为递增的(从1开始);再从临时表中取出指定主建是某数字的pid。
如上表我先把id为1的记录查入到指定的临时表#t,再去出主建(假设为nid)为3的记录,结果取出的应该是4
if exists(select name from sysobjects where name = 'protectid' and type = 'p')
drop procedure protectid
go
create procedure protectid
@username nvarchar(50),
@numid bigint,
@protectid nvarchar(12) output
as
declare @tbname nvarchar(50)
set @tbname=quotename('#'+@username)
if object_id(@tbname) is not null exec('drop table '+@tbname)
exec('select nnid=identity(bigint,1,1), protectid into'+ @tbname+'from productdet')
exec('select'+ @protectid +'=protectid from '+ @tbname +'where nnid='+@numid)
GO
declare @sprotectid nvarchar(12)
exec protectid 't','3',@sprotectid output
我这样写,提示错误
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: '=' 附近有语法错误。
该怎么写,谢谢!