繁体   English   中英

更新表并添加新列,检查用户名编号的所有行 | SQL服务器

[英]Update table and add new column with check for all rows for UserName numbers | SQL Server

我需要添加这个脚本

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);

我知道没有理由更喜欢位而不是计算列的数字。 但是如果你真的想招致bit的开销,你可以转换这个值:

alter table mytable add MyNewColumnCheckUserName as 
    (convert(bit, (case when UserName like '%[^0-9]%' then 0 else 1 end));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM