日期:2014-05-18  浏览次数:20646 次

SQLServer中表名想用输入的参数该怎么搞
CREATE PROCEDURE procGetCurNo
(
  @tblName varchar(256),
@curid0 int OUTPUT
)
AS
BEGIN TRANSACTION
select @curid0 = max(id0) + 1 from @tblName
COMMIT TRANSACTION
GO

我是这样写脚本的,但是不行,请高手指教下

------解决方案--------------------
SQL code
 CREATE PROCEDURE procGetCurNo
    (
      @tblName VARCHAR(256) ,
      @curid0 INT OUTPUT
    )
AS 
declare @sql nvarchar(4000)
set @sql=N'select @curid0=max(id0) + 1 from '+convert(nvarchar(256),@tblName)
exec sp_executesql @sql,'@curid0 int output',@curid0

------解决方案--------------------
SQL code

 CREATE PROCEDURE procGetCurNo
    (
      @tblName VARCHAR(256) ,
      @curid0 INT OUTPUT
    )
AS 
declare @sql nvarchar(4000)
set @sql=N'select @curid0=max(id0) + 1 from '+convert(nvarchar(256),@tblName)
exec sp_executesql @sql,'@curid0 int output',@curid0
--楼主的意思,应该还要返回这个出参值