473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

instead-of trigger and contraints

Is Microsoft full of #*$#*% (again) or am I badly misunderstanding
something?

Quote from Microsoft's T-SQL doc:
INSTEAD OF triggers are executed instead of the triggering action.
These triggers are executed after the inserted and deleted tables
reflecting the changes to the base table are created, but before any
other actions are taken. They are executed before any constraints, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ so can perform preprocessing that supplements the constraint actions.

(SQL Server 2000 sp3a)

CREATE TABLE t (
a INT PRIMARY KEY,
b CHAR(1) NOT NULL)

I want to override the value of [b] with the value of 'X' when
inserting into t...

CREATE TRIGGER t_tbi ON t INSTEAD OF INSERT AS BEGIN
SET NOCOUNT ON
INSERT INTO t (a,b) (SELECT a,'X' FROM inserted)
END

Let's try it...

INSERT INTO t (a,b) VALUES(1,'z')
SELECT * FROM t
a | b
---|---
1 | X

Good, the trigger did what it was supposed to. Lets try a
slight variation...

INSERT INTO t (a) VALUES(2)
Server: Msg 233, Level 16, State 2, Line 1
The column 'b' in table 't' cannot be null.

WTF? What was that I just read about "[instead-of triggers]
are executed before any constraints"?!?!

What's going on here???

Jul 23 '05 #1
8 6485
Stuart,

I am not sure you got the right document. I can not locate your quote in
BOL, and the quote "These triggers are executed after the inserted and
deleted tables reflecting the changes to the base table are created, but
before any other actions are taken." is clearly not correct. MS is not that
stupid.

Can you provide the way you found the quote?

Quentin

"Stuart McGraw" <sm********@friizz.RimoovAllZZs.com> wrote in message
news:11************@corp.supernews.com...
Is Microsoft full of #*$#*% (again) or am I badly misunderstanding
something?

Quote from Microsoft's T-SQL doc:
INSTEAD OF triggers are executed instead of the triggering action.
These triggers are executed after the inserted and deleted tables
reflecting the changes to the base table are created, but before any
other actions are taken. They are executed before any constraints,

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
so can perform preprocessing that supplements the constraint actions.

(SQL Server 2000 sp3a)

CREATE TABLE t (
a INT PRIMARY KEY,
b CHAR(1) NOT NULL)

I want to override the value of [b] with the value of 'X' when
inserting into t...

CREATE TRIGGER t_tbi ON t INSTEAD OF INSERT AS BEGIN
SET NOCOUNT ON
INSERT INTO t (a,b) (SELECT a,'X' FROM inserted)
END

Let's try it...

INSERT INTO t (a,b) VALUES(1,'z')
SELECT * FROM t
a | b
---|---
1 | X

Good, the trigger did what it was supposed to. Lets try a
slight variation...

INSERT INTO t (a) VALUES(2)
Server: Msg 233, Level 16, State 2, Line 1
The column 'b' in table 't' cannot be null.

WTF? What was that I just read about "[instead-of triggers]
are executed before any constraints"?!?!

What's going on here???

Jul 23 '05 #2
NOT NULL is not a constraint, it is a property of the column definition, the
same as the datatype. INSERT INTO t(a,b) VALUES (1,'aa') will give an error
as well. The INSERT statement still get validated against the columns.

Check constraints, foreign keys etc don't get checked before the trigger
though:

CREATE TABLE t (
a INT PRIMARY KEY,
b CHAR(1) NOT NULL)

GO
CREATE TRIGGER t_tbi ON t INSTEAD OF INSERT AS BEGIN
SET NOCOUNT ON
INSERT INTO t (a,b) (SELECT a,'X' FROM inserted)
END

GO
ALTER TABLE t ADD CONSTRAINT c CHECK ( b='x')
GO

INSERT INTO t(a,b) VALUES (1,'a')

GO
DROP TABLE t

--
Jacco Schalkwijk
SQL Server MVP
"Stuart McGraw" <sm********@friizz.RimoovAllZZs.com> wrote in message
news:11************@corp.supernews.com...
Is Microsoft full of #*$#*% (again) or am I badly misunderstanding
something?

Quote from Microsoft's T-SQL doc:
INSTEAD OF triggers are executed instead of the triggering action.
These triggers are executed after the inserted and deleted tables
reflecting the changes to the base table are created, but before any
other actions are taken. They are executed before any constraints,

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
so can perform preprocessing that supplements the constraint actions.

(SQL Server 2000 sp3a)

CREATE TABLE t (
a INT PRIMARY KEY,
b CHAR(1) NOT NULL)

I want to override the value of [b] with the value of 'X' when
inserting into t...

CREATE TRIGGER t_tbi ON t INSTEAD OF INSERT AS BEGIN
SET NOCOUNT ON
INSERT INTO t (a,b) (SELECT a,'X' FROM inserted)
END

Let's try it...

INSERT INTO t (a,b) VALUES(1,'z')
SELECT * FROM t
a | b
---|---
1 | X

Good, the trigger did what it was supposed to. Lets try a
slight variation...

INSERT INTO t (a) VALUES(2)
Server: Msg 233, Level 16, State 2, Line 1
The column 'b' in table 't' cannot be null.

WTF? What was that I just read about "[instead-of triggers]
are executed before any constraints"?!?!

What's going on here???

Jul 23 '05 #3
Stuart McGraw wrote:
Is Microsoft full of #*$#*% (again) or am I badly misunderstanding
something?

Quote from Microsoft's T-SQL doc:
INSTEAD OF triggers are executed instead of the triggering action.
These triggers are executed after the inserted and deleted tables
reflecting the changes to the base table are created, but before any
other actions are taken. They are executed before any constraints,


^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
so can perform preprocessing that supplements the constraint actions.


(SQL Server 2000 sp3a)

CREATE TABLE t (
a INT PRIMARY KEY,
b CHAR(1) NOT NULL)

I want to override the value of [b] with the value of 'X' when
inserting into t...

CREATE TRIGGER t_tbi ON t INSTEAD OF INSERT AS BEGIN
SET NOCOUNT ON
INSERT INTO t (a,b) (SELECT a,'X' FROM inserted)
END

Let's try it...

INSERT INTO t (a,b) VALUES(1,'z')
SELECT * FROM t
a | b
---|---
1 | X

Good, the trigger did what it was supposed to. Lets try a
slight variation...

INSERT INTO t (a) VALUES(2)
Server: Msg 233, Level 16, State 2, Line 1
The column 'b' in table 't' cannot be null.

WTF? What was that I just read about "[instead-of triggers]
are executed before any constraints"?!?!

What's going on here???


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Constraints are:

CREATE TABLE t (a char(1) CHECK (a <> 'z'))

The CHECK is the constraint that makes sure the column [a] cannot have
the value 'z' stored in it.

Other CONSTRAINTS are Primary Key, Foreign Key and Default.
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQmgjI4echKqOuFEgEQJXQwCfcU9SQ/m9WrZOEih+5db3sbiLg9AAnAky
oxV+MzTp3DTqgwvySrLyhtRc
=BGR+
-----END PGP SIGNATURE-----
Jul 23 '05 #4
NOT NULL is not a constraint, it is a property of the column definition, the same as the datatype.


Ehm, actually, "not null" IS a constraint. Apparently, it's not in MS SQL
speak, but it is a constraint, a "column level constraint" to be exact.

:-)
--
With regards,

Martijn Tonies
Database Workbench - developer tool for InterBase, Firebird, MySQL & MS SQL
Server
Upscene Productions
http://www.upscene.com
Jul 23 '05 #5
Sigh. Now that you mention it I have a vague recollection of
encountering this semantic difference in the past. I have been
working with Postgresql a lot lately so the distinction did not
occur to me. Thanks (even though it was not what I wanted
to hear!)

"Jacco Schalkwijk" <ja****************@to.newsgroups.mvps.org.invalid > wrote in message
news:Ou**************@TK2MSFTNGP10.phx.gbl...
NOT NULL is not a constraint, it is a property of the column definition, the
same as the datatype. INSERT INTO t(a,b) VALUES (1,'aa') will give an error
as well. The INSERT statement still get validated against the columns.

Check constraints, foreign keys etc don't get checked before the trigger
though:

CREATE TABLE t (
a INT PRIMARY KEY,
b CHAR(1) NOT NULL)

GO
CREATE TRIGGER t_tbi ON t INSTEAD OF INSERT AS BEGIN
SET NOCOUNT ON
INSERT INTO t (a,b) (SELECT a,'X' FROM inserted)
END

GO
ALTER TABLE t ADD CONSTRAINT c CHECK ( b='x')
GO

INSERT INTO t(a,b) VALUES (1,'a')

GO
DROP TABLE t

--
Jacco Schalkwijk
SQL Server MVP
"Stuart McGraw" <sm********@friizz.RimoovAllZZs.com> wrote in message
news:11************@corp.supernews.com...
Is Microsoft full of #*$#*% (again) or am I badly misunderstanding
something?

Quote from Microsoft's T-SQL doc:
INSTEAD OF triggers are executed instead of the triggering action.
These triggers are executed after the inserted and deleted tables
reflecting the changes to the base table are created, but before any
other actions are taken. They are executed before any constraints,

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
so can perform preprocessing that supplements the constraint actions.

(SQL Server 2000 sp3a)

CREATE TABLE t (
a INT PRIMARY KEY,
b CHAR(1) NOT NULL)

I want to override the value of [b] with the value of 'X' when
inserting into t...

CREATE TRIGGER t_tbi ON t INSTEAD OF INSERT AS BEGIN
SET NOCOUNT ON
INSERT INTO t (a,b) (SELECT a,'X' FROM inserted)
END

Let's try it...

INSERT INTO t (a,b) VALUES(1,'z')
SELECT * FROM t
a | b
---|---
1 | X

Good, the trigger did what it was supposed to. Lets try a
slight variation...

INSERT INTO t (a) VALUES(2)
Server: Msg 233, Level 16, State 2, Line 1
The column 'b' in table 't' cannot be null.

WTF? What was that I just read about "[instead-of triggers]
are executed before any constraints"?!?!

What's going on here???



Jul 23 '05 #6
SQL Books Online
- Creating and Maintaining Databases
- - Enforcing Business Rules with Triggers
- - - Designing Triggers
- - - - Trigger Execution
- - - - - (2nd paragraph)

maybe you were thinking of "after" triggers?

"Quentin Ran" <re***************@yahoo.com> wrote in message news:OW**************@TK2MSFTNGP14.phx.gbl...
Stuart,

I am not sure you got the right document. I can not locate your quote in
BOL, and the quote "These triggers are executed after the inserted and
deleted tables reflecting the changes to the base table are created, but
before any other actions are taken." is clearly not correct. MS is not that
stupid.

Can you provide the way you found the quote?

Quentin

"Stuart McGraw" <sm********@friizz.RimoovAllZZs.com> wrote in message
news:11************@corp.supernews.com...
Is Microsoft full of #*$#*% (again) or am I badly misunderstanding
something?

Quote from Microsoft's T-SQL doc:
INSTEAD OF triggers are executed instead of the triggering action.
These triggers are executed after the inserted and deleted tables
reflecting the changes to the base table are created, but before any
other actions are taken. They are executed before any constraints,

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
so can perform preprocessing that supplements the constraint actions.

(SQL Server 2000 sp3a)

[...snip...]
Jul 23 '05 #7
In Standard SQL, constraints can be declared so that you can defer
them until the end of the session and temporarily have an illegal state
in a block of code. The rule in SQL is that all constraints must be
enforces at the end of a session. So you override the rules until you
leave the unit of work and all of them are checked. It is executed
BEFORE constraints, not INSTEAD OF constraints.

Jul 23 '05 #8
That would be the DEFFERED keyword I guess, which I see in Oracle and
in Postgresql (for foreign key contraints only) but not in SQL Server 2k.

"--CELKO--" <jc*******@earthlink.net> wrote in message news:11**********************@g14g2000cwa.googlegr oups.com...
In Standard SQL, constraints can be declared so that you can defer
them until the end of the session and temporarily have an illegal state
in a block of code. The rule in SQL is that all constraints must be
enforces at the end of a session. So you override the rules until you
leave the unit of work and all of them are checked. It is executed
BEFORE constraints, not INSTEAD OF constraints.

Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: DCK | last post by:
Hello :) Into group-archive i found most e-mails, which touches PINGing. In my work i've used TELNET for testing if host is operational. Sometimes, for unknown reasons, workstation doesn't...
5
by: lukedigby | last post by:
Hi there. Could someone please help me. Is it possible to show addresses such: http://www.spanishinvest.com/list.php?area_id=5 like this instead:...
1
by: sandwich_eater | last post by:
The following code throws a compiler error: expected init-declarator before '<' token struct mystru { int yspace; std::string ymonkey; };
4
by: arotem | last post by:
Hi, I am trying to call an unbound method (PrintInput) with the object instance as the first argument but getting the following error: "TypeError: unbound method PrintInput() must be called with...
2
by: andrew | last post by:
C:\Documents and Settings\watts\My Documents\Visual Studio Projects\boeing\showPMACfg\vuePMAcfg\vuePMAcfg.cs(88): Static member 'vuePMAcfg.pmaDataHash.pmaDataHash1' cannot be accessed with an...
1
by: mp | last post by:
Hi, How we can use xml file instead data base table (read/write). Example: CustID | FirstName | lastName | etc. I am looking for some example. Thanks
4
by: khvatov | last post by:
Hi, How do I specidify to use asp.net 2.0 instead 1.1? Is there a setting in config file that I have to change? I'm new to asp.net 2.0. Please help. Thanks. Alex
7
by: fyleow | last post by:
Hi guys, I'm a student/hobbyist programmer interested in creating a web project. It's nothing too complicated, I would like to get data from an RSS feed and store that into a database. I want...
3
by: jdvon | last post by:
Reports and Queries return auto ID number instead record itself Access 2002 when I run a report or a query they result in the auto ID number (in some cases) rather than the value iteslf. What...
2
by: LinuxDuud | last post by:
Hi, I've made an MDI Application, And when I start the other form, It's menu appears in the parent form instead in the child form. How can I fix that? Thanks.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.