5

我有像“Bed”和“PatientRecord”这样的表格。我想为患者分配一张空床(即状态为“可用”的床表中的出价),然后将该出价状态更改为“已占用”。请帮助解决这个问题。以下是我当前的查询,但我不确定如何继续。

update Patient_Record 
set b_ID = (select top 1 b_ID from bed where Status_Avai_Occ = 'Available')
/*i want to change the status of this b_ID to "Occupied"*/
where Admission_Type = 'In Patient' 
4

6 回答 6

3

我认为您发现这很困难的原因是您的设计存在缺陷,但没有足够的信息来纠正它。我不确定您要为哪个患者提供床位,在下面我使用@PatientIdToGiveBed来指定,如在您的示例中,您将为所有“患者”提供同一张床。您可能需要修改它以选择第一个“In Patient”,就像选择床一样。

事务需要有一个隔离级别,REPEATABLE READ可以防止两个事务分配同一张床,如果你使用默认值READ COMMITTED,两个事务可以选择同一张床。这样,aREAD LOCK就被保留在选定的床上,直到交易完成。

您可以将下面的整个语句放入 SSMS 并运行以对其进行测试。

DECLARE @Bed TABLE  (
    b_ID INT,
    Status_Avai_Occ VARCHAR(20)
)

DECLARE @Patient_Record TABLE (
    Id INT, 
    Name VARCHAR(10),
    b_ID INT
)

INSERT INTO @Bed VALUES (1, 'Available') 
INSERT INTO @Bed VALUES (2, 'Available') 
INSERT INTO @Bed VALUES (3, 'Available') 

INSERT INTO @Patient_Record VALUES (1, 'Adam', NULL) 
INSERT INTO @Patient_Record VALUES (2, 'Ben', NULL) 
INSERT INTO @Patient_Record VALUES (3, 'Charles', NULL) 

DECLARE @PatientIdToGiveBed INT = 1

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ 
BEGIN TRANSACTION 
    DECLARE @Available_BID INT
    SELECT TOP 1 @Available_BID = b_ID FROM @BED WHERE Status_Avai_Occ = 'Available' ORDER BY b_ID

    IF @Available_BID IS NULL 
    BEGIN
        Print 'No Beds Available'
        COMMIT 
        RETURN 
    END 

    UPDATE @Patient_Record SET b_ID = @Available_BID WHERE Id = @PatientIdToGiveBed 
    UPDATE @Bed SET Status_Avai_Occ = 'Occupied' WHERE b_ID = @Available_BID 
COMMIT 

SELECT * FROM @Patient_Record
SELECT * FROM @Bed
于 2013-03-19T09:20:08.390 回答
2

SQL Server 不允许您在单个 UPDATE 语句中更新多个表,但是您可以简单地将两个更新包装在单个事务中,如下所示:

DECLARE @b_ID INT
SELECT TOP 1 @b_ID = b_ID FROM bed WHERE Status_Avai_Occ = 'Available'

BEGIN TRAN Update_Patient

UPDATE  Patient_Record 
  SET   b_ID = @b_ID
  WHERE Admission_Type = 'In Patient' 

UPDATE  bed
  SET   Status_Avai_Occ = 'Occupied'
  WHERE (b_ID = @b_ID)

COMMIT TRAN Update_Patient
于 2013-03-19T08:57:52.253 回答
2
1. set variable "openBed" to available bed id
2. update bed with bed id = openBed to 'Occupied'
3. update patient_record set bed id = openBed where admission_type = 'in patient'

注意:第三条语句In Patient每次都会将表中的每条记录更新Patient_Record为相同的床号。换句话说,每次有新患者入院时,每位患者都将被重新分配到下一个可用床位。那将是一张非常拥挤的床。

于 2013-03-19T08:58:17.583 回答
2

如果您真的想一次对一组患者运行此操作,则需要首先匹配床位,然后分配它们,因为 SQL Server 中无法同时更新两个表,因此您的基于集合的select 每次都会产生不同的 bed_ID。

-- Set up some temp tables to hold data
DECLARE @availBeds table (num int identity(1,1), b_ID int)
DECLARE @inPatients table (num int identity(1,1), p_ID int)
DECLARE @patientBeds table (p_ID int, b_ID int)

-- Populate table of available beds only, giving them a unique incremental ID
INSERT INTO @availBeds SELECT b_ID FROM Bed WHERE status_avai_occ = 'Available'

-- Populate table of in patients without assigned beds only, giving them a unique incremental ID
INSERT INTO @inPatients SELECT p_ID FROM Patient_Record WHERE admission_type = 'In Patient' AND b_ID IS NULL

-- Join the two tables above based on their incremental ID's,
-- effectively matching up available beds with patients
INSERT INTO @patientBeds
    SELECT p.p_ID, b.b_ID FROM @inPatients p
    INNER JOIN @availBeds b ON b.num = p.num

-- Update Patient_Record with the new bed ID's
UPDATE p
SET p.b_ID = b.b_ID
FROM Patient_Record p
INNER JOIN @patientBeds b ON p.p_ID = b.p_ID

-- Set the Beds to Occupied
UPDATE Bed SET status_avai_occ = 'Occupied' WHERE b_ID IN (SELECT b_ID FROM @patientBeds)

演示

于 2013-03-19T09:20:57.393 回答
2
  1. 您不能在单个查询中更新 2 个表(您需要使用事务)

  2. 你可以试试这样的

Begin Tran

update Patient_Record 
set b_ID = b.b_ID from bed b where b.Status_Avai_Occ = 'Available'
and Patient_Record.Admission_Type = 'In Patient'

Update bed set Status_Avai_Occ='Occupied' where b_ID in (select b_ID from Patient_Record where Patient_Record.Admission_Type = 'In Patient')
Commit Tran
于 2013-03-19T09:22:08.067 回答
0

这将使用 Admission_Type = 更新所有 PatientRecordsIn Patient

我不确定我是否完全理解你的结构,但你可以这样做:

DECLARE @AvailableBID INT
SELECT TOP 1 @AvailableBID = b_ID FROM bed WHERE Status_Avai_Occ = 'Available'

UPDATE Patient_Record 
SET b_ID = @AvailableBID
WHERE Admission_Type = 'In Patient'

UPDATE bed
SET Status_Avai_occ = 'Occupied'
WHERE b_ID = @AvailableBid

请注意,这仍将使用 Admission_Type In Patient 更新所有 Patient_records。您将需要应用额外的过滤器,即行号。

http://sqlfiddle.com/#!6/fa26d/9

从评论中编辑

DECLARE @AvailableBID INT
DECLARE @AvailablePatientRecord INT

SELECT TOP 1 @AvailableBID = b_ID FROM bed WHERE Status_Avai_Occ = 'Available'
SELECT TOP 1 @AvailablePatientRecord  = PatientRecordId FROM Patient_Record WHERE Admission_Type = 'In Patient'

UPDATE Patient_Record 
SET b_ID = @AvailableBID
WHERE PatientRecordId = @AvailablePatientRecord

UPDATE bed
SET Status_Avai_occ = 'Occupied'
WHERE b_ID = @AvailableBid

注意:您必须确保在 Patient_Record 表中有 PatientRecordId

于 2013-03-19T08:55:10.850 回答