使用INNER JOIN的SQL Server存储过程

时间:2013-10-23 15:44:27

标签: sql-server stored-procedures

ALTER PROCEDURE [dbo].[SP_STORED_PROCEDURE]
AS
BEGIN  
  SELECT 
     CUSTOMER_NAME,
     CUSTOMER_PRODUCT_ID
  FROM CUSTOMER
  INNER JOIN PRODUCT ON PRODUCT_ID = CUSTOMER_PRODUCT_ID
END

我从产品表中选择数据。但是,如果CUSTOMER_PRODUCT_IDnull,则无法选择(请参阅)空数据。

如何查看所有数据?

  • 如果PRODUCT_ID 等于 CUSTOMER_PRODUCT_ID

  • PRODUCT_ID 不为空 CUSTOMER_PRODUCT_ID?

任何帮助都将受到极大的赞赏。

感谢。

1 个答案:

答案 0 :(得分:2)

ALTER PROCEDURE [dbo].[SP_STORED_PROCEDURE]
AS
BEGIN  
  SELECT 
     CUSTOMER_NAME,
     CUSTOMER_PRODUCT_ID
  FROM CUSTOMER
  Left Outer JOIN PRODUCT ON PRODUCT_ID = CUSTOMER_PRODUCT_ID
END

使用Left outer join返回Customer表中包含product表中匹配行的所有数据,并在行不匹配时返回null