SQL Server bit data type хранит 1/0 для значений true/false и преобразует строки «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
' UPDATE YourTable SET FirstName = 'Foo', LastName = 'Bar', где Status In ('True', 'False') '? – Siyual