Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在SQL Server中创建视图_Sql_Sql Server_Database_View_Sql Server 2012 - Fatal编程技术网

在SQL Server中创建视图

在SQL Server中创建视图,sql,sql-server,database,view,sql-server-2012,Sql,Sql Server,Database,View,Sql Server 2012,我有两张桌子: 合同(联系人ID、合同号、估计成本、CurrencyEstimatedCostID、合同成本、CurrencyContractCostID) CurrencyTypes(CurrencyID,CurrencyTypeName) 我想在视图中(在SQL Server视图中)用CurrencyTypeName代替外键CurrencyEstimatedCostID和CurrencyContractCostID查看合同表 我使用此查询,但它只为CurrencyTypeName返回一列

我有两张桌子:

  • 合同
    (联系人ID、合同号、估计成本、CurrencyEstimatedCostID、合同成本、CurrencyContractCostID)

  • CurrencyTypes
    (CurrencyID,CurrencyTypeName)

我想在视图中(在SQL Server视图中)用
CurrencyTypeName
代替外键
CurrencyEstimatedCostID
CurrencyContractCostID
查看合同表

我使用此查询,但它只为
CurrencyTypeName
返回一列(我希望
CurrencyEstimatedCostID
CurrencyContractCostID
各返回一列)


如何更改查询?

无法100%确定我是否理解您试图执行的操作-但是,如果您有两列同时充当
CurrencyTypes
表的外键,则需要联接两次:

SELECT 
    c.contractID, c.ContractNo, 
    EstimatedCostCurrency = ct1.CurrencyType, 
    ContractCostCurrency = ct2.CurrencyType
FROM 
    Contracts  c
INNER JOIN 
    CurrencyTypes ct1 ON c.CurrencyEstimatedCostID = ct1.CurrencyID
INNER JOIN 
    CurrencyTypes ct2 ON c.CurrencyContractCostID = ct2.CurrencyID

你不应该加入CurrencyId而不是CurrencyType吗?@TienDinh:是的,很有可能-最初的问题有这个加入条件,但那真的没有任何意义,你是对的
SELECT 
    c.contractID, c.ContractNo, 
    EstimatedCostCurrency = ct1.CurrencyType, 
    ContractCostCurrency = ct2.CurrencyType
FROM 
    Contracts  c
INNER JOIN 
    CurrencyTypes ct1 ON c.CurrencyEstimatedCostID = ct1.CurrencyID
INNER JOIN 
    CurrencyTypes ct2 ON c.CurrencyContractCostID = ct2.CurrencyID