Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
coolerbob
Aged Yak Warrior
841 Posts |
Posted - 2007-06-08 : 06:45:16
|
| I read these:http://weblogs.sqlteam.com/davidm/archive/2004/05/21/1364.aspxhttp://weblogs.sqlteam.com/mladenp/archive/2007/05/17/60209.aspxI thought I'd try something different (do a Find-and-replace on the word "YourDB"):How about this? No Calculated column, no view - so no arithabort pains (or any other common set option challenges for that matter). This will perform slower though:use YourDBprint 'DDL'BEGIN TRANSACTIONSET QUOTED_IDENTIFIER ONSET ARITHABORT ONSET NUMERIC_ROUNDABORT OFFSET CONCAT_NULL_YIELDS_NULL ONSET ANSI_NULLS ONSET ANSI_PADDING ONSET ANSI_WARNINGS ONCOMMITBEGIN TRANSACTIONGOCREATE TABLE dbo.TestAccounts ( AccID int NOT NULL IDENTITY (1, 1), EMailAdr varchar(255) NULL ) ON [PRIMARY]GOCOMMITBEGIN TRANSACTIONGOCREATE TABLE dbo.UniqueEmails ( UniqueEmail varchar(255) NOT NULL ) ON [PRIMARY]GOALTER TABLE dbo.UniqueEmails ADD CONSTRAINT [Email Addresses must be unique!] PRIMARY KEY CLUSTERED ( UniqueEmail ) WITH( PAD_INDEX = ON, FILLFACTOR = 95, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]GOCOMMITSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOUSE YourDBGO--The USE statement might fail. The GO statement clears out @@error. So you have to check DB_NAME if you don't want to run the risk of creating SProcs in the wrong DB.IF DB_NAME()<>'YourDB' BEGIN RAISERROR('Wrong database.',16,10) RETURNENDGOIF EXISTS(SELECT * FROM sysobjects WHERE name='tri_TestAccounts_Update') DROP trigger dbo.tri_TestAccounts_UpdateGOCREATE trigger tri_TestAccounts_Update on TestAccounts after update------------------------------------------------------------------------------------------------------ OBJECT NAME : tri_TestAccounts_Update---- AUTHOR : Coolerbob-- DATE : 08/06/2007---- INPUTS : -- OUTPUTS : -- DEPENDENCIES : None---- CALLED FROM : ---- DESCRIPTION : ---- EXAMPLES (optional) : ---- DEBUG (optional) : -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- MODIFICATION HISTORY :------------------------------------------------------------------------------------------------------ dd/mm/yyyy - Coolerbob-- no changes yet------------------------------------------------------------------------------------------------------ASinsert into UniqueEmails (UniqueEmail)select s.EMailAdrfrom inserted s where s.EMailAdr is not nulldelete t from UniqueEmails twhere exists (select * from deleted d where t.UniqueEmail=d.EMailAdr) and not exists (select * from TestAccounts s where t.UniqueEmail=s.EMailAdr)GO--EXEC isp_Grant_PermissionsSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOUSE YourDBGO--The USE statement might fail. The GO statement clears out @@error. So you have to check DB_NAME if you don't want to run the risk of creating SProcs in the wrong DB.IF DB_NAME()<>'YourDB' BEGIN RAISERROR('Wrong database.',16,10) RETURNENDGOIF EXISTS(SELECT * FROM sysobjects WHERE name='tri_TestAccounts_Delete') DROP trigger dbo.tri_TestAccounts_DeleteGOCREATE trigger tri_TestAccounts_Delete on TestAccounts after delete------------------------------------------------------------------------------------------------------ OBJECT NAME : tri_TestAccounts_Delete---- AUTHOR : Coolerbob-- DATE : 08/06/2007---- INPUTS : -- OUTPUTS : -- DEPENDENCIES : None---- CALLED FROM : ---- DESCRIPTION : ---- EXAMPLES (optional) : ---- DEBUG (optional) : -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- MODIFICATION HISTORY :------------------------------------------------------------------------------------------------------ dd/mm/yyyy - Coolerbob-- no changes yet------------------------------------------------------------------------------------------------------ASdelete t from UniqueEmails twhere exists (select * from deleted d where t.UniqueEmail=d.EMailAdr) and not exists (select * from TestAccounts s where t.UniqueEmail=s.EMailAdr)GO--EXEC isp_Grant_PermissionsSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOUSE YourDBGO--The USE statement might fail. The GO statement clears out @@error. So you have to check DB_NAME if you don't want to run the risk of creating SProcs in the wrong DB.IF DB_NAME()<>'YourDB' BEGIN RAISERROR('Wrong database.',16,10) RETURNENDGOIF EXISTS(SELECT * FROM sysobjects WHERE name='tri_TestAccounts_Insert') DROP trigger dbo.tri_TestAccounts_InsertGOCREATE trigger tri_TestAccounts_Insert on TestAccounts after insert------------------------------------------------------------------------------------------------------ OBJECT NAME : tri_TestAccounts_Insert---- AUTHOR : Coolerbob-- DATE : 08/06/2007---- INPUTS : -- OUTPUTS : -- DEPENDENCIES : None---- CALLED FROM : ---- DESCRIPTION : ---- EXAMPLES (optional) : ---- DEBUG (optional) : -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- MODIFICATION HISTORY :------------------------------------------------------------------------------------------------------ dd/mm/yyyy - Coolerbob-- no changes yet------------------------------------------------------------------------------------------------------ASinsert into UniqueEmails (UniqueEmail)select s.EMailAdrfrom inserted s where s.EMailAdr is not nullGO--EXEC isp_Grant_PermissionsSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOprint 'DML'print '--Add Email Address'insert into TestAccounts(EMailAdr)select 'Test@ms.com'print '--Now try a second time unsuccesfully'insert into TestAccounts(EMailAdr)select 'Test@ms.com'print '--Remove it'delete from TestAccountswhere EMailAdr='Test@ms.com'print '--Add it again'insert into TestAccounts(EMailAdr)select 'Test@ms.com' |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-08 : 10:08:36
|
You should have posted this in Script Library Forum MadhivananFailing to plan is Planning to fail |
 |
|
|
coolerbob
Aged Yak Warrior
841 Posts |
Posted - 2007-06-08 : 10:28:33
|
quote: Originally posted by madhivanan You should have posted this in Script Library Forum MadhivananFailing to plan is Planning to fail
sorry, I'm a peasant |
 |
|
|
|
|
|
|
|