|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Saturday, December 15, 2012 9:45 AM
Points: 35,
Visits: 77
|
|
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!
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Thursday, June 13, 2013 8:50 PM
Points: 3,208,
Visits: 4,178
|
|
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.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Saturday, December 15, 2012 9:45 AM
Points: 35,
Visits: 77
|
|
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.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Friday, June 14, 2013 7:24 AM
Points: 1,543,
Visits: 914
|
|
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
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Saturday, December 15, 2012 9:45 AM
Points: 35,
Visits: 77
|
|
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!
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 1:31 PM
Points: 2,558,
Visits: 17,421
|
|
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
|
|
|
|
|
Say Hey Kid
      
Group: General Forum Members
Last Login: Today @ 5:48 AM
Points: 661,
Visits: 698
|
|
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
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Yesterday @ 10:18 AM
Points: 551,
Visits: 1,153
|
|
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!
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 2:49 PM
Points: 2,163,
Visits: 2,150
|
|
Great question and learned a couple things in the process, a very good start to a day.
Thanks!
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Today @ 5:03 PM
Points: 18,853,
Visits: 12,438
|
|
|
|
|