| Author |
Topic |
|
marino
Starting Member
7 Posts |
Posted - 2010-02-18 : 05:48:16
|
| Hello, sorry for the english but i'm italian.I'd like to sync in real time two table in sql server 2005 express.To do this i've realized 3 trigger (one for insert, for update and for delete).For example i'd like to sync table1 in DBmaster and table1 in DBslave.DBmaster and DBslave are in the same sql istance but the mdf file are in two different disk(c: and d: for example, d: can be a removable device)This is the trigger that I have used to sync table in case of update it and works correctly.CREATE TRIGGER [Dbmaster].[dbo].[AggiornaTab] ON [Dbmaster].[dbo].[table1] after update AS begin trydelete from [DBslave].[dbo].[table1] where [DBslave_DB1].[dbo].[table1].ID in (select ID from deleted) INSERT [DBslave_DB1].[dbo].[table1] SELECT * FROM inserted end try Begin Catch End catchIf the Dbslave is not findable the update in DBmaster is blocked.I'd like that the update in DBmaster not be blocked in every case.Thanks for the attention |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-18 : 05:53:49
|
| for real time sync isnt replication enough?http://msdn.microsoft.com/en-us/library/ms165700(SQL.90).aspx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
marino
Starting Member
7 Posts |
Posted - 2010-02-18 : 06:00:28
|
quote: Originally posted by visakh16 for real time sync isnt replication enough?http://msdn.microsoft.com/en-us/library/ms165700(SQL.90).aspx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
My sqlserver is express edition and replication, mirror and log shipping aren't supported. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-18 : 06:10:00
|
| then put a rollback inside catch with proper error message------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
marino
Starting Member
7 Posts |
Posted - 2010-02-18 : 06:22:34
|
quote: Originally posted by visakh16 then put a rollback inside catch with proper error message------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
If the DBslave isn't findble (for exmple if the removable drive is not plugged) the try block don't intercet error and every istruction in catch block isn't performed and also the update in DBmaster fail |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-02-18 : 06:46:33
|
| From SQL Documentation:"A TRY…CATCH construct catches all execution errors with severity greater than 10 that do not terminate the database connection.Errors with severity from 0 through 10 are informational messages and do not cause execution to jump from the CATCH block of a TRY…CATCH construct.Errors that terminate the database connection, usually with severity from 20 through 25, are not handled by the CATCH block because execution is aborted when the connection terminates."If database is offline, or table does not exist, I think you get an error with >20 severity |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-02-18 : 07:43:33
|
error severity is 16...-- Try...Catch is not working:BEGIN TRYselect * from isntThere.dbo.isntThereEND TRYBEGIN CATCHSELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() AS ErrorState, ERROR_PROCEDURE() AS ErrorProcedure, ERROR_LINE() AS ErrorLine, ERROR_MESSAGE() AS ErrorMessage;END CATCH-- Try...Catch IS workingBEGIN TRYexec ('select * from isntThere.dbo.isntThere')END TRYBEGIN CATCHSELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() AS ErrorState, ERROR_PROCEDURE() AS ErrorProcedure, ERROR_LINE() AS ErrorLine, ERROR_MESSAGE() AS ErrorMessage;END CATCHRead here:http://msdn.microsoft.com/en-us/library/ms175976(SQL.90).aspx No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-02-18 : 08:04:57
|
| Useful, thanks WebFred.It was the next para in BoL:"The following types of errors are not handled by a CATCH block when they occur at the same level of execution as the TRY…CATCH construct:Compile errors, such as syntax errors, that prevent a batch from running.Errors that occur during statement-level recompilation, such as object name resolution errors that occur after compilation because of deferred name resolution."So the SProc which caused the Trigger to fire COULD catch such an error? |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-02-18 : 08:19:12
|
Looks like it is so:create table isThere (test varchar(255))create trigger test on isThere for insert asinsert isntThere select * from insertedbegin trybegin traninsert isThere select 'test'commit tranend trybegin catchSELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() AS ErrorState, ERROR_PROCEDURE() AS ErrorProcedure, ERROR_LINE() AS ErrorLine, ERROR_MESSAGE() AS ErrorMessage; rollback tranend catchselect * from isTherecreate table isntThere (test varchar(255))begin trybegin traninsert isThere select 'test'commit tranend trybegin catchSELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() AS ErrorState, ERROR_PROCEDURE() AS ErrorProcedure, ERROR_LINE() AS ErrorLine, ERROR_MESSAGE() AS ErrorMessage; rollback tranend catchselect * from isThereselect * from isntTheredrop table isTheredrop table isntThere No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
marino
Starting Member
7 Posts |
Posted - 2010-02-18 : 12:28:37
|
| Thanks for your replay.In my tigger, if i use exec istruction, the try intercets the error but the update in DBmaster is interrupted.CREATE TRIGGER [Dbmaster].[dbo].[AggiornaTab] ON [Dbmaster].[dbo].[table1] after update AS begin tryexec ('delete from [DBslave].[dbo].[table1] where [DBslave_DB1].[dbo].[table1].ID in (select ID from deleted') exec('INSERT [DBslave_DB1].[dbo].[table1] SELECT * FROM inserted') end try Begin Catchprint 'ISN'T OK'End catchSo if the database DBslave is not findeble the query print 'ISN'T OK' but the update in DBmaster was aborted |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
marino
Starting Member
7 Posts |
Posted - 2010-02-18 : 12:45:38
|
| Sorry.if i use print 'is not ok' i the same case.The trigger cause the abort of update in DBmaster (and obviously in BDslave) |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-02-18 : 12:52:17
|
| That is how I read the Documentation as to what will happen.I don't know how you can prevent it.You could check that the Table exists (in sys.systables) and only proceed if the table exists, but that will not help if the database/server is offline |
 |
|
|
marino
Starting Member
7 Posts |
Posted - 2010-02-19 : 06:30:19
|
| Exist another way to sync two database in real time with sql 2005 express? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-19 : 06:36:49
|
quote: Originally posted by marino Exist another way to sync two database in real time with sql 2005 express?
is both dbs express ones?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
marino
Starting Member
7 Posts |
Posted - 2010-02-19 : 07:01:11
|
quote: Originally posted by visakh16
quote: Originally posted by marino Exist another way to sync two database in real time with sql 2005 express?
is both dbs express ones?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
All Database server are express edition |
 |
|
|
|