2016-04-14 7 views
1

Der Fehler selbst ist nicht verwirrend, aber der Grund warum ist.SQL-Fehler: Konvertierung fehlgeschlagen beim Konvertieren des Varchar-Wertes 'PJOI015' in den Datentyp Bit

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 

Der Fehler

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

Die Verwirrung

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 

Wenn ich die letzte Vereinigung (oben) der Code funktioniert gut entfernen. Und ich kann die letzte Verbindung selbständig ausführen, aber die @ PID zu der tatsächlichen Nummer ändern.

+2

Wenn UNION, müssen die Datentypen der ausgewählten Spalten übereinstimmen. – jarlh

+0

Diese Frage wurde abgelehnt, weil Sie sich nicht die Mühe gemacht haben, ein minimales Beispiel zu erstellen, das das Problem reproduziert. Es gibt eine Menge Dinge im geposteten Code, die für Ihr Problem nicht relevant sind. – wvdz

+0

Ich wusste nicht, was genau den Fehler verursacht hat, also wie könnte ich es minimieren? – Richard

Antwort

3

Ist ProductStockOption.Stocked ein Bitfeld? Versuchen Sie es in nvarchar umzuwandeln und sehen Sie, ob das hilft.

+0

Ja. Jetzt fühle ich mich dumm, danke nur ein frisches Paar Augen benötigt. – Richard

+0

Ich war auf der Suche nach einem komplexeren Fehler und überblickte das einfache, Danke – Richard

Verwandte Themen