第一次用存储过程,真是不适应。一个简单的例子,大家看一下,错误在哪里。(sql server服务器端建立的存储过程)
CREATE PROCEDURE dbo.result
@mult1 int,
@mult2 int,
@result int output
as
set @result=@mult1*@mult2
GO
(应用程序)
procedure TForm1.Button2Click(Sender: TObject);
var
p1,p2,p3:Tparam;
begin
storedproc1.StoredProcName:='Result';
storedproc1.Close;
storedproc1.Params.Clear;
p1:=Tparam.Create(storedproc1.Params,ptinput);
p2:=Tparam.Create(storedproc1.Params,ptinput);
p3:=Tparam.Create(storedproc1.Params,ptoutput);
try
storedproc1.Params[0].name:='mult1';
storedproc1.Params[1].Name:='mult2';
storedproc1.Params[2].Name:='@result';
storedproc1.ParamByName('mult1').AsInteger:=3;
storedproc1.ParamByName('mult2').AsInteger:=2;
StoredProc1.prepared:=true;
StoredProc1.open;
//storedproc1.ExecProc;
edit1.Text:=inttostr(storedproc1.ParamByName('@result').AsInteger);
finally
p1.Free;
p2.Free;
p3.Free;
end;
end;出错提示是:field of '@result' is unknown type.
CREATE PROCEDURE dbo.result
@mult1 int,
@mult2 int,
@result int output
as
set @result=@mult1*@mult2
GO
(应用程序)
procedure TForm1.Button2Click(Sender: TObject);
var
p1,p2,p3:Tparam;
begin
storedproc1.StoredProcName:='Result';
storedproc1.Close;
storedproc1.Params.Clear;
p1:=Tparam.Create(storedproc1.Params,ptinput);
p2:=Tparam.Create(storedproc1.Params,ptinput);
p3:=Tparam.Create(storedproc1.Params,ptoutput);
try
storedproc1.Params[0].name:='mult1';
storedproc1.Params[1].Name:='mult2';
storedproc1.Params[2].Name:='@result';
storedproc1.ParamByName('mult1').AsInteger:=3;
storedproc1.ParamByName('mult2').AsInteger:=2;
StoredProc1.prepared:=true;
StoredProc1.open;
//storedproc1.ExecProc;
edit1.Text:=inttostr(storedproc1.ParamByName('@result').AsInteger);
finally
p1.Free;
p2.Free;
p3.Free;
end;
end;出错提示是:field of '@result' is unknown type.
var
p1,p2,p3:Tparam;
begin
storedproc1.StoredProcName:='Result';
storedproc1.Close;
storedproc1.Params.refresh;
try
storedproc1.ParamByName('@mult1').AsInteger:=3;
storedproc1.ParamByName('@mult2').AsInteger:=2;
StoredProc1.prepared:=true;
storedproc1.ExecProc;
edit1.Text:=inttostr(storedproc1.ParamByName('@result').AsInteger);
finally
end;
end;
这个你最好静态指定
应该定义成浮点型
我用这个SP从来不会出错
可惜我装SQLSERVER不能给你试了
看看别人是不是能试试
ADOStoredProc1.ProcedureName := 'result;1'; ADOStoredProc1.Parameters[1].Value := 1;
ADOStoredProc1.Parameters[2].Value :=3;
ADOStoredProc1.prepared:=true;
ADOStoredProc1.ExecProc;
Edit1.Text := VarToStr(ADOStoredProc1.Parameters.ParamByName('@result').Value);