如何更新满足SQL Server中SELECT语句内条件的特定行的值

时间:2013-03-19 08:48:46

标签: sql sql-server sql-server-2012

我有像“Bed”和“PatientRecord”这样的表格。我想为病人分配一张空床(即来自Bed table的bID,状态=“可用”),然后将该bID状态更改为“Occupied”。请帮忙。以下是我当前的查询,但我不确定如何继续。

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' 

6 个答案:

答案 0 :(得分:3)

我认为您发现这种困难的原因是因为您的设计存在缺陷,但没有足够的信息来纠正这一点。我不确定你试图给哪一位病人,在下面我已经使用@PatientIdToGiveBed来指明在你的例子中你会给所有'病人'同一张床。您可能需要修改此选项以选择第一个“患者”,就像选择床一样。

事务需要具有REPEATABLE READ的隔离级别,这将阻止两个事务分配相同的床,如果您使用默认的READ COMMITTED,则两个事务可以选择相同的床。这样,READ 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

答案 1 :(得分: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

答案 2 :(得分: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条记录更新为相同的床位ID。换句话说,每次新患者入院时,每位患者都会被重新分配到下一张病床。那将是一张非常拥挤的床。

答案 3 :(得分:2)

如果你真的想同时在一组患者中运行它,你需要首先匹配床,然后分配它们,因为SQL Server无法一次更新两个表,所以你的基于集合的选择每次都会产生不同的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)

Demo

答案 4 :(得分:2)

  1. 您无法在单个查询中更新2个表(您需要使用交易)

  2. 你可以尝试这样的事情

  3. 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
    

答案 5 :(得分:0)

这将使用Admission_Type = In Patient

更新所有PatientRecords

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

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