-- ============================================= -- Author: -- Create date: <22.06.2010> -- Description: -- ============================================= CREATE PROCEDURE [spConsignor] -- Input parameters: -- @OrderID = Search key for order ord invoice, CustID = Search key for customers, @Param1 is not in use yet @Parma1 int, @OrderID int, @CustID int AS DECLARE @OrderBool AS bit DECLARE @InvoiceBool AS bit BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; --Checks if its a order number or a invoice number SET @OrderBool = (SELECT COUNT(Ordrenr) FROM ORDRE WHERE Ordrenr = @OrderID) IF @OrderBool <> 1 BEGIN SET @InvoiceBool = 1 END -- If the order number exist, this SELECT statement will be executed IF @OrderBool = 1 BEGIN SELECT -- Customer NULL AS CustomerNum_Customer, NULL AS Name1_Customer, NULL AS Name2_Customer, NULL AS StreetAddress1_Customer, NULL AS StreetAddress2_Customer, NULL AS StreetAddress3_Customer, NULL AS Postcode_Customer, NULL AS City_Customer, NULL AS Mobile_Customer, NULL AS Phone_Customer, NULL AS Email_Customer, NULL AS Fax_Customer, NULL AS PostOfficeBox_Customer, NULL AS PostOfficeBoxCity_Customer, NULL AS PostOfficeBoxPostcode_Customer, NULL AS CountryName_Customer, NULL AS CustomerOrigin_Customer, NULL AS Attention_Customer, NULL AS Contact_Customer, NULL AS ReceiverRef_Customer, NULL AS OurRef_Customer, NULL AS MessageToCarrier_Customer, NULL AS MessageToDriver_Customer, NULL AS MessageToReceiver_Customer, NULL AS PurchaseNo_Customer, NULL AS ShipmentTypeNo_Customer, -- Order CustomerOrder / CustomerOrderCopy Kundenr AS CustomerNum_Order, Ordrenr AS OrderNumber_Order, Kundenavn AS Name1_Order, NULL AS Name2_Order, Adresse AS StreetAddress1_Order, Adresse2 AS StreetAddress2_Order, NULL AS StreetAddress3_Order, Postnr AS Postcode_Order, Poststed AS City_Order, NULL AS Mobile_Order, NULL AS Phone_Order, NULL AS Email_Order, NULL AS Fax_Order, NULL AS PostOfficeBox_Order, NULL AS PostOfficeBoxCity_Order, NULL AS PostOfficeBoxPostcode_Order, o.Land AS CountryName_Order, NULL AS CustomerOrigin_Order, NULL AS Attention_Order, NULL AS Contact_Order, o.Deres_Ref AS ReceiverRef_Order, o.Vaar_Ref AS OurRef_Order, NULL AS MessageToCarrier_Order, NULL AS MessageToDriver_Order, NULL AS MessageToReceiver_Order, NULL AS PurchaseNo_Order, NULL AS ShipmentTypeNo_Order, -- Delivery NULL AS CustomerNum_Delivery, NULL AS Name1_Delivery, NULL AS Name2_Delivery, NULL AS StreetAddress1_Delivery, NULL AS StreetAddress2_Delivery, o.Levering AS StreetAddress3_Delivery, NULL AS Postcode_Delivery, NULL AS City_Delivery, NULL AS Mobile_Delivery, NULL AS Phone_Delivery, NULL AS Email_Delivery, NULL AS Fax_Delivery, NULL AS PostOfficeBox_Delivery, NULL AS PostOfficeBoxCity_Delivery, NULL AS PostOfficeBoxPostcode_Delivery, o.Landkode AS CountryName_Delivery, NULL AS CustomerOrigin_Delivery, NULL AS Attention_Delivery, NULL AS Contact_Delivery, NULL AS ReceiverRef_Delivery, NULL AS OurRef_Delivery, NULL AS MessageToCarrier_Delivery, NULL AS MessageToDriver_Delivery, NULL AS MessageToReceiver_Delivery, NULL AS PurchaseNo_Delivery, NULL AS ShipmentTypeNo_Delivery, -- Invoice NULL AS CustomerNum_Invoice, Kundenavn AS Name1_Invoice, NULL AS Name2_Invoice, o.Adresse AS StreetAddress1_Invoice, o.Adresse2 AS StreetAddress2_Invoice, NULL AS StreetAddress3_Invoice, o.Postnr AS Postcode_Invoice, o.Poststed AS City_Invoice, NULL AS Mobile_Invoice, NULL AS Phone_Invoice, NULL AS Email_Invoice, NULL AS Fax_Invoice, NULL AS PostOfficeBox_Invoice, NULL AS PostOfficeBoxCity_Invoice, NULL AS PostOfficeBoxPostcode_Invoice, NULL AS CountryName_Invoice, NULL AS CustomerOrigin_Invoice, NULL AS Attention_Invoice, NULL AS Contact_Invoice, NULL AS ReceiverRef_Invoice, NULL AS OurRef_Invoice, NULL AS MessageToCarrier_Invoice, NULL AS MessageToDriver_Invoice, NULL AS MessageToReceiver_Invoice, NULL AS PurchaseNo_Invoice, NULL AS ShipmentTypeNo_Invoice, -- Contact NULL AS ContactNum_Contact, NULL AS Name1_Contact, NULL AS Name2_Contact, NULL AS StreetAddress1_Contact, NULL AS StreetAddress2_Contact, NULL AS StreetAddress3_Contact, NULL AS Postcode_Contact, NULL AS City_Contact, mobil.Phone AS Mobile_Contact, telefon.Phone AS Phone_Contact, e.mailadr AS Email_Contact, fax.Phone AS Fax_Contact, NULL AS PostOfficeBox_Contact, NULL AS PostOfficeBoxCity_Contact, NULL AS PostOfficeBoxPostcode_Contact, NULL AS CountryName_Contact, NULL AS CustomerOrigin_Contact, NULL AS Attention_Contact, NULL AS Contact_Contact, NULL AS CustomerNum_Contact, -- Package 1 AS PackagesCount, NULL AS PackagesMarking, NULL AS PackagesContents, NULL AS PackagesWeight, NULL AS PackagesHeight, NULL AS PackagesLength, NULL AS PackagesWidth , NULL AS PackagesVolume, NULL AS CODAccount, o.Totalsum AS CODAmount, NULL AS CODKID, NULL AS CODReference, NULL AS InsuranceAmount, NULL AS InsuranceCategory, NULL AS InsurancePolicyNo, NULL AS DeliveryTerms, NULL AS Department, NULL AS InvoiceNumber, o.Betbetingelse AS PaymentTerms, NULL AS PaymentType, NULL AS ProjectName, NULL AS ProjectNumber, o.Transportmaate AS CarrierCode, NULL AS SearchKeyOrder1, NULL AS SearchKeyOrder2, NULL AS SearchKeyInvoiceNum, NULL AS SearchKeyCustomerNum, NULL AS SearchKeyContactNum, NULL AS Link_Table1, NULL AS Link_Table2, NULL AS Link_Table3, NULL AS Link_Table4, NULL AS Link_Table5, NULL AS Link_Table6, NULL AS Link_Table7, NULL AS Link_Table8, NULL AS Link_Table9, NULL AS Link_Table10 FROM ORDRE AS o INNER JOIN DEBITOR AS d ON o.Kundenr = d.Kontonr INNER JOIN c_Name AS n ON d.NameID = n.ID LEFT OUTER JOIN c_email AS e ON e.namelink = n.ID AND ISNULL(e.standard, 0) <> 0 LEFT OUTER JOIN c_Phone AS telefon ON telefon.NameLink = n.ID AND ISNULL(telefon.standard, 0) <> 0 AND telefon.phonetype = 150101 LEFT OUTER JOIN c_Phone AS mobil ON mobil.NameLink = n.ID AND ISNULL(mobil.standard, 0) <> 0 AND mobil.phonetype = 150102 LEFT OUTER JOIN c_Phone AS fax ON fax.NameLink = n.ID AND ISNULL(fax.standard, 0) <> 0 AND fax.phonetype = 150103 LEFT OUTER JOIN c_Contacts AS contact ON contact.FK_ID = d.Kontonr AND contact.FK_Type = 0 AND ISNULL(e.standard, 0) <> 0 WHERE o.Ordrenr = @OrderID END --If the order exist in the FAKTURA table, this SELECT statement will be executed ELSE IF @InvoiceBool = 1 BEGIN SELECT -- Customer NULL AS CustomerNum_Customer, NULL AS Name1_Customer, NULL AS Name2_Customer, NULL AS StreetAddress1_Customer, NULL AS StreetAddress2_Customer, NULL AS StreetAddress3_Customer, NULL AS Postcode_Customer, NULL AS City_Customer, NULL AS Mobile_Customer, NULL AS Phone_Customer, NULL AS Email_Customer, NULL AS Fax_Customer, NULL AS PostOfficeBox_Customer, NULL AS PostOfficeBoxCity_Customer, NULL AS PostOfficeBoxPostcode_Customer, NULL AS CountryName_Customer, NULL AS CustomerOrigin_Customer, NULL AS Attention_Customer, NULL AS Contact_Customer, NULL AS ReceiverRef_Customer, NULL AS OurRef_Customer, NULL AS MessageToCarrier_Customer, NULL AS MessageToDriver_Customer, NULL AS MessageToReceiver_Customer, NULL AS PurchaseNo_Customer, NULL AS ShipmentTypeNo_Customer, -- Order CustomerOrder / CustomerOrderCopy o.Kundenr AS CustomerNum_Order, o.Fakturanr AS OrderNumber_Order, o.Kundenavn AS Name1_Order, NULL AS Name2_Order, o.Adresse AS StreetAddress1_Order, o.Adresse2 AS StreetAddress2_Order, NULL AS StreetAddress3_Order, o.Postnr AS Postcode_Order, o.Poststed AS City_Order, NULL AS Mobile_Order, NULL AS Phone_Order, NULL AS Email_Order, NULL AS Fax_Order, NULL AS PostOfficeBox_Order, NULL AS PostOfficeBoxCity_Order, NULL AS PostOfficeBoxPostcode_Order, o.Land AS CountryName_Order, NULL AS CustomerOrigin_Order, NULL AS Attention_Order, NULL AS Contact_Order, o.Deres_Ref AS ReceiverRef_Order, o.Vaar_Ref AS OurRef_Order, NULL AS MessageToCarrier_Order, NULL AS MessageToDriver_Order, NULL AS MessageToReceiver_Order, NULL AS PurchaseNo_Order, NULL AS ShipmentTypeNo_Order, -- Delivery NULL AS CustomerNum_Delivery, NULL AS Name1_Delivery, NULL AS Name2_Delivery, NULL AS StreetAddress1_Delivery, NULL AS StreetAddress2_Delivery, o.Levering AS StreetAddress3_Delivery, NULL AS Postcode_Delivery, NULL AS City_Delivery, NULL AS Mobile_Delivery, NULL AS Phone_Delivery, NULL AS Email_Delivery, NULL AS Fax_Delivery, NULL AS PostOfficeBox_Delivery, NULL AS PostOfficeBoxCity_Delivery, NULL AS PostOfficeBoxPostcode_Delivery, o.Landkode AS CountryName_Delivery, NULL AS CustomerOrigin_Delivery, NULL AS Attention_Delivery, NULL AS Contact_Delivery, NULL AS ReceiverRef_Delivery, NULL AS OurRef_Delivery, NULL AS MessageToCarrier_Delivery, NULL AS MessageToDriver_Delivery, NULL AS MessageToReceiver_Delivery, NULL AS PurchaseNo_Delivery, NULL AS ShipmentTypeNo_Delivery, -- Invoice NULL AS CustomerNum_Invoice, o.Kundenavn AS Name1_Invoice, NULL AS Name2_Invoice, o.Adresse AS StreetAddress1_Invoice, o.Adresse2 AS StreetAddress2_Invoice, NULL AS StreetAddress3_Invoice, o.Postnr AS Postcode_Invoice, o.Poststed AS City_Invoice, NULL AS Mobile_Invoice, NULL AS Phone_Invoice, NULL AS Email_Invoice, NULL AS Fax_Invoice, NULL AS PostOfficeBox_Invoice, NULL AS PostOfficeBoxCity_Invoice, NULL AS PostOfficeBoxPostcode_Invoice, NULL AS CountryName_Invoice, NULL AS CustomerOrigin_Invoice, NULL AS Attention_Invoice, NULL AS Contact_Invoice, NULL AS ReceiverRef_Invoice, NULL AS OurRef_Invoice, NULL AS MessageToCarrier_Invoice, NULL AS MessageToDriver_Invoice, NULL AS MessageToReceiver_Invoice, NULL AS PurchaseNo_Invoice, NULL AS ShipmentTypeNo_Invoice, -- Contact NULL AS ContactNum_Contact, NULL AS Name1_Contact, NULL AS Name2_Contact, NULL AS StreetAddress1_Contact, NULL AS StreetAddress2_Contact, NULL AS StreetAddress3_Contact, NULL AS Postcode_Contact, NULL AS City_Contact, mobil.Phone AS Mobile_Contact, telefon.Phone AS Phone_Contact, e.mailadr AS Email_Contact, fax.Phone AS Fax_Contact, NULL AS PostOfficeBox_Contact, NULL AS PostOfficeBoxCity_Contact, NULL AS PostOfficeBoxPostcode_Contact, NULL AS CountryName_Contact, NULL AS CustomerOrigin_Contact, NULL AS Attention_Contact, NULL AS Contact_Contact, NULL AS CustomerNum_Contact, -- Package 1 AS PackagesCount, NULL AS PackagesMarking, NULL AS PackagesContents, NULL AS PackagesWeight, NULL AS PackagesHeight, NULL AS PackagesLength, NULL AS PackagesWidth , NULL AS PackagesVolume, NULL AS CODAccount, o.Totalsum AS CODAmount, NULL AS CODKID, NULL AS CODReference, NULL AS InsuranceAmount, NULL AS InsuranceCategory, NULL AS InsurancePolicyNo, NULL AS DeliveryTerms, NULL AS Department, NULL AS InvoiceNumber, o.Betbetingelse AS PaymentTerms, NULL AS PaymentType, NULL AS ProjectName, NULL AS ProjectNumber, o.Transportmaate AS CarrierCode, NULL AS SearchKeyOrder1, NULL AS SearchKeyOrder2, NULL AS SearchKeyInvoiceNum, NULL AS SearchKeyCustomerNum, NULL AS SearchKeyContactNum, NULL AS Link_Table1, NULL AS Link_Table2, NULL AS Link_Table3, NULL AS Link_Table4, NULL AS Link_Table5, NULL AS Link_Table6, NULL AS Link_Table7, NULL AS Link_Table8, NULL AS Link_Table9, NULL AS Link_Table10 FROM FAKTURA AS o INNER JOIN DEBITOR AS d ON o.Kundenr = d.Kontonr INNER JOIN c_Name AS n ON d.NameID = n.ID LEFT OUTER JOIN c_email AS e ON e.namelink = n.ID AND ISNULL(e.standard, 0) <> 0 LEFT OUTER JOIN c_Phone AS telefon ON telefon.NameLink = n.ID AND ISNULL(telefon.standard, 0) <> 0 AND telefon.phonetype = 150101 LEFT OUTER JOIN c_Phone AS mobil ON mobil.NameLink = n.ID AND ISNULL(mobil.standard, 0) <> 0 AND mobil.phonetype = 150102 LEFT OUTER JOIN c_Phone AS fax ON fax.NameLink = n.ID AND ISNULL(fax.standard, 0) <> 0 AND fax.phonetype = 150103 LEFT OUTER JOIN c_Contacts AS contact ON contact.FK_ID = d.Kontonr AND contact.FK_Type = 0 AND ISNULL(e.standard, 0) <> 0 WHERE o.Fakturanr = @OrderID END END GO