我有这个存储过程,如果我手动执行它。但有时我会在运行此存储过程时看到超时异常。这是由于MERGE,http://www.mssqltips.com/sqlservertip/3074/use-caution-with-sql-servers-merge-statement/
ALTER PROCEDURE [dbo].[InsertOrUpdateMobileUser]
(
@ID BIGINT
,@Name NVARCHAR(255)
)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @TranCount INT;
SET @TranCount = @@TRANCOUNT;
BEGIN TRY
IF @TranCount = 0
BEGIN TRANSACTION
ELSE
SAVE TRANSACTION InsertOrUpdateMobileUser;
MERGE INTO MobileUsers MU
USING (SELECT @ID AS ID) T ON (MU.ID = T.ID)
WHEN MATCHED THEN
UPDATE SET [Name] = CASE WHEN @Name IS NULL OR @Name = '' THEN [Name] ELSE @Name END
WHEN NOT MATCHED THEN
INSERT ([Name])
VALUES (@Name)
SELECT *
FROM MobileUsers
WHERE ID = @ID;
LBEXIT:
IF @TranCount = 0
COMMIT;
END TRY
BEGIN CATCH
DECLARE @Error INT, @Message VARCHAR(4000), @XState INT;
SELECT @Error = ERROR_NUMBER() ,@Message = ERROR_MESSAGE() ,@XState = XACT_STATE();
IF @XState = -1
ROLLBACK;
IF @XState = 1 AND @TranCount = 0
rollback
IF @XState = 1 AND @TranCount > 0
ROLLBACK TRANSACTION InsertOrUpdateMobileUser;
RAISERROR ('InsertOrUpdateMobileUser: %d: %s', 16, 1, @error, @message) ;
END CATCH
END
答案 0 :(得分:1)
您可以区分两种执行方法。它们究竟是什么?你的意思是,如果只运行程序的代码它是有效的,并且当你执行proc时它不起作用?它通过EXECUTE工作,并在工作中失败?
超时仅涉及客户端应用程序。您在.NET中有SqlCommand .CommandTimeout属性,而在Management Studio中有Tools> Options> Query Execution> Command Timeout。如果你有一份工作,那么它应该无限运行,甚至没有选择在Sql Server Agent中设置超时。