将varchar值','转换为数据类型int时转换失败。
update Quotation_T set DOID= CASE
WHEN DOID IS NULL THEN CAST(2 AS NVARCHAR(20))
WHEN DOID LIKE '%'+CAST(2 AS NVARCHAR(20))+'%' THEN DOID
WHEN DOID IS NOT NULL THEN (DOID +','+CAST(2 AS NVARCHAR(20)))
END
where Quotation_T.QuotationID = 2 and Br_Code=1 and Item_Code=705
答案 0 :(得分:0)
You must convert DIOD to NVARCHAR in last when block.
update Quotation_T set DOID= CASE
WHEN DOID IS NULL THEN CAST(2 AS NVARCHAR(20))
WHEN DOID LIKE '%'+CAST(2 AS NVARCHAR(20))+'%' THEN CAST(DOID AS NVARCHAR(20))
WHEN DOID IS NOT NULL THEN (CAST(DOID AS NVARCHAR(20)) +','+CAST(2 AS NVARCHAR(20)))
END
where Quotation_T.QuotationID = 2 and Br_Code=1 and Item_Code=705
答案 1 :(得分:0)
I believe your DOID data type is int, and needs to be NVARCHAR(20) - (maybe NVARCHAR(max) would be better) to support characters. Your update works fine if the DOID is NVARCHAR(20).
Here's what I see from testing.
create table Quotation_T
(
DOID nvarchar(20)
)
insert into Quotation_T VALUES(N'hello');
insert into Quotation_T VALUES(null);
insert into Quotation_T VALUES(N'2')
update Quotation_T set DOID= CASE
WHEN DOID IS NULL THEN CAST(2 AS NVARCHAR(20))
WHEN DOID LIKE '%'+CAST(2 AS NVARCHAR(20))+'%' THEN DOID
WHEN DOID IS NOT NULL THEN (DOID +','+CAST(2 AS NVARCHAR(20)))
END
SELECT * from Quotation_T
/* results in
hello,2
2
2
*/
create table Quotation_Tint
(
DOID int
)
insert into Quotation_Tint VALUES(4);
insert into Quotation_Tint VALUES(null);
insert into Quotation_Tint VALUES(2)
update Quotation_Tint set DOID= CASE
WHEN DOID IS NULL THEN CAST(2 AS NVARCHAR(20))
WHEN DOID LIKE '%'+CAST(2 AS NVARCHAR(20))+'%' THEN DOID
WHEN DOID IS NOT NULL THEN (DOID +','+CAST(2 AS NVARCHAR(20)))
END
SELECT * from Quotation_Tint
/* results in
Conversion failed when converting the varchar value ',' to data type int.
*/