如何加入四 table 进行发票?
How to join four table for invoice?
我要开具销售发票创建了四个表:(在stimulsoft中发送变量)
如何使用程序将这些表连接在一起?
这些是表格:
这是程序:
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
但是没用!
您缺少 INNER JOIN
。
请加
INNER JOIN dbo.Software_Order ON dbo.Software_Invoice.Id = dbo.Software_Order.Invoice_Id
祝你好运。
您缺少两个 JOIN
语句:
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 时,您正在使用新的 table 加入您现在拥有的内容。所以加入两个 tables,你将使用
SELECT *
FROM [Table1]
INNER JOIN [Table2]
ON [Table1].[ForeignKeyToTable2] = [Table2].[PrimaryKey]
ON
部分可以是任何条件,不一定是外键关系。
在加入这两个 table 之后,您现在有一个新的 "table",其中包含两个 table 的所有列([Table1].*
和 [Table2].*
).然后可以进一步加入新的 JOIN
语句:
INNER JOIN [Table3]
ON [Table3].[Column] = [Table1/2].[Column]
ON
语句可以进一步包含几个条件,例如:
ON [Table3].[Column1] = [Table1].[Column1]
AND [Table3].[Column2] = [Table2].[Column1]
我要开具销售发票创建了四个表:(在stimulsoft中发送变量)
如何使用程序将这些表连接在一起?
这些是表格:
这是程序:
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
但是没用!
您缺少 INNER JOIN
。
请加
INNER JOIN dbo.Software_Order ON dbo.Software_Invoice.Id = dbo.Software_Order.Invoice_Id
祝你好运。
您缺少两个 JOIN
语句:
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 时,您正在使用新的 table 加入您现在拥有的内容。所以加入两个 tables,你将使用
SELECT *
FROM [Table1]
INNER JOIN [Table2]
ON [Table1].[ForeignKeyToTable2] = [Table2].[PrimaryKey]
ON
部分可以是任何条件,不一定是外键关系。
在加入这两个 table 之后,您现在有一个新的 "table",其中包含两个 table 的所有列([Table1].*
和 [Table2].*
).然后可以进一步加入新的 JOIN
语句:
INNER JOIN [Table3]
ON [Table3].[Column] = [Table1/2].[Column]
ON
语句可以进一步包含几个条件,例如:
ON [Table3].[Column1] = [Table1].[Column1]
AND [Table3].[Column2] = [Table2].[Column1]