Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««1234»»»

INSTEAD OF trigger and OUTPUT clause Expand / Collapse
Author
Message
Posted Thursday, May 13, 2010 5:58 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, September 4, 2014 8:51 AM
Points: 35, Visits: 80
I selected "Returns an error". It didn't seem right, but I'm not sure why the following code works

INSERT TestTable (string)
OUTPUT inserted.*

Doesn't the select return more columns (ID and String) than the insert list? That is one of my more common errors...

Great question, BTW. I need to learn more about OUTPUT!
Post #921163
Posted Thursday, May 13, 2010 7:15 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Friday, May 9, 2014 12:47 AM
Points: 3,448, Visits: 4,407
tlewis-993411 (5/13/2010)
I'm not sure why the following code works

INSERT TestTable (string)
OUTPUT inserted.*

Doesn't the select return more columns (ID and String) than the insert list?

When a row is inserted into a table, SQL Server fills all columns in that row. All these columns are contaned in the 'inserted.*' construct. Some of these values are explicit, and some are implicit (NULL, identity, default value, computed value etc).

Here is an example:
create table #t
( id int identity,
a varchar(10),
b varchar(10) default 'hello',
c varchar(10),
computed_column as b + ' ' + c
)

insert #t (c)
output inserted.*
values ('test')

In the QOTD, the construct 'OUTPUT inserted.*' is equal to 'OUTPUT inserted.id, inserted.string'. 'Inserted.string' is an explicit column, while 'inserted.id' is an impilcit column.
Post #921234
Posted Thursday, May 13, 2010 7:46 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, September 4, 2014 8:51 AM
Points: 35, Visits: 80
My problem is that I expected that "inserted" would be a complete copy of the inserted row (I haven't used OUTPUT, but triggers certainly work that way). As such, the Inserted.* should return 2 columns, but the insert list only contains one field. See the code below and the resulting error message. This is almost exactly the same construct.

I'll do some research, but it appears that Output is only returning the "explicitly added" columns?

BTW, generically, I'm opposed to * for this very reason!

Declare @foo Table
(
ID int,
String varchar(100)
)

Insert into @Foo(String)
Select * from
(Select 1 as intval, 'sample1' as string
union all
Select 2 as intval, 'sample2' as string
) as bar

Msg 121, Level 15, State 1, Line 7
The select list for the INSERT statement contains more items than the insert list. The number of SELECT values must match the number of INSERT columns.
Post #921263
Posted Thursday, May 13, 2010 7:47 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 7:41 AM
Points: 1,931, Visits: 1,014
Toreador (5/13/2010)
Interesting - it never occurred to me that the identity values wouldn't be available in the inserted table!
So how would you get the identity values of the rows you'd just inserted?


If you are using an instead of trigger, I'm not sure. However, not using an instead of trigger works fine to retrieve the identity values using OUTPUT. I ran the below on SQL Server 2008.

CREATE TABLE Test
(
ID int identity(1,1) NOT NULL PRIMARY KEY,
AnotherColumn varchar(50) NULL
)
GO

CREATE TRIGGER Test_Insert
ON Test
AFTER INSERT
AS
SELECT 'Trigger Executed'
GO

DECLARE @tTableVar table(TestID int NOT NULL,
TestAnotherColumn varchar(50) NULL)


INSERT INTO Test(AnotherColumn)
OUTPUT inserted.ID, inserted.AnotherColumn
INTO @tTableVar(TestID,TestAnotherColumn)
SELECT 'Blah'
UNION ALL
SELECT 'Blah2'

SELECT TestID,TestAnotherColumn FROM @tTableVar

DROP TABLE Test

Post #921264
Posted Thursday, May 13, 2010 7:55 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, September 4, 2014 8:51 AM
Points: 35, Visits: 80
Duh - The "Into @Tab" is not using the Select, but instead the output, which does include two columns.

Select * into a table (or Output * into table, in this case) only works if the fields are selected in the same order and same count as the target, which is obviously true in this case. However, I would still avoid the * if possible, explicitly naming the fields instead. Otherwise, a change to the target table would break that condition.

Thanks for putting up with me working through my own question. That's how I learn almost everything!
Post #921272
Posted Thursday, May 13, 2010 7:58 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 12:59 PM
Points: 2,394, Visits: 18,032
HA HA HA! I took a guess (which was wrong), but checked by running the code before I answered (some consider this cheating, I consider it "using all available resources for the task at hand"). But I forgot to change my answer before submitting, so I still got it wrong! Serves me right, I guess!

Chad
Post #921276
Posted Thursday, May 13, 2010 8:02 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, November 25, 2014 11:59 PM
Points: 894, Visits: 889
Wow, this was such a good question, I wish I'd written it! I learned at least three things from it, so I don't mind having gotten it wrong.

-----
a haiku...

NULL is not zero
NULL is not an empty string
NULL is the unknown
Post #921282
Posted Thursday, May 13, 2010 8:34 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: 2 days ago @ 4:10 PM
Points: 554, Visits: 1,208
Toreador (5/13/2010)
Interesting - it never occurred to me that the identity values wouldn't be available in the inserted table!
So how would you get the identity values of the rows you'd just inserted?


Change the trigger as follows. Of course, I'm not sure how you would capture the output result set, save using the temp table method as previously mentioned, but then the caller would have to know to look for that temp table.
CREATE TRIGGER TestTrigger ON TestTable
INSTEAD OF INSERT
AS
INSERT TestTable (string)
OUTPUT INSERTED.*
SELECT CASE WHEN string = 'TestString' THEN string ELSE 'Stub' END
FROM inserted

Excellent question, I definitely learned something today!
Post #921320
Posted Thursday, May 13, 2010 10:03 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, September 5, 2014 2:00 PM
Points: 2,160, Visits: 2,191
Great question and learned a couple things in the process, a very good start to a day.

Thanks!
Post #921435
Posted Thursday, May 13, 2010 11:19 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 9:21 AM
Points: 17,977, Visits: 15,981
Thanks for a great question.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Post #921497
« Prev Topic | Next Topic »

Add to briefcase ««1234»»»

Permissions Expand / Collapse