当前位置: 代码迷 >> Sql Server >> SQL SERVER2000存储过程小疑点,之
  详细解决方案

SQL SERVER2000存储过程小疑点,之

热度:81   发布时间:2016-04-27 12:11:17.0
SQL SERVER2000存储过程小问题,高手进,在线等之!
我想建立这样一个存储过程:通过传入表名和该表的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
------解决方案--------------------
探讨

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]
e……

------解决方案--------------------
探讨

我现在要把最大的值返回,怎么写呢?
CREATE PROC TEST
(
@Table varchar(20),
@colname varchar(20),
@outmax int output
)
as
exec('SELECT [email protected]+'=MAX([email protected]+') FROM [email protected])
这样,[email protected]

------解决方案--------------------
楼上的有几个结果都对诶!
  相关解决方案