想取出max值的存储过程问题。(0分)

  • 主题发起人 主题发起人 fjfztv
  • 开始时间 开始时间
F

fjfztv

Unregistered / Unconfirmed
GUEST, unregistred user!
CREATE procedure GetBillNo<br>&nbsp; @TableName varchar(20),<br>&nbsp; @IDName varchar(18),<br>&nbsp; @ID varchar(18) output<br>as<br>begin<br>&nbsp; declare &nbsp; &nbsp;<br>&nbsp; &nbsp; @billname varchar(2), <br>&nbsp; &nbsp; @tmpsql varchar(255),<br>&nbsp; &nbsp; @tmp varchar(14), <br>&nbsp; &nbsp; @tmpformno varchar(18), @tmpno int,<br>&nbsp; &nbsp; @tmpdate datetime<br>&nbsp; select @tmpdate = getdate() &nbsp;<br>&nbsp; select @billname = billname from sys_billname where tablename = @tablename<br>&nbsp; select @tmp = @billname + '-' + convert(varchar(10), @tmpdate, 20) + '-'<br>&nbsp; select @tmpsql = 'select max(' + @IDName + ') from ' + &nbsp;@TableName + ' where ' + @IDName + ' like ''' + @tmp + '% '''<br>&nbsp; exec(@tmpsql)<br>&nbsp; if @tmpformno is null <br>&nbsp; &nbsp; select @tmpno = 1<br>&nbsp; else<br>&nbsp; &nbsp; select @tmpno = convert(int, right(@tmpformno, 4)) &nbsp;+1<br>&nbsp; select @tmpformno = &nbsp;@billname + '-' + convert(varchar(10), @tmpdate, 20) &nbsp;+ '-' + replicate('0', 4 - len(cast(@tmpno as varchar))) + cast(@tmpno as varchar)<br>&nbsp; select @ID = @tmpformno &nbsp;<br>end<br><br><br>我想把 &nbsp; select @tmpsql = 'select max(' + @IDName + ') from ' + &nbsp;@TableName + ' where ' + @IDName + ' like ''' + @tmp + '% ''' 这里的max 值取出来什么取啊 <br>改成这句 &nbsp;select @tmpsql = 'select @tmpformno=max(' + @IDName + ') from ' + &nbsp;@TableName + ' where ' + @IDName + ' like ''' + @tmp + '% '''<br>提示说要@tmpformno要定义。
 
你将<br>exec(@tmpsql)<br>改为<br>exec sp_executesql @tmpsql, N'@tmpformno int output', @tmpformno output<br>就可以了
 
@tmpsql 应该为nvarchar
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
928
SUNSTONE的Delphi笔记
S
后退
顶部