TSQL 2008如何使视图的列唯一

时间:2019-07-10 10:43:08

标签: tsql view

我有一个查询要保存为视图,但SSMS返回错误,指出有多个具有相同名称的列。

我试图将受影响的列重命名为别名,但是没有成功。

每个视图或函数中的列名必须唯一。视图或函数“ vwBlocks”中的列名“ codemanQuestionID”被多次指定。

create view vwBlocks as


SELECT        

LUConstructionType.codemanOptionID AS LUConstruction_codemanOptionID

, LUFascias.codemanOptionID AS LUFascias_codemanOptionID
, Block.windowsID
, LUWindows.windows
, LUWindows.codemanQuestionID AS codemanQuestion_ID

, Block.externalDoorID
, LUExternalDoor.externalDoor
, LUExternalDoor.codemanOptionID as LUExternal_codemanOptionID

FROM            Block LEFT OUTER JOIN
                         LUOwnership ON Block.ownershipID = LUOwnership.ownershipID LEFT OUTER JOIN
                         LULocalAuthority ON Block.localAuthorityID = LULocalAuthority.authorityTypeID LEFT OUTER JOIN
                         LUConstructionType ON Block.constructionTypeID = LUConstructionType.constructionTypeID LEFT OUTER JOIN
                         LUTV ON Block.TVID = LUTV.TVID LEFT OUTER JOIN
                         LUSatellite ON Block.satelliteID = LUSatellite.satelliteID LEFT OUTER JOIN
                         LUPlayArea ON Block.playArea = LUPlayArea.playAreaID LEFT OUTER JOIN
                         LURoofCovering ON Block.roofCoveringID = LURoofCovering.roofCoveringID LEFT OUTER JOIN
                         LUFascias ON Block.fasciasID = LUFascias.fasciasID LEFT OUTER JOIN
                         LUWindows ON Block.windowsID = LUWindows.windowsID LEFT OUTER JOIN
                         LUExternalDoor ON Block.externalDoorID = LUExternalDoor.externalDoorID LEFT OUTER JOIN
                         LUcontractorInfo ON Block.contractorInfoID = LUcontractorInfo.contractorID LEFT OUTER JOIN
                         LUagentInfo ON Block.agentInfoID = LUagentInfo.agentID LEFT OUTER JOIN
                         LULandlord ON Block.LandlordID = LULandlord.landlordID LEFT OUTER JOIN
                         LUblockStatus ON Block.blockStatusID = LUblockStatus.blockStatusID LEFT OUTER JOIN
                         LUPropertyGroup ON Block.propertyGroup = LUPropertyGroup.propertyGroupID LEFT OUTER JOIN
                         LUCommunalBoilerType ON Block.communalBoilerType = LUCommunalBoilerType.communalBoilerID LEFT OUTER JOIN
                         LUExternalAreaManagedBy ON Block.externalAreaManagedBy = LUExternalAreaManagedBy.managedByID LEFT OUTER JOIN
                         LUgasBoilerMakeModel ON Block.CommBoilerMakeModelID = LUgasBoilerMakeModel.makeModelId LEFT OUTER JOIN
                         LUMaintenanceResp ON Block.maintenanceRepID = LUMaintenanceResp.maintenanceRepID

有人可以推荐解决方案吗?

谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我没有两次看到codemanQuestionID。这是一些帮助我避免和/或解决此类问题的建议。

至少有四种方式为列添加别名:

  1. (表达式)AS((别名))

  2. (((alias))=(expression)

  3. 有((cte name))(((alias1),(alias2),...

  4. FROM((子查询))AS((alias1>),(alias2,...

AS是最糟糕的IMO。它草率而混乱,特别是当您不包括AS时。我通常使用的别名样式是:

SELECT
   col1   = <expression>,
   col2   = <expression>,
   colABC = <expression>
FROM  schema.table1 AS t1
JOIN  schema.table2 AS t2;

这使调试变得更加容易。