如何根据 T-SQL 中具有位数据类型的列更新数据

How to update the data based on column which has bit data type in T-SQL

使用 3 列(名字、姓氏、状态)测试 Table

我想在单个 UPDATE 查询中更新所有行的名字和姓氏以获取任何状态,无论是真还是假。这个怎么写?

SQL Server bit data type 为 true/false 值存储 1/0,并将字符串 'true' 和 'false' 分别转换为 1 和 0。因此,您可以根据 '0' 或 '1',或 'False' 或 'True'

进行查询

如果您希望逻辑使用单个更新查询来更新所有行,并根据 [Status] 列的值进行不同的更新,那么您需要一个 case 语句:

有多种方法可以做到这一点,其中的一些例子可以在下面的例子和结果中看到:

-- Let's make a test table that mimicks your schema and put some data in it    
-- We will use a function to re-make the table
Create Function MakeTestTable ()
Returns @Test Table (
    FirstName varchar(50), 
    LastName varchar(50), 
    [Status] bit
)
As Begin
    Insert Into @Test Values 
            ('John', 'Doe', 'false'), 
            ('Jane', 'Dane', 'true'),
            ('Space', 'Cadet', Null)
    Return 
End
Go

-- Our local test table variable for examples:
Declare @Test Table (
    FirstName varchar(50), 
    LastName varchar(50), 
    [Status] bit
)

-- Let's seed the test table
Insert Into @Test Select * From MakeTestTable()

-- Verify initial Values 
Select * From @Test

-- Single update statement depending on [Status] value using Case statement:
Update @Test 
Set FirstName = 
    Case -- Here we use 1/0 for True/False, respectively 
        When [Status] = 1 Then 'FirstName updated while Status was True' 
        When [Status] = 0 Then 'FirstName updated while Status was False'
        Else 'FirstName updated while Status was Null'
    End,
    LastName = 
    Case -- Here we use 'True' and 'False' which SQL converts to 1 and 0, respectively
        When [Status] = 'True' Then 'LastName updated while Status was True' 
        When [Status] = 'False' Then 'LastName updated while Status was False'
        Else 'LastName updated while Status was Null'
    End

-- Verify our updates:
Select * From @Test

-- Reset for next example:
Delete @Test; Insert Into @Test Select * From MakeTestTable()

-- Single update statement based on [Status] value not being null using Case statement:
Update @Test 
Set FirstName = 
    Case -- Here we update if the value of [Status] is either 0 or 1 using In clause
        When [Status] In (0,1) Then 'FirstName updated while Status was True or False' 
        Else 'FirstName updated while Status was neither 0 nor 1'
    End,
    LastName = 
    Case -- Here we base our update on [Status] being null (reverse of the above)
        When [Status] Is Not Null Then 'LastName updated while Status was Not Null' 
        Else 'LastName updated while Status was Null'
    End

-- Verify our updates:
Select * From @Test

-- Reset for next example:
Delete @Test; Insert Into @Test Select * From MakeTestTable()

-- Finally, update all columns based on status not being null using Where clause:
-- Without the CASE statement, the WHERE clause is applied to the entire update,
-- The code is simpler, but you lose the ability to update each column based on a 
-- different condition.
Update @Test 
Set FirstName = 'FirstName updated while Status is not Null',
    LastName = 'LastName updated while Status is not Null'
Where [Status] Is Not Null

-- Verify our updates:
Select * From @Test

-- Clean up
Drop Function MakeTestTable 

如果 Status 不允许为 null,则每一行都是 true 或 false

update table set firstname = 'fname';

如果 Status 确实允许 null,则

update table set firstname = 'fname' where status is not null;

如果您对不同的值有不同的更新,那么只需使用两个更新

update table set firstname = 'fnameT' where status = 'true'; 
update table set firstname = 'fnameF' where status = 'false';