Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL错误:转换varchar值时转换失败';PJOI015';到数据类型位_Sql_Sql Server_Tsql_Union - Fatal编程技术网

SQL错误:转换varchar值时转换失败';PJOI015';到数据类型位

SQL错误:转换varchar值时转换失败';PJOI015';到数据类型位,sql,sql-server,tsql,union,Sql,Sql Server,Tsql,Union,错误本身并不令人困惑,但原因是 我的代码 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 Ot

错误本身并不令人困惑,但原因是

我的代码

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更改为实际数字。

是ProductStockOption。是否存储了位字段?尝试将其转换为nvarchar,看看是否有帮助。

当合并时,选定列的数据类型必须匹配。由于您没有努力创建一个复制问题的最小示例,因此拒绝了此问题。发布的代码中有很多内容与您的问题无关。我不知道错误的确切原因,因此如何将其最小化?正如我在问题中所说,工会在没有最后一点的情况下工作,最后一点自行工作!是的。现在我觉得很愚蠢,谢谢你,只是需要一双新的眼睛。我在寻找一个更复杂的错误,忽略了简单的,谢谢你