分布式交易的處理
1.檢查全局臨時表是否存在 方法1: if exists(select * from tempdb.dbo.sysobjects where name='##tblCT') print '存在' else print '不存在' 方法2: IF object_id('tempdb..##tblCT') is not null print '存在' else print '不存在'
2.檢查局部臨時表是否存在 IF object_id('tempdb..#tblCT') is not null print '存在' else print '不存在'
3.存儲過程調用存儲過程,並取得返回值 CREATE PROCEDURE [dbo].[tempProce] ( @idno numeric(18,0), @ReturnValue nvarchar(100) output--一定要設為output ) as BEGIN set @ReturnValue='返回值' END GO
調用方法: declare @Return nvarchar(100) exec tempProce 10 ,@Return output--一定也要加output select @Return
另:判斷@Return是否不等於null的方法, if @Return is not null, 不能這樣寫 if @Return<>null
4.查詢其它數據庫 方法1: select * from OPENDATASOURCE('SQLOLEDB','DRIVER={SQL Server};SERVER=it-srv-erpdb;UID=sa;PWD=123456;').fd_db.iemis.tblCT
方法2: SELECT a.* FROM OPENROWSET('SQLOLEDB','IT-SRV-ERPDB';'sa';'123456', 'SELECT * FROM fd_db.iemis.tblCT') AS a
方法3: sp_addlinkedserver 'IT-SRV-ERPDB', ' ', 'MSDASQL', NULL,NULL,'Driver={SQL Server};Database=ERPDB;Server=IT-SRV-ERPDB;UID=iemis;PWD=123456;'
select * from [IT-SRV-ERPDB].[fd_db].[iemis].[tblCT]
5.做分布式交易,首先需開啟msdtc服務.但接下來還是踫到錯誤.環境是win2000 server sp4+sql2000 sp4.其它幾台電腦沒問題,不知道是不是server版的原因,網上有很多關於win2003的方案. 打开注册表,HKEY_LOCAL_MACHINE/Software/Microsoft/MSDTC AllowonlySecureRpcCalls=0 TurnoffRpcSecurity=1