1 好久没写存储过程啦 突然记不清晰,,写个最简单的试试
create proc test
as
begin
select * from classes
end
exec test
1 带输出参数的存储过程
例如:
create proc testproc
(
@ids int,
@count int out
)
AS
begin
update students set seecount=seecount+1 where id=@ids;
select @count=seecount from students where id=@ids;
end
declare @count int
exec testproc 301,@count output
select @count
c# 代码:
public int testbystr(int id)
{
SqlParameter[] param = new SqlParameter[2];
param[0]=new SqlParameter("@ids",SqlDbType.Int,id);
param[1] = new SqlParameter("@count", SqlDbType.VarChar,30);
param[1].Direction=ParameterDirection.Output;
DbHelperSQL.RunProcedure("testproc",param);
return int.Parse(param[1].Value.ToString());
}
其中可能出现一下错误
string[1] size 属性就有无效大小值0
需要注意这个: param[1] = new SqlParameter("@count", SqlDbType.VarChar,30);
Output 和returnValue 是有区别的。
带输入参数和输出参数的存储过程是怎么回事 ?
参数模式共分为三种: IN模式、OUT模式、IN OUT模式
其中:
IN模式:可以在函数中接收用户传入的参数值,其为单向的值传递。
OUT模式:不可以为函数传入参数值,但其可以作为函数的返回值,类似C中的传址(其不可以作为函数的输入参数,只能作为输出参数)
IN OUT模式:即可以作为函数的输入函数,又可以作为函数的输出参数,同C中的传址。
举例:
-- OUT模式CREATE OR REPLACE FUNCTION f_outparmtest (sid in number, sname out varchar2, sage out number)RETURN boolean
IS
result boolean := true;
BEGIN
SELECT stuname, age INTO sname, sage FROM student WHERE stuid = sid;
RETURN result;
EXCEPTION WHEN others THEN
RETURN false;
END;
-- IN OUT模式,交换两个变量值CREATE OR REPLACE FUNCTION f_swap (n1 in out number, n2 in out number)RETURN boolean
IS temp number;
result boolean := true;
BEGIN temp := n1; n1 := n2; n2 := temp;
RETURN result;
END;
220

被折叠的 条评论
为什么被折叠?



