秦
秦浩天
Unregistered / Unconfirmed
GUEST, unregistred user!
A,B表构成主从表关系。要实现以下功能:1.当B表中的子记录
不为空时,不允许删除A表中的主记录。2.当A表中的state为1
时,不允许在B表中插入,删除,修改子记录。3.当A表中的state
为1时,不允许删除主记录。
开发工具elphi5+ADO+SQL Server7
问题:
当A表的state为1时,删除A表主记录或在B表中插入,删除子记录
时产生意外错误,不知是为什么?
A表及触发器如下:
CREATE TABLE [dbo].[A] (
[aid] [varchar] (4) NOT NULL ,
[name] [varchar] (8) NULL ,
[state] [bit] NOT NULL
) ON [PRIMARY]
GO
CREATE TRIGGER [tri_DEL_A] ON dbo.A
FOR DELETE
AS
declare
@aid varchar(4),
@STATE bit
select @STATE=STATE,@AID=AID
from deleted
if @STATE=1
begin
RAISERROR ('TRI_DEL_A:STATE IS TRUE.', 16, 1)
ROLLBACK TRANSACTION
end
IF EXISTS(SELECT * FROM B WHERE AID=@AID)
begin
RAISERROR ('TRI_DEL_A:B IS NOT EMPTY.', 16, 1)
ROLLBACK TRANSACTION
end
GO
*************************************************************
B表及触发器如下:
CREATE TABLE [dbo]. (
[bid] [varchar] (2) NOT NULL ,
[aid] [varchar] (4) NOT NULL ,
[je] [numeric](10, 2) NULL
) ON [PRIMARY]
GO
CREATE TRIGGER [tri_ins_b] ON dbo.B
FOR INSERT
AS
declare
@aid varchar(4),
@STATE bit
select @aid=aid
from inserted
select @STATE=STATE
from A
where aid=@aid
if @STATE=1
begin
RAISERROR ('TRI_INS_B:STATE IS TRUE.', 16, 1)
ROLLBACK TRANSACTION
end
GO
CREATE TRIGGER [tri_DEL_b] ON dbo.B
FOR DELETE
AS
declare
@aid varchar(4),
@STATE bit
select @aid=aid
from DELETED
select @STATE=STATE
from A
where aid=@aid
if @STATE=1
begin
RAISERROR ('TRI_DEL_B:STATE IS TRUE.', 16, 1)
ROLLBACK TRANSACTION
end
GO
CREATE TRIGGER [tri_UPD_b] ON dbo.B
FOR UPDATE
AS
declare
@aid varchar(4),
@STATE bit
select @aid=aid
from inserted
select @STATE=STATE
from A
where aid=@aid
if @STATE=1
begin
RAISERROR ('TRI_UPD_B:STATE IS TRUE.', 16, 1)
ROLLBACK TRANSACTION
end
GO
***********************************************************
Delphi程序如下,即用2个ADOTable,DBGrid分别连接和显视A.B表:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, DBCtrls, Grids, DBGrids, Db, ADODB;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOTable1: TADOTable;
ADOTable2: TADOTable;
DataSource1: TDataSource;
DataSource2: TDataSource;
DBGrid1: TDBGrid;
DBGrid2: TDBGrid;
DBNavigator1: TDBNavigator;
DBNavigator2: TDBNavigator;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
end.
不为空时,不允许删除A表中的主记录。2.当A表中的state为1
时,不允许在B表中插入,删除,修改子记录。3.当A表中的state
为1时,不允许删除主记录。
开发工具elphi5+ADO+SQL Server7
问题:
当A表的state为1时,删除A表主记录或在B表中插入,删除子记录
时产生意外错误,不知是为什么?
A表及触发器如下:
CREATE TABLE [dbo].[A] (
[aid] [varchar] (4) NOT NULL ,
[name] [varchar] (8) NULL ,
[state] [bit] NOT NULL
) ON [PRIMARY]
GO
CREATE TRIGGER [tri_DEL_A] ON dbo.A
FOR DELETE
AS
declare
@aid varchar(4),
@STATE bit
select @STATE=STATE,@AID=AID
from deleted
if @STATE=1
begin
RAISERROR ('TRI_DEL_A:STATE IS TRUE.', 16, 1)
ROLLBACK TRANSACTION
end
IF EXISTS(SELECT * FROM B WHERE AID=@AID)
begin
RAISERROR ('TRI_DEL_A:B IS NOT EMPTY.', 16, 1)
ROLLBACK TRANSACTION
end
GO
*************************************************************
B表及触发器如下:
CREATE TABLE [dbo]. (
[bid] [varchar] (2) NOT NULL ,
[aid] [varchar] (4) NOT NULL ,
[je] [numeric](10, 2) NULL
) ON [PRIMARY]
GO
CREATE TRIGGER [tri_ins_b] ON dbo.B
FOR INSERT
AS
declare
@aid varchar(4),
@STATE bit
select @aid=aid
from inserted
select @STATE=STATE
from A
where aid=@aid
if @STATE=1
begin
RAISERROR ('TRI_INS_B:STATE IS TRUE.', 16, 1)
ROLLBACK TRANSACTION
end
GO
CREATE TRIGGER [tri_DEL_b] ON dbo.B
FOR DELETE
AS
declare
@aid varchar(4),
@STATE bit
select @aid=aid
from DELETED
select @STATE=STATE
from A
where aid=@aid
if @STATE=1
begin
RAISERROR ('TRI_DEL_B:STATE IS TRUE.', 16, 1)
ROLLBACK TRANSACTION
end
GO
CREATE TRIGGER [tri_UPD_b] ON dbo.B
FOR UPDATE
AS
declare
@aid varchar(4),
@STATE bit
select @aid=aid
from inserted
select @STATE=STATE
from A
where aid=@aid
if @STATE=1
begin
RAISERROR ('TRI_UPD_B:STATE IS TRUE.', 16, 1)
ROLLBACK TRANSACTION
end
GO
***********************************************************
Delphi程序如下,即用2个ADOTable,DBGrid分别连接和显视A.B表:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, DBCtrls, Grids, DBGrids, Db, ADODB;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOTable1: TADOTable;
ADOTable2: TADOTable;
DataSource1: TDataSource;
DataSource2: TDataSource;
DBGrid1: TDBGrid;
DBGrid2: TDBGrid;
DBNavigator1: TDBNavigator;
DBNavigator2: TDBNavigator;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
end.