sql server - openquery apears to be rolled back when done -
i'm using following query.
select * openquery(exitweb,n'set nocount on; declare @result table (id int); insert [system_files] ([is_public], [file_name], [file_size], [content_type], [disk_name], [updated_at], [created_at]) output inserted.id @result(id) values (n''1'',n''7349.jpg'',n''146921'',n''image/jpeg'',n''5799dcc8a1eb1413195192.jpg'',n''2016-07-28 10:22:00.000'',n''2016-07-28 10:22:00.000'') declare @id int = (select top 1 id @result) select * system_files id = @id insert linktoexternal (id, id_ext) values(@id, 47) --select @id ')
when perform select within query works fine:
but when go check database when call has finished, record no longer there.
so i'm suspecting transaction rolled back. question is: why. can prevent transaction rolled if that's case.
well, always, after days of struggling , me post question on stackoverflow find solution: http://www.sqlservercentral.com/forums/topic1128997-391-1.aspx#bm1288825
i having same problem , gave on have found answer problem. reading article sharing data between stored procedures discovered openquery issues implicit transaction , rolling insert. had add explicit commit stored procedures, in additional discovered if use in query has union has commited twice. since i'm doing insert inside begin try can commit twice , not worry whether being used in union. i'm returning different values if there error apart of debugging.
select top 5 * mm join openquery([localserver], 'exec cms60.dbo.sp_recordreportlastrun ''lps'', ''test''') rptstats on 1=1 alter procedure [dbo].[sp_recordreportlastrun] -- add parameters stored procedure here @libraryname varchar(50), @reportname varchar(50) begin -- set nocount on added prevent result sets interfering select statements. set nocount on; -- insert statements procedure here begin try insert cms60.dbo.reportstatistics (libraryname, reportname, rundate) values (@libraryname, @reportname, getdate()) -- commit; --needed because openquery starts implicit transaction doesn't commit it. commit; --need second commit when used in union , although throws error when not used in union doesn't cause problem. end try begin catch select 2 test end catch select 1 test end
in case, adding ;commit;
after inserts solved it, , made sure got written database.
Comments
Post a Comment