What if I'm my trigger can't find the second database ans say "b" is invalid
"Andrea Montanari" <andrea.sqlDMO@virgilio.it> wrote in message
news:3rhjurFji7nvU1@individual.net...
> hi,
> Islamegy® wrote:
>> While I discovered there is no way to create FK relation betwwen 2
>> table in diffrent Database & I need to check integrity between them..
>> So i'm trying to use For Delete Trigger but I'm unable to make my
>> trigger refrence to second database. How could i implement trigger
>> work with 2 database..
>> Second.. How could i make my C# application detect if the row deleted
>> or the trigger restored it??
>
> SET NOCOUNT ON
> USE master
> CREATE DATABASE a
> GO
> CREATE DATABASE b
> GO
> USE a
> GO
> CREATE TABLE dbo.masterT (
> ID int not null PRIMARY KEY
> )
> INSERT INTO dbo.masterT VALUES ( 1 )
> INSERT INTO dbo.masterT VALUES ( 2 )
> INSERT INTO dbo.masterT VALUES ( 3 ) -- NOT REFERENCED
> GO
> USE b
> GO
> CREATE TABLE dbo.detailT (
> ID int NOT NULL IDENTITY PRIMARY KEY ,
> IdMaster int NOT NULL
> )
> INSERT INTO dbo.detailT VALUES ( 1 )
> INSERT INTO dbo.detailT VALUES ( 2 )
> INSERT INTO dbo.detailT VALUES ( 2 )
> GO
> USE a
> GO
> CREATE TRIGGER tr_D_masterT ON dbo.masterT
> FOR DELETE
> AS BEGIN
> IF @@ROWCOUNT = 0 RETURN
>
> IF EXISTS (
> SELECT IdMaster FROM b.dbo.detailT t JOIN deleted d ON t.IdMaster=d.ID
> )
> BEGIN
> RAISERROR ('Referenced articles can not be deleted', 16 , 1 )
> ROLLBACK
> RETURN
> END
> END
> GO
> PRINT 'delete ok'
> DELETE dbo.masterT
> WHERE ID = 3
>
> PRINT 'delete failure'
> DELETE dbo.masterT
> WHERE ID = 1
>
> GO
> USE master
> GO
> DROP DATABASE a
> DROP DATABASE b
> --<--------
> ...
> delete ok
> delete failure
> Server: Msg 50000, Level 16, State 1, Procedure tr_D_masterT, Line 10
> Referenced articles can not be deleted
> ...
> --
> Andrea Montanari (Microsoft MVP - SQL Server)
>
http://www.asql.biz/DbaMgr.shtm http://italy.mvps.org > DbaMgr2k ver 0.15.0 - DbaMgr ver 0.60.0
> (my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
> interface)
> --------- remove DMO to reply
>
>