Wednesday, April 16, 2008

OUTPUT clause in INSERT/UPDATE/DELETE statements

just playing with the new (for sql server 2005 and for me) OUTPUT clause.
I had some hopes for it but turns out i cant write something like


insert targettable (col1,col2)
output Inserted.col1, sourcetable.somecol
select somecol1,somecol2 from sourcetable



that is - i cant output values from the Inserted and also values from the source table :(
a bit disappointing in a way, of course, because i wanted to have backwards mapping between the source and inserted tables, but now I just decided to use one of the columns in the inserted table as this mapping ids placeholder

voila...

EDIT:
according to msdn article on T-SQL Delete
it is possible to OUTPUT exactly what I need...


DELETE Production.ProductProductPhoto
OUTPUT DELETED.ProductID,
p.Name,
p.ProductModelID,
DELETED.ProductPhotoID
INTO @MyTableVar
FROM Production.ProductProductPhoto AS ph
JOIN Production.Product as p
ON ph.ProductID = p.ProductID
WHERE p.ProductModelID BETWEEN 120 and 130;



strange...now I'll have to achieve the same results somehow...

Friday, April 11, 2008

Server: Msg 7347, Level 16, State 1, Line 1 OLE DB provider 'SQLOLEDB' returned an unexpected data length for the fixed-length column

I got the error described here http://support.microsoft.com/kb/920930
when running a test select * from a view on a linked server





Server: Msg 7347, Level 16, State 1, Line 1 OLE DB provider 'SQLOLEDB' returned an unexpected data length for the fixed-length column '[LinkedServerName].[DBName].[OwnerName].[TableOrViewName].ColumnName'. The expected data length is n, while the returned data length is m.





when searching on google web/groups the compains were for sql server 2000 and below so not much help until i decided run the same query on the linked server itself and it went through without a problem.
turned out the column in question (the one that reportedly returned larger data than expected) is empty.
so i just rewrote the query to return only a subset of data i needed and all worked nicely

so...fun...