We are seeing 'interesting behavior with our SQL Sever Database. We have a merge statement which selects a table X. In the match clauses there is a subselect to table X. When we execute the stored procedure from the SQL Server Tools it works fine. But when executed from IPC (an ETL Tool) we get an exception Invalid object name 'X'.
So far nothing special as I understand there can get lots of things wrong with permissions and stuff.
The strange thing: The merge statement is in a try block and in the catch block the error message gets written into the table X via an update statement! How is this possible when Sql Server complains it can't find a table X?
Also everything works fine with another stored procedure which is constructed in the same way (via code generation) but on a different set of tables.
The code looks like this
merge ...
using
(select ...
from dbo.X
where ...
when not matched by target
and not exists (select 1 from dbo.X q2 where ...)
then insert (...
)
values (...
)
when matched and q.ACTION='D'
then delete
when matched AND NOT exists (select 1 from dbo.X q3 where ...)
then update
set
...
OUTPUT $action INTO @l_SummaryOfChanges;
-- Query the results of the table variable.
SELECT ACTION, COUNT(*) AS CountPerChange
FROM @l_SummaryOfChanges
GROUP BY ACTION;
end try
begin catch
update dbo.X
set LAST_ERROR_MSG=ERROR_MESSAGE(), ERROR_COUNTER=ERROR_COUNTER+1
where SYNC_ID=@l_SyncID
end catch
Any ideas what is going on?Invalid object name 'sync$_tabTeiledaten'.