我想建立这样一个存储过程:通过传入表名和该表的ID字段名(ID字段为int型),返回这个表的最大ID,求解!
------解决方案--------------------
- SQL code
CREATE PROC TEST ( @Table varchar(20), @name int)as SELECT MAX(ID) FROM @TABLE WHERE [email protected]
------解决方案--------------------
- SQL code
CREATE PROC TEST ( @Table varchar(20), @colname varchar(20))as exec('SELECT MAX([email protected]+') FROM [email protected])
------解决方案--------------------
SELECT MAX(@colname) FROM @TABLE
这么些不行,[email protected],并非表格名称。
2楼的才是正确的。
------解决方案--------------------
try this,
- SQL code
CREATE PROC TEST ( @Table varchar(20), @colname varchar(20), @outmax int output)asbegin declare @sql varchar(6000) select @sql='SELECT MAX([email protected]+') FROM [email protected] create table #o(outmax int) insert into #o(outmax) exec(@sql) select @outmax=outmax from #oend-- 调用方法declare @om intexec TEST [表名],[字段名],@om outputselect @om
------解决方案--------------------
- SQL code
CREATE PROC TEST ( @Table varchar(20), @colname varchar(20), @outmax int output)as declare @sql nvarchar(100) set @sql='SELECT @max=MAX([email protected]+') FROM [email protected] exec sp_executesql @sql,[email protected] int output',@outmax output
------解决方案--------------------
- SQL code
--调用:declare @max intexec TEST '表名','字段名',@max outputselect @max
------解决方案--------------------
方法2,
- SQL code
create proc TEST ( @Table varchar(20), @colname varchar(20), @outmax int output)asbegin declare @sql nvarchar(4000) select @sql=N'SELECT @m=MAX([email protected]+') FROM [email protected] exec sp_executesql @sql,[email protected] int output',@[email protected] outputend-- 调用方法declare @om intexec TEST [表名],[字段名],@om outputselect @om
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------
楼上的有几个结果都对诶!