1. 程式人生 > >Delphi調用存儲過程

Delphi調用存儲過程

varchar RM con put reat sets proc dep rev

1.表結構如下(預算數據明細表):

CREATE TABLE [dbo].[BA_FeeDetail](
[ID] [int] IDENTITY(1,1) NOT NULL,
[FeeDeptID] [nvarchar](4) NULL,
[FeeDate] [int] NULL,
[FeeCode] [nvarchar](10) NULL,
[FeeType] [nvarchar](1) NULL,
[BAType] [nvarchar](1) NULL,
[FeeAmt] [float] NULL
) ON [PRIMARY]

2.創建存儲過程(用於取得某部門某次預算的某月某費目的預算額)

CREATE PROCEDURE [dbo].[GetBudgetAmt]
-- Add the parameters for the stored procedure here
@DeptID nvarchar(4) = ‘7120‘,
@FeeDate int = 201301,
@FeeCode nvarchar(8) =‘31301‘ ,
@FeeType nvarchar(1) = ‘2‘,
@BAType nvarchar(1)=‘1‘,
@FeeAmt float output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
SELECT @FeeAmt = FeeAmt from BA_FeeDetail where (
FeeCode = @FeeCode and
FeeType = @FeeType and
FeeDate = @FeeDate and
FeeDeptID = @DeptID and
BAType = @BAType
)

Return @FeeAmt
END

3.delphi 程序中調用(使用ADOConnect 和 ADOStoreProc )

procedure TForm1.btn1Click(Sender: TObject);
Var
BudgetAmt:Single;
begin
with ADOSP1 do
begin
ProcedureName :=‘GetBudgetAmt‘;
Parameters.Refresh; //必須有本語句,刷新參數
Parameters.ParamByName(‘@DeptID‘).Value:=EdtDept.Text;
Parameters.ParamByName(‘@FeeDate‘).Value:=EdtFeeDate.Text;
Parameters.ParamByName(‘@FeeCode‘).Value:=EdtFeeCode.Text;
Parameters.ParamByName(‘@FeeType‘).Value:=EdtFeeType.Text;
Parameters.ParamByName(‘@BAType‘).Value:=EdtBAType.Text;
Parameters.ParamByName(‘@FeeAmt‘).Value:=0.00;
ExecProc;
BudgetAmt:=Parameters.ParamByName(‘@FeeAmt‘).Value;
edtFeeAmt.Text:=Format(‘%.2f‘,[BudgetAmt]);
end;
end;

Delphi調用存儲過程