0
A
回答
2
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
+1
开关箱这里有道理..谢谢德米特里。 – JPS
+0
很高兴帮助:) –
1
如果状态不不允许null,则每行是真还是假
update table set firstname = 'fname';
如果情况确实允许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';
相关问题
- 1. 基于ID更新DATE数据类型#
- 2. 如何更新数据视图中列的数据类型
- 3. 如何使用其他表格数据更新一列数据TSQL
- 4. 基于uniqueidentifier数据类型列的排序数据
- 5. 基于表中其他列数据的SQL查询更新
- 6. TSQL根据数据类型为每个列名称更新标记
- 7. 如何基于Haskell Groundhog的数据库类型指定不同的列类型?
- 8. 如何更改德比数据库中列的数据类型?
- 9. 基于SWRL规则更新数据类型属性值
- 10. SQL Server - 基于SELECT的更新数据
- 11. 熊猫:基于更大的数据框更新Dataframe的系列
- 12. 如何在Django中更新列数据类型
- 13. 新列表数据类型
- 14. 如何基于不同数据库中的值访问数据库? (TSQL)
- 15. 基于其他列表的数据库显示数据laravel 5
- 16. 如何基于数据框中其他列中的值创建新列?
- 17. 如何更改pyspark数据帧列数据类型?
- 18. 数据更新,但基于数据的D3表不
- 19. 更改列的数据类型在PySpark数据帧
- 20. 基于数据类型的ASP.Net GridView的格式列
- 21. 如何根据收到的数据更改数据类型
- 22. 如何将数据帧中每列的数据类型存储到新数据帧的新列
- 23. 如何筛选出基于其他列的数据
- 24. 从其datagridview更新对应于数据库的数据集
- 25. 如何将数据插入位数据类型的字段?
- 26. 如何在C#中更新数据类型的值
- 27. 如何在SQL Server的varcher数据类型列中输入nvarchar数据类型?
- 28. 更改存在于多个表中的列数据类型
- 29. 更改表列的数据类型
- 30. 更改列的数据类型
' UPDATE YourTable SET FirstName ='Foo',LastName ='Bar'Where Status In('True','False')'? – Siyual