SQL错误:将varchar值'PJOI015'转换为数据类型位时转换失败(SQL Error: Conversion failed when converting the var

编程入门 行业动态 更新时间:2024-10-22 21:27:18
SQL错误:将varchar值'PJOI015'转换为数据类型位时转换失败(SQL Error: Conversion failed when converting the varchar value 'PJOI015' to data type bit)

错误它的自我并不混淆,但原因是。

我的代码

DECLARE @Pcode as varchar(20) DECLARE @PID as int set @Pcode = 'PJOI015' set @PID = (select productid from product where product.ProductCode = @Pcode and Deleted = 0) select 'ProductCode' as Data, ProductCode as Value,'' as Other From Product where productid = @PID Union All select 'Descripton' as Data, Description as Value,'' as Other From Product where productid = @PID Union All select 'Long Descripton' as Data, FullDescription as Value,'' as Other From Product where productid = @PID Union All select 'Alternative Keywords' as Data, AlternativeKeywords as Value,'' as Other From Product where productid = @PID Union All select 'Manufactures Code' as Data, ManufacturerCode as Value,'' as Other From Product where productid = @PID Union All select 'Main Barcode' as Data, BarCode as Value,'' as Other From Product where productid = @PID Union All select 'Barcodes' as Data, Barcode as Value, (case when BarcodeType = 0 then 'Default' when BarcodeType = 1 then 'Inner' when BarcodeType = 2 then 'Outer' when BarcodeType = 3 then 'Pallet' when BarcodeType = 4 then 'Other' end) as Other From ProductBarcode where productid = @PID Union all select 'Default Supplier' as Data, Supplier.Name as Value,Supplier.SupplierCode as Other From Product left join Supplier on Supplier.SupplierID = Product.SupplierID where productid = @PID Union all SELECT 'Other Suppliers' as Data,Supplier.Name as Value, 'StanBuy ' + Cast(StandardBuy as varchar(10)) as Other FROM ProductSupplierPrice left join Supplier on Supplier.SupplierID = ProductSupplierPrice.SupplierID WHERE ProductID = @PID GROUP BY ProductSupplierPrice.SupplierID, ProductID, StandardBuy,Supplier.Name Union all select 'Stocked?' as 'Data', ProductStockOption.Stocked as 'Value',branch.Name as 'Other' from ProductStockOption left join Branch on Branch.BranchID = ProductStockOption.BranchID where ProductID = @PID

错误

Msg 245, Level 16, State 1, Line 8 Conversion failed when converting the varchar value 'PJOI015' to data type bit.

困惑

Union all select 'Stocked?' as 'Data', ProductStockOption.Stocked as 'Value',branch.Name as 'Other' from ProductStockOption left join Branch on Branch.BranchID = ProductStockOption.BranchID where ProductID = @PID

如果我删除最后一个联合(上面)代码工作正常。 我可以自己运行最后一个联合,但将@PID更改为实际数字。

The Error its self is not confusing but the reason why is.

My Code

DECLARE @Pcode as varchar(20) DECLARE @PID as int set @Pcode = 'PJOI015' set @PID = (select productid from product where product.ProductCode = @Pcode and Deleted = 0) select 'ProductCode' as Data, ProductCode as Value,'' as Other From Product where productid = @PID Union All select 'Descripton' as Data, Description as Value,'' as Other From Product where productid = @PID Union All select 'Long Descripton' as Data, FullDescription as Value,'' as Other From Product where productid = @PID Union All select 'Alternative Keywords' as Data, AlternativeKeywords as Value,'' as Other From Product where productid = @PID Union All select 'Manufactures Code' as Data, ManufacturerCode as Value,'' as Other From Product where productid = @PID Union All select 'Main Barcode' as Data, BarCode as Value,'' as Other From Product where productid = @PID Union All select 'Barcodes' as Data, Barcode as Value, (case when BarcodeType = 0 then 'Default' when BarcodeType = 1 then 'Inner' when BarcodeType = 2 then 'Outer' when BarcodeType = 3 then 'Pallet' when BarcodeType = 4 then 'Other' end) as Other From ProductBarcode where productid = @PID Union all select 'Default Supplier' as Data, Supplier.Name as Value,Supplier.SupplierCode as Other From Product left join Supplier on Supplier.SupplierID = Product.SupplierID where productid = @PID Union all SELECT 'Other Suppliers' as Data,Supplier.Name as Value, 'StanBuy ' + Cast(StandardBuy as varchar(10)) as Other FROM ProductSupplierPrice left join Supplier on Supplier.SupplierID = ProductSupplierPrice.SupplierID WHERE ProductID = @PID GROUP BY ProductSupplierPrice.SupplierID, ProductID, StandardBuy,Supplier.Name Union all select 'Stocked?' as 'Data', ProductStockOption.Stocked as 'Value',branch.Name as 'Other' from ProductStockOption left join Branch on Branch.BranchID = ProductStockOption.BranchID where ProductID = @PID

The Error

Msg 245, Level 16, State 1, Line 8 Conversion failed when converting the varchar value 'PJOI015' to data type bit.

The Confusion

Union all select 'Stocked?' as 'Data', ProductStockOption.Stocked as 'Value',branch.Name as 'Other' from ProductStockOption left join Branch on Branch.BranchID = ProductStockOption.BranchID where ProductID = @PID

If I remove the last union (Above) the code works fine. And I can run the last union on its own but changing the @PID to the actual number.

最满意答案

ProductStockOption.Stocked有点领域吗? 尝试将其转换为nvarchar并查看是否有帮助。

Is ProductStockOption.Stocked a bit field? Try converting it to nvarchar and see if that helps.

更多推荐

@PID,select,Union,Value,Data,电脑培训,计算机培训,IT培训"/> <meta name="

本文发布于:2023-08-04 10:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415166.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   数据类型   错误   varchar   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!