我有以下过程(SQL Server 2008 R2):
create procedure usp_SaveCompanyUserData
@companyId bigint,
@userId bigint,
@dataTable tt_CoUserdata readonly
as
begin
set nocount, xact_abort on;
merge CompanyUser with (holdlock) as r
using (
select
@companyId as CompanyId,
@userId as UserId,
MyKey,
MyValue
from @dataTable) as newData
on r.CompanyId = newData.CompanyId
and r.UserId = newData.UserId
and r.MyKey = newData.MyKey
when not matched then
insert (CompanyId, UserId, MyKey, MyValue) values
(@companyId, @userId, newData.MyKey, newData.MyValue);
end;
Run Code Online (Sandbox Code Playgroud)
CompanyId、UserId、MyKey 构成目标表的组合键。CompanyId 是父表的外键。此外,还有一个非聚集索引CompanyId asc, UserId asc
。
它是从许多不同的线程调用的,我一直在调用相同语句的不同进程之间出现死锁。我的理解是“with (holdlock)”对于防止插入/更新竞争条件错误是必要的。
我假设两个不同的线程在验证约束时以不同的顺序锁定行(或页面),因此是死锁。
这是一个正确的假设吗? …
我正在与需要将数据导入远程 SQL Server (2008) 实例的客户合作。将在其中进行导入的计算机上不能安装 SQL Server。BCP.exe 实用程序可以在没有安装 SQL Server 的情况下运行吗?
谢谢你的帮助。
我正在尝试设置一个简单的 SQL Server Service Broker 消息设置。我正在关注 MS 教程,但无法使以下代码正常工作。一切运行正常,但消息没有出现在队列中。任何人都可以找出问题所在。
谢谢你的帮助。
-- This query returns "1"
SELECT
Is_Broker_Enabled
FROM Sys.Databases
WHERE Name = 'ActivationTest2';
GO
-- Create the message types.
CREATE MESSAGE TYPE TestRequestMessage VALIDATION = WELL_FORMED_XML;
CREATE MESSAGE TYPE TestReplyMessage VALIDATION = WELL_FORMED_XML;
-- Create the contract.
CREATE CONTRACT TestContract (
TestRequestMessage SENT BY INITIATOR,
TestReplyMessage SENT BY TARGET) ;
-- Create the target queue and service.
CREATE QUEUE ActTestTgtQueue;
CREATE SERVICE ActTestTgtService ON QUEUE ActTestTgtQueue;
-- Create the initiator queue …
Run Code Online (Sandbox Code Playgroud)