views:

155

answers:

1

I'm playing with the Output keyword in SQL Serer 2005 Express. I've written the following query:

Declare @tempTable as Table(masterLotDB datetime)
Insert  into dbo.tblMasterLot (RecordCreation)
Values ('2009-10-02')
OUTPUT INSERTED.RecordCreation
into @tempTable

I get a syntax error of Msg 102, Level 15, State 1, Line 6 Incorrect syntax near 'OUTPUT'. I've tried various combinations.

The query works without the OUTPUT stuff (i.e. it puts a new record in tblMasterLot, There is nothing special about tblMasterLot.RecordCreation other than being defined 'not null'. It's not a primary key or identity.

  1. What's going on? Any chance 'OUTPUT' is not really supported in SQL Express 2005?
  2. Any tutorials on OUTPUT? (especially in conjuntion with Insert).
+1  A: 

The OUTPUT clause should come before VALUES

See Example A here

Following up: I reproduced your original 'incorrect syntax near OUTPUT' error using your code. However, by changing the order of the clauses, it works fine. Here's my code:

create table #tmp_test (recordCreation datetime)

Declare @tempTable as Table(masterLotDB datetime)

Insert  into #tmp_test (RecordCreation)
OUTPUT INSERTED.RecordCreation into @tempTable
Values ('2009-10-02')

Output: (1 row(s) affected)

sidereal
I don't believe that is the answer. In this case Values corresponds to what you are putting in the first table ('tblMasterLot'). However, I think it's related to this. I saw that the MSDN documentation often shows the use of VALUES after OUTPUT. But what specifically would I put in this case? I did try your suggestion and got an error of "Incorrect syntax near the keyword 'into'."
Dave
Thanks, that clears it up.
Dave