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 |
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-03-05 : 15:41:47
|
I've master tables and row as follow,CREATE TABLE [dbo].[tCompy]( [idx] [smallint] IDENTITY(1,1) NOT NULL, [cd] [varchar](50) NOT NULL, [desn] [varchar](50) NOT NULL, [stat] [bit] NOT NULL, [crtby] [varchar](20) NOT NULL, [crtdte] [smalldatetime] NOT NULL CONSTRAINT [DF_tCompany_crtdte] DEFAULT (getdate()), [updby] [varchar](20) NOT NULL CONSTRAINT [DF_tCompany_updby] DEFAULT ('na'), [upddte] [smalldatetime] NOT NULL CONSTRAINT [DF_tCompany_upddte] DEFAULT (getdate()), [editno] [smallint] NOT NULL CONSTRAINT [DF_tCompany_editno] DEFAULT ((1)), CONSTRAINT [PK_tCompany] PRIMARY KEY CLUSTERED ( [idx] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY], CONSTRAINT [tcompany01] UNIQUE NONCLUSTERED ( [cd] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY];insert into tCompy(cd,desn,stat,crtby)values('snt','sn tech',1,'admin');CREATE TABLE [dbo].[tCout]( [idx] [smallint] IDENTITY(1,1) NOT NULL, [cd] [varchar](20) NOT NULL, [desn] [varchar](50) NOT NULL, [inettrnx] [bit] NOT NULL, [stat] [bit] NOT NULL, [remk] [varchar](100) NOT NULL, [crtby] [varchar](20) NOT NULL, [crtdte] [smalldatetime] NOT NULL CONSTRAINT [DF_TCounter_crtdte] DEFAULT (getdate()), [updby] [varchar](20) NOT NULL CONSTRAINT [DF_tCounter_updby] DEFAULT ('na'), [upddte] [smalldatetime] NOT NULL CONSTRAINT [DF_tCounter_upddte] DEFAULT (getdate()), [editno] [smallint] NOT NULL CONSTRAINT [DF_tCounter_editno] DEFAULT ((1)), CONSTRAINT [PK_tCounter] PRIMARY KEY CLUSTERED ( [idx] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY], CONSTRAINT [tcounter01] UNIQUE NONCLUSTERED ( [cd] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY];insert into tCout(cd, desn, inettrnx, stat, remk, crtby)values('kl','kuala lumpur',1,1,'','admin');insert into tCout(cd, desn, inettrnx, stat, remk, crtby)values('iph','ipoh',1,1,'','admin');Let's say, i designed as follow,CREATE TABLE [dbo].[t1]( [idx] [smallint] IDENTITY(1,1) NOT NULL, [tcompycd] [varchar](50) NOT NULL, CONSTRAINT [PK_t1] PRIMARY KEY CLUSTERED ( [idx] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGOALTER TABLE [dbo].[t1] WITH CHECK ADD CONSTRAINT [FK_t1_tcompycd] FOREIGN KEY([tcompycd])REFERENCES [dbo].[tCompy] ([cd])ON UPDATE CASCADEON DELETE CASCADEGOALTER TABLE [dbo].[t1] CHECK CONSTRAINT [FK_t1_tcompycd];CREATE TABLE [dbo].[t2]( [idx] [smallint] IDENTITY(1,1) NOT NULL, [t1idx] [smallint] NOT NULL, [tcoutcd] [varchar](20) NOT NULL, CONSTRAINT [PK_t2] PRIMARY KEY CLUSTERED ( [idx] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGOALTER TABLE [dbo].[t2] WITH CHECK ADD CONSTRAINT [FK_t2_t1idx] FOREIGN KEY([t1idx])REFERENCES [dbo].[t1] ([idx])ON UPDATE CASCADEON DELETE CASCADEGOALTER TABLE [dbo].[t2] CHECK CONSTRAINT [FK_t2_t1idx]GOALTER TABLE [dbo].[t2] WITH CHECK ADD CONSTRAINT [FK_t2_tcoutcd] FOREIGN KEY([tcoutcd])REFERENCES [dbo].[tCout] ([cd])ON UPDATE CASCADEON DELETE CASCADEGOALTER TABLE [dbo].[t2] CHECK CONSTRAINT [FK_t2_tcoutcd];CREATE TABLE [dbo].[t3]( [idx] [smallint] IDENTITY(1,1) NOT NULL, [t1idx] [smallint] NOT NULL, [tcoutcd] [varchar](20) NOT NULL, CONSTRAINT [PK_t3] PRIMARY KEY CLUSTERED ( [idx] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGOALTER TABLE [dbo].[t3] WITH CHECK ADD CONSTRAINT [FK_t3_t1idx] FOREIGN KEY([t1idx])REFERENCES [dbo].[t1] ([idx])ON UPDATE CASCADEON DELETE CASCADEGOALTER TABLE [dbo].[t3] CHECK CONSTRAINT [FK_t3_t1idx]GOALTER TABLE [dbo].[t3] WITH CHECK ADD CONSTRAINT [FK_t3_tcoutcd] FOREIGN KEY([tcoutcd])REFERENCES [dbo].[tCout] ([cd])ON UPDATE CASCADEON DELETE CASCADEGOALTER TABLE [dbo].[t3] CHECK CONSTRAINT [FK_t3_tcoutcd] Me execute insert statement into t1 and t2 as follow,declare @idx smallintinsert into t1 values('snt');set @idx=scope_identity();insert into t2(t1idx,tcoutcd) values(@idx,'iph');I knew, how to create unique contraint into 1 tableMy question is,1. It's possible to create unique constraint into 2 table?2. If possible, how to make it combination of table t1(tcompycd) and table t2(tcoutcd) is a unique? |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-03-05 : 17:50:05
|
quote: Originally posted by tkizer A constraint can not span more than one table. Hope I understood your question correctly. You can instead write a trigger.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog"Let's begin with the premise that everything you've done up until this point is wrong."
Hi Sir,Felt better now. I understood constraint can not span more than one table.So far, i've as followt1idx | tcompycd----------------------------1 sntt2idx | t1idx | tcoutcd----------------------------1 1 iphMy objective now, i want to prevent the same recorddeclare @idx smallintinsert into t1 values('snt');set @idx=scope_identity();insert into t2(t1idx,tcoutcd) values(@idx,'iph');inserted into t1 and t2.Can you me, how the trigger look's like?Really need help |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-06 : 02:01:14
|
do you mean something likeCREATE TRIGGER YourtriggerON t2INSTEAD OF INSERTASBEGINSET NOCOUNT ONIF EXISTS(SELECT t1.tcompycd, i.tcoutcd FROM t1 JOIN INSERTED i ON i.t1idx =t1.idx GROUP BY t1.tcompycd, i.tcoutcd HAVING COUNT(*)=1)BEGININSERT INTO t2SELECT MAX(i.idx),i.t1idx,i.tcoutcdFROM t1 JOIN INSERTED iON i.t1idx =t1.idxGROUP BY i.t1idx,t1.tcompycd, i.tcoutcdHAVING COUNT(*)=1ENDEND ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-03-06 : 12:45:51
|
quote: Originally posted by visakh16 do you mean something likeCREATE TRIGGER YourtriggerON t2INSTEAD OF INSERTASBEGINSET NOCOUNT ONIF EXISTS(SELECT t1.tcompycd, i.tcoutcd FROM t1 JOIN INSERTED i ON i.t1idx =t1.idx GROUP BY t1.tcompycd, i.tcoutcd HAVING COUNT(*)=1)BEGININSERT INTO t2SELECT MAX(i.idx),i.t1idx,i.tcoutcdFROM t1 JOIN INSERTED iON i.t1idx =t1.idxGROUP BY i.t1idx,t1.tcompycd, i.tcoutcdHAVING COUNT(*)=1ENDEND ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Me using your code as follow,CREATE TRIGGER t201 ON t2 INSTEAD OF INSERTASBEGINSET NOCOUNT ONIF EXISTS(SELECT t1.tcompycd, i.tcoutcd FROM t1 JOIN INSERTED i ON i.t1idx =t1.idx GROUP BY t1.tcompycd, i.tcoutcd HAVING COUNT(*)=1) BEGIN INSERT INTO t2 SELECT MAX(i.idx),i.t1idx,i.tcoutcd FROM t1 JOIN INSERTED i ON i.t1idx =t1.idx GROUP BY i.t1idx,t1.tcompycd, i.tcoutcd HAVING COUNT(*)=1 ENDEND But it return error as follow,An explicit value for the identity column in table 't2' can only be specified when a column list is used and IDENTITY_INSERT is ON. |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-03-06 : 13:02:14
|
| It means the value in column id in table t2 is set as an autoincrement i.e an identity property has been set for the column.PBUH |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-03-06 : 13:04:13
|
quote: Originally posted by Idera It means the value in column id in table t2 is set as an autoincrement i.e an identity property has been set for the column.PBUH
can you show to me, where to change in my code? |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-03-06 : 13:08:36
|
| Just run thisSET IDENTITY_INSERT t2 ONPBUH |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-03-06 : 13:17:25
|
quote: Originally posted by Idera Just run thisSET IDENTITY_INSERT t2 ONPBUH
My alter trigger as follow,ALTER TRIGGER [dbo].[t201] ON [dbo].[t2] INSTEAD OF INSERTASBEGINSET NOCOUNT ONSET IDENTITY_INSERT t2 ONIF EXISTS(SELECT t1.tcompycd, i.tcoutcd FROM t1 JOIN INSERTED i ON i.t1idx =t1.idx GROUP BY t1.tcompycd, i.tcoutcd HAVING COUNT(*)=1) BEGIN INSERT INTO t2 SELECT MAX(i.idx),i.t1idx,i.tcoutcd FROM t1 JOIN INSERTED i ON i.t1idx =t1.idx GROUP BY i.t1idx,t1.tcompycd, i.tcoutcd HAVING COUNT(*)=1 ENDEND My error as follow,An explicit value for the identity column in table 't2' can only be specified when a column list is used and IDENTITY_INSERT is ON. |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-03-06 : 13:20:06
|
| Did u try my solution.Please run thisSET IDENTITY_INSERT t2 ONI cannot be more specific than this.PBUH |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-03-06 : 13:25:12
|
quote: Originally posted by Idera Did u try my solution.Please run thisSET IDENTITY_INSERT t2 ONI cannot be more specific than this.PBUH
1st, below was executedSET IDENTITY_INSERT t2 ON The result was,Command(s) completed successfully.2nd, below was executed as follow,ALTER TRIGGER [dbo].[t201] ON [dbo].[t2] INSTEAD OF INSERTASBEGINSET NOCOUNT ONIF EXISTS(SELECT t1.tcompycd, i.tcoutcd FROM t1 JOIN INSERTED i ON i.t1idx =t1.idx GROUP BY t1.tcompycd, i.tcoutcd HAVING COUNT(*)=1) BEGIN INSERT INTO t2 SELECT MAX(i.idx),i.t1idx,i.tcoutcd FROM t1 JOIN INSERTED i ON i.t1idx =t1.idx GROUP BY i.t1idx,t1.tcompycd, i.tcoutcd HAVING COUNT(*)=1 ENDEND The result was,An explicit value for the identity column in table 't2' can only be specified when a column list is used and IDENTITY_INSERT is ON. |
 |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2010-03-06 : 14:07:40
|
| The identity insert goes inside the trigger. Not run separately.--Gail ShawSQL Server MVP |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-03-06 : 14:21:06
|
quote: Originally posted by GilaMonster The identity insert goes inside the trigger. Not run separately.--Gail ShawSQL Server MVP
Me paste the identity inside the trigger as follow,ALTER TRIGGER [dbo].[t201] ON [dbo].[t2] INSTEAD OF INSERTASBEGINSET NOCOUNT ONSET IDENTITY_INSERT t2 ONIF EXISTS(SELECT t1.tcompycd, i.tcoutcd FROM t1 JOIN INSERTED i ON i.t1idx =t1.idx GROUP BY t1.tcompycd, i.tcoutcd HAVING COUNT(*)=1) BEGIN INSERT INTO t2 SELECT MAX(i.idx),i.t1idx,i.tcoutcd FROM t1 JOIN INSERTED i ON i.t1idx =t1.idx GROUP BY i.t1idx,t1.tcompycd, i.tcoutcd HAVING COUNT(*)=1 ENDEND It return an error as follow,An explicit value for the identity column in table 't2' can only be specified when a column list is used and IDENTITY_INSERT is ON. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-07 : 02:27:14
|
make last insert like below and tryINSERT INTO t2 (t1idx ,tcoutcd) SELECT i.t1idx,i.tcoutcd FROM t1 JOIN INSERTED i ON i.t1idx =t1.idx GROUP BY i.t1idx,t1.tcompycd, i.tcoutcd HAVING COUNT(*)=1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-03-07 : 02:43:47
|
quote: Originally posted by visakh16 make last insert like below and tryINSERT INTO t2 (t1idx ,tcoutcd) SELECT i.t1idx,i.tcoutcd FROM t1 JOIN INSERTED i ON i.t1idx =t1.idx GROUP BY i.t1idx,t1.tcompycd, i.tcoutcd HAVING COUNT(*)=1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
With your help, i'm successful to create trigger as follow,CREATE TRIGGER [dbo].[t201] ON [dbo].[t2] INSTEAD OF INSERTAS BEGINSET NOCOUNT ONIF EXISTS(SELECT t1.tcompycd, i.tcoutcd FROM t1 JOIN INSERTED i ON i.t1idx =t1.idx GROUP BY t1.tcompycd, i.tcoutcd HAVING COUNT(*)=1) BEGIN INSERT INTO t2 (t1idx ,tcoutcd) SELECT i.t1idx,i.tcoutcd FROM t1 JOIN INSERTED i ON i.t1idx =t1.idx GROUP BY i.t1idx,t1.tcompycd, i.tcoutcd HAVING COUNT(*)=1 ENDEND Currently, my t1 and t2 as follow,t1idx | tcompycd----------------------1 sntt2idx | t1idx | tcoutcd-----------------------------1 1 iphBut the problem is, me still can't achieve my objectivei want to prevent the same recorddeclare @idx smallintinsert into t1 values('snt');set @idx=scope_identity();insert into t2(t1idx,tcoutcd) values(@idx,'iph');inserted into t1 and t2. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-07 : 08:26:56
|
| what do you mean by same record?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-03-07 : 10:06:22
|
| i think, i need to normalized my table design first sir.tq for helping me |
 |
|
|
|
|
|
|
|