DELPHI 调用SQL server 存储过程中自定义消息

-----sql server 2008  

USE [abc]
GO
/****** Object:  StoredProcedure [dbo].[zero]    Script Date: 07/26/2020 17:07:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:        杨宗会
-- Create date: 2020-07-26
-- Description:    mdsn qq_457565758
---分母为零时的出错信息。
-- =============================================
ALTER PROCEDURE [dbo].[zero] 
    -- Add the parameters for the stored procedure here
      @A int,
      @b int,
      @c int,
      @msgError   varchar(200) output
AS
BEGIN
    
    SET NOCOUNT ON;
     begin try
      set @c=@a/@b
    end try
    begin catch
      select @msgError=ERROR_MESSAGE();
      raiserror(@msgError,16,1)
    end  catch
        
END
----------------------------------------------------------------------------------------------------------------------------------------------------------------------

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 201
  ClientWidth = 447
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object AbcConnection: TFDConnection
    Params.Strings = (
      'ConnectionDef=ABC')
    Connected = True
    LoginPrompt = False
    Left = 143
    Top = 29
  end
  object ZeroProc: TFDStoredProc
    Connection = AbcConnection
    StoredProcName = 'abc.dbo.zero'
    Left = 112
    Top = 119
    ParamData = <
      item
        Position = 1
        Name = '@RETURN_VALUE'
        DataType = ftInteger
        ParamType = ptResult
      end
      item
        Position = 2
        Name = '@A'
        DataType = ftInteger
        ParamType = ptInput
      end
      item
        Position = 3
        Name = '@b'
        DataType = ftInteger
        ParamType = ptInput
      end
      item
        Position = 4
        Name = '@c'
        DataType = ftInteger
        ParamType = ptInput
      end
      item
        Position = 5
        Name = '@msgError'
        DataType = ftString
        ParamType = ptInputOutput
        Size = 200
      end>
  end
end

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option,
  FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
  FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.MSSQL,
  FireDAC.Phys.MSSQLDef, FireDAC.VCLUI.Wait, FireDAC.Stan.Param, FireDAC.DatS,
  FireDAC.DApt.Intf, FireDAC.DApt, Data.DB, FireDAC.Comp.DataSet,
  FireDAC.Comp.Client;

type
  TForm1 = class(TForm)
    AbcConnection: TFDConnection;
    ZeroProc: TFDStoredProc;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
      try
          ZeroProc.ExecProc('Zero', [100, 0]);-----100/0
       except
         showmessage (ZeroProc.ParamByName('@msgError').Value);-----出错捕错
       end;
end;

end.

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值