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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 instead of trigger

Author  Topic 

Sean_B
Posting Yak Master

111 Posts

Posted - 2007-01-11 : 11:23:01
Hi,
Can anyone help

I'm trying to use an instead of trigger to get a unique id from one table and insert it as a foregin key in another table, but the trigger won't work.

These are the tables
/****** Object: Table [dbo].[Instrument] Script Date: 01/11/2007 16:16:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Instrument](
[InstrumentId] [int] IDENTITY(1,1) NOT NULL,
[InstrumentTypeId] [int] NOT NULL,
CONSTRAINT [PK_Instrument] PRIMARY KEY CLUSTERED
(
[InstrumentId] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[Instrument] WITH CHECK ADD CONSTRAINT [FK_Instrument_InstrumentType] FOREIGN KEY([InstrumentTypeId])
REFERENCES [dbo].[InstrumentType] ([InstrumentTypeId])
GO
ALTER TABLE [dbo].[Instrument] CHECK CONSTRAINT [FK_Instrument_InstrumentType]
GO
ALTER TABLE [dbo].[Instrument] WITH CHECK ADD CONSTRAINT [FK_Instrument_Test1] FOREIGN KEY([InstrumentId])
REFERENCES [dbo].[Test] ([instrumentId])
GO
ALTER TABLE [dbo].[Instrument] CHECK CONSTRAINT [FK_Instrument_Test1]
GO
/****** Object: Table [dbo].[Test] Script Date: 01/11/2007 16:16:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Test](
[instrumentId] [int] NOT NULL,
[MyId] [int] IDENTITY(1,1) NOT NULL,
[MyValue] [varchar](50) COLLATE Latin1_General_CI_AS NOT NULL,
CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED
(
[MyId] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

-- this is the trigger
/****** Object: Trigger [dbo].[FutConInstrumentId] Script Date: 01/11/2007 16:18:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE trigger [dbo].[FutConInstrumentId]
on [dbo].[Test]
instead of insert
as
insert instrument
(
InstrumentTypeId
)
select instrumenttypeid from instrumenttype where instrumenttype.InstrumentName = 'FutCon'

insert Test
(
instrumentId,
MyValue
)
select @@IDENTITY, inserted.MyValue from inserted

I can't get this to work, I've tried it with a unique index instead of a foreign key and it'll let me insert one row, but fails when I try multiple inserts.

Can anyone tell me how to get this working please.
Thanks






___________________________________________________________

Sean

X002548
Not Just a Number

15586 Posts

Posted - 2007-01-11 : 11:26:08
why do you need a trigger for this?

Use Scope_identity in the sproc



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

Sean_B
Posting Yak Master

111 Posts

Posted - 2007-01-11 : 11:46:53
Thanks Brett, I'll take a look at this.

I managed to get this trigger working, but it isn't a very elegant way of doing this.

Sean
Go to Top of Page
   

- Advertisement -