sql - 更新表并添加新列,检查用户名编号的所有行 | SQL 服务器
问题描述
我需要添加这个脚本
ALTER TABLE [dbo].[MyTable]
ADD [MyNewColumnCheckUserName] BIT NOT NULL DEFAULT 0;
MyTable
但是更新后,如果 UserName 只有数字,我需要检查所有旧行- 所有 UserName(它是此表中的列)?如果是 - 将此行设置为 true MyNewColumnCheckUserName
。
解决方案
您可以使用:
update mytable
set MyNewColumnCheckUserName = (case when UserName like '%[^0-9]%' then 0 else 1 end);
但是,我建议将其添加为计算列:
alter table mytable add MyNewColumnCheckUserName as
(case when UserName like '%[^0-9]%' then 0 else 1 end);
我知道没有理由更喜欢位而不是计算列的数字。但是如果你真的想招致 a 的开销bit
,你可以转换这个值:
alter table mytable add MyNewColumnCheckUserName as
(convert(bit, (case when UserName like '%[^0-9]%' then 0 else 1 end));
推荐阅读
- javascript - 如何使自定义块形状成块?
- c# - 保存转换后的邮件地址时,为什么我的数据库中有空值?
- python - 使用 selenium 执行事件和抓取
- c - 使用 fork() 的 Linux 进程树
- django - 在 Apache 服务器上提供的 Django 站点不起作用,日志没有错误
- python - Python - 动态创建的字典无法返回或添加到另一个字典
- html - 如何在 Angular2 (7) 中使用 NgModel 在 Select/Option 中设置占位符?
- reactjs - 语义用户界面反应:Form.Select 中的大型数据集是否会使表单变慢?
- angular - 角度,在 Array.map 中等待函数结束继续
- r - 如何从数据表单元格单击中观察动态生成的 UI?