使用SQL Server Management Studio 2017创建视图时出现问题

时间:2018-11-09 00:38:16

标签: sql sql-server

创建视图vw_contacttypedetails

a。 ContactTypeId,来自person.contacttype的名称

b。来自person.businessentitycontact的BusinessEntityId,PersonId

c。连接两个表,最终显示4列(如上所述,每个表中有2列)

d。然后,运行查询以查看vw_contacttypedetails中的数据

我尝试但得到了无效的对象名称。以下是我的查询:

create view vwcontacttypedetails
as
select ContactTypeID,Name,BusinessEntityID,PersonID
from tblContacttype
join tblbusinessentitycontact
on Contacttype.ContactTypeID = businessentitycontact.ContactTypeID

1 个答案:

答案 0 :(得分:0)

看来您的加入声明是错误的...

create view vwcontacttypedetails
as
select c.ContactTypeID,b.Name,b.BusinessEntityID,b.PersonID
from tblContacttype c
join tblbusinessentitycontact b
on c.ContactTypeID = b.ContactTypeID

有关联接的语法如何工作,请在此处查看:
https://www.w3schools.com/sql/sql_join.asp

以及来自Microsoft的官方文档:
https://docs.microsoft.com/en-us/sql/relational-databases/performance/joins?view=sql-server-2017

  

在FROM子句中指定连接条件有助于将其与WHERE子句中可能指定的任何其他搜索条件区分开,并且是指定连接的推荐方法。简化的ISO FROM子句连接语法为:

FROM first_table join_type second_table [ON (join_condition)]
  

join_type指定执行哪种连接:内部,外部或交叉连接。 join_condition定义要为每对连接的行评估的谓词。以下是FROM子句连接规范的示例:

FROM Purchasing.ProductVendor JOIN Purchasing.Vendor
     ON (ProductVendor.BusinessEntityID = Vendor.BusinessEntityID)
  

以下是使用此联接的简单SELECT语句:

SELECT ProductID, Purchasing.Vendor.BusinessEntityID, Name
FROM Purchasing.ProductVendor JOIN Purchasing.Vendor
    ON (Purchasing.ProductVendor.BusinessEntityID = Purchasing.Vendor.BusinessEntityID)
WHERE StandardPrice > $10
  AND Name LIKE N'F%'
GO

接下来的问题请看这里: https://stackoverflow.com/help/how-to-ask