all groups > sql server programming > december 2005 >
You're in the

sql server programming

group:

INSTEAD OF trigger not quite working ...


Re: INSTEAD OF trigger not quite working ... Kalen Delaney
12/26/2005 7:56:36 PM
sql server programming:

Hi Chris

Please show us the CREATE TABLE statement you are using, with the default
value for TypeID.

--
HTH
Kalen Delaney, SQL Server MVP
www.solidqualitylearning.com


[quoted text, click to view]


INSTEAD OF trigger not quite working ... Chris Smith
12/26/2005 11:07:17 PM
I am trying to create an INSTEAD OF trigger on a table that only has two
fields: an ID and a description. The following code is not quite doing,
well, anything that I have been able to figure out ...

CREATE TRIGGER InsteadInsertTrigger_BookTypes on BookTypes

INSTEAD OF INSERT

AS

BEGIN

-- The only column in the inserted table is the TypeDesc

-- Therefore we need to generate the UniqueID on behalf

-- of the application ...

INSERT INTO BookTypes

SELECT NewID() as TypeID, TypeDesc

FROM inserted

END

go



Where have I gone wrong here ?

Re: INSTEAD OF trigger not quite working ... Erland Sommarskog
12/26/2005 11:13:34 PM
Chris Smith (chris.smith@shaw.ca) writes:
[quoted text, click to view]

It would certainly help if you included the definition of BookTypes
and the INSERT statement you are using.

[quoted text, click to view]

A general comment on style here: always provide a column list for your
INSERT statements. If another column is added to the table, this INSERT
statement will fail.

By the way, why not simply use NEWID() as the default value for TypeID?



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: INSTEAD OF trigger not quite working ... Chris Smith
12/27/2005 1:45:01 AM
I HAVE put the newid() default value in for TypeID, but it does not seem to
function.

Field definitions are UniqueIdenfier for TypeID, and Varchar(50) for Desc.


[quoted text, click to view]

Re: INSTEAD OF trigger not quite working ... Erland Sommarskog
12/27/2005 8:16:36 AM
Chris Smith (chris.smith@shaw.ca) writes:
[quoted text, click to view]

This works for me:

CREATE TABLE chris (clé uniqueidentifier NOT NULL
CONSTRAINT chris_newid DEFAULT newid(),
data int NOT NULL)
go
INSERT chris (data) Values (4711)
go
SELECT * FROM chris


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: INSTEAD OF trigger not quite working ... Chris Smith
12/28/2005 1:01:34 AM
The table was originally created as a simple little table, and with time I
have added a couple of things with alter table - alter column and triggers.
The Create Table statement now looks like:
**************

USE [MediaCollectionData]

GO

/****** Object: Table [dbo].[BookTypes] Script Date: 12/27/2005 17:59:42
******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE TABLE [dbo].[BookTypes](

[TypeID] [uniqueidentifier] NOT NULL CONSTRAINT [DefaultType] DEFAULT
(newid()),

[TypeDesc] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,

CONSTRAINT [PK_UniqueBookTypes] PRIMARY KEY CLUSTERED

(

[TypeID] ASC

)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]



**************
[quoted text, click to view]

Re: INSTEAD OF trigger not quite working ... Tibor Karaszi
12/28/2005 10:34:47 AM
Most likely, you have an incorrect INSERT statement. My guess is that your INSERT statement
specifies a value (possibly NULL) for the TypeID column, which is why the default isn't respected. I
executed your CREATE TABLE statement, and then the below INSERT statement:

INSERT INTO BookTypes (TypeDesc) VALUES('Hello')

A values was indeed generated for the TypeID column.

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/


[quoted text, click to view]
Re: INSTEAD OF trigger not quite working ... Chris Smith
12/28/2005 4:39:01 PM
That's interesting to "hear". The insert statement is being generated behind
the scenes in a VB.Net program.

I'll have to do some digging on this. But I have a new direction now. Thank
you.


"Tibor Karaszi" <tibor_please.no.email_karaszi@hotmail.nomail.com> wrote in
message news:uGPHxH5CGHA.1124@TK2MSFTNGP10.phx.gbl...
[quoted text, click to view]

Re: INSTEAD OF trigger not quite working ... Tibor Karaszi
12/28/2005 6:25:48 PM
[quoted text, click to view]

I would have guesses that. :-)
You can use Profiler to catch the insert statement. And then dig into ADO.NET and see how you can
make the INSERT leave out that column (or possibly post in an ADO.NET group).

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/


[quoted text, click to view]
AddThis Social Bookmark Button