示例创建数据库中的存储过程:
CREATE TABLE BOOK(
BOOK_NO VARCHAR(20) PRIMARY KEY CLUSTERED,
BOOk_NAME VARCHAR(20),
AMOUNT SMALLINT
)
CREATE PROCEDURE ADO_STORED
@BOOK_NO VARCHAR(20),@BOOK_NAME VARCHAR(20),@AMOUNT SMALLINT=-1 OUTPUT
AS
BEGIN
INSERT INTO BOOK VALUES(@BOOK_NO,@BOOK_NAME,1)
SELECT @AMOUNT=AMOUNT FROM BOOK WHERE BOOK_NO=@BOOK_NO AND BOOK_NAME=@BOOK_NAME
END
DECLARE @AMOUNT SMALLINT
EXEC ADO_STORED '010','ado存储',@AMOUNT OUTPUT
SELECT @AMOUNT
以上是在sqlsever2000中创建一个实验的存储过程并取得成功
接下来调用delphi的adoquery控件来实现调用存储过程并实现返回值:
procedure TForm1.Button2Click(Sender: TObject);
var i:integer; //声明变量来测试返回值的成功
begin
with adoquery1 do
//adoquery的connectionstring属性可以直接跟数据库实现连接,可以不用adoconnection控件
begin
close;
sql.Clear;
parameters.Clear;
sql.Add('exec ADO_STORED :A,:B,:C output');//这就是调用存储过程
parameters.Items[0].Value:=edit1.text; //0对应着A
parameters.Items[1].Value:=edit2.Text; //1对应着B
prepared:=true;
execsql;
i:=parameters.Items[2].Value; //2对应着C,为返回值赋值
showmessage(inttostr(i)); //显示结果
end;
end;