Default Value based on a if command
I have a table with one String column. I want to insert another column null that the value of the lines already inserted on the DB will be filled with a "y" or "n" based if the String column is filled or not. Is it possible to do?
actually it won't have a default value. i just need to know if is possible to set these values based on a if command
您可以添加一个计算列:
ALTER TABLE
mytable
ADD is_string_filled AS (CASE WHEN string_field IS NULL THEN 'N' ELSE 'Y' END)
//You can implement this in a select query as well instead of adding a column.
select strColumn,
CASE
WHEN strColumn is null THEN 'N'
ELSE 'Y'
END as colCheck
FROM TABLE1;
You can know more about using conditional clause in sql out here : How do I perform an IF...THEN in an SQL SELECT?
Calculating the above problem with a slect query rather than adding a column would be preferable. You can make a view from the select query if you need tabular representation.
Use Computed Columns ...
http://msdn.microsoft.com/en-us/library/ms191250.aspx
链接地址: http://www.djcxy.com/p/94362.html上一篇: 在SQL Server中删除部分日期时间的最佳方法
下一篇: 基于if命令的默认值