2015-05-19 36 views
0

我要发出一张销售发票创建了四张表格:(在stimulsoft中发送变量)如何加入四张发票表格?

我该如何使用该过程将这些表格连接在一起?

这些都是表:

enter image description here

这是程序:

USE [RahgoshafanDB] 
 
GO 
 
/****** Object: StoredProcedure [dbo].[Invoice_Soft] Script Date: 05/19/2015 10:30:26 ******/ 
 
SET ANSI_NULLS ON 
 
GO 
 
SET QUOTED_IDENTIFIER ON 
 
GO 
 
CREATE Procedure [dbo].[Invoice_Soft] 
 
@Invoice_Id int 
 

 
AS 
 
BEGIN 
 
select 
 
     dbo.Customer.Customer as customer, 
 
     dbo.Customer.Tel as tel, 
 
     dbo.Customer.Adr as adr, 
 
     dbo.Software_Invoice.Dat as dat, 
 
     dbo.Software_Invoice.Payable as payable, 
 
     dbo.Software_Invoice.Discount as discount, 
 
     dbo.Software.Software as software, 
 
     dbo.Software_Order.Price as price, 
 
     dbo.Software_Order.Quantity as quantity, 
 
     dbo.Software_Order.Sum as sum 
 
     From dbo.Software_Invoice inner join dbo.Customer 
 
     on dbo.Software_Invoice.Customer_Id = dbo.Customer.Id AND  
 
     dbo.Software_Invoice.Id = dbo.Software_Order.Invoice_Id 
 
     where dbo.Software_Invoice.Id = @Invoice_Id 
 
END

,但它不工作!

回答

2

你是缺席两场JOIN -statements:

FROM dbo.Software_Invoice 
    INNER JOIN dbo.Customer 
     ON dbo.Software_Invoice.Customer_Id = dbo.Customer.Id 
    INNER JOIN dbo.Software_Order  
     ON dbo.Software_Invoice.Id = dbo.Software_Order.Invoice_Id 
    INNER JOIN dbo.Software 
     ON dbo.Software_Order.Software_Id = dbo.Software.Id 

这也许应该修复它。

说明

当使用SQL JOIN,要加入你现在有一个新的表有什么。因此,连接两个表,你会用

SELECT * 
FROM [Table1] 
INNER JOIN [Table2] 
    ON [Table1].[ForeignKeyToTable2] = [Table2].[PrimaryKey] 

ON部分可以是任何条件,也不一定外键关系。

将这两个表格加入后,您现在会有一个由两个表格的所有列组成的新“表格”([Table1].*[Table2].*)。这可以进一步用新的JOIN语句中加入:

INNER JOIN [Table3] 
    ON [Table3].[Column] = [Table1/2].[Column] 

ON语句可以进一步由多个条件语句,例如,:

ON [Table3].[Column1] = [Table1].[Column1] 
    AND [Table3].[Column2] = [Table2].[Column1] 
+0

问题解决了 非常感谢Njal! –

1

您错过了INNER JOIN
请添加

INNER JOIN dbo.Software_Order ON dbo.Software_Invoice.Id = dbo.Software_Order.Invoice_Id 

好运。

+0

非常感谢... :)强尼... –

+0

而@Njål是正确的,如果你想在你的选择中包含'Software.Software',你还有一个'INNER JOIN'来执行。 –