我有一个status_flag为int的表。我想status_flag有空值。我从视图vwtable中提取其他值(vwtable中没有status_flag值) 我在这里错了。请帮忙。我正在使用sql server 2008。
INSERT INTO dbo.Data(id, last_name,first_name,middle_init,alternate,birthdate_month,birthdate_day,birthdate_year,gender,status_flag)
select( [id],[last_name],[first_name],[middle_init],[alternate],[birthdate.month],[birthdate.day][birthdate.year],[gender],
coalesce(cast (status_flag as varchar(1)),'') as status_flag
FROM vwtable
答案 0 :(得分:1)
您的COALESCE表达式求值为varchar,该值无法插入到status_flag列(这是一个int)中。空格('')不是整数列的有效值。
不要因为不想在表中使用空值而受到指责。另一种选择是将其他值视为空(如0或-1),然后使用此COALESCE语句:
coalesce(status_flag, -1) as status_flag
我刚刚再次阅读你的问题。你说vwtable没有status_flag值,但是你从vwtable中选择status_flag。也许那是你的问题。你能详细说明你遇到的确切问题吗?
如果vwtable有一个status_flag列,但其中没有任何值(即它们都是null),那么根本不需要coalesce表达式。您只需插入“空白”值即可。