每个视图或函数中的列名必须是唯一的

Column names in each view or function must be unique

在创建视图时出现错误,每个视图或函数中的列名必须是唯一的,但是在构建 select 查询时我只得到一个 record.i 必须使用 col1 和 col2 tables..如果数据在 A table 中不存在,它将从 B table.how 中获取,我可以提前 this.Thanks..

Create View ViewName AS
select 
A.col1 as col1,
A.col2  as col2,
null as col1,
B.col2 as col2
from table A,table B where A.col3=B.col3

从您的评论来看,您似乎在寻找这个:

Create View ViewName AS
select 
   A.col1,
   COALESCE(A.col2, B.col2) AS col2
from table A
  left join table B 
  on  A.col3=B.col3;

使用 LEFT OUTER JOIN 处理连接失败的情况。

实际错误的解释:

错误说明了一切 - 您的视图有两列名为 col1 和两列 col2。更改其中一组的列名称。与视图不同,即席 select 查询不需要唯一名称(或任何列名,就此而言)。

根据您的评论,您可能需要这样的东西:

CREATE VIEW ViewName AS
SELECT ISNULL(A.col1, B.col1) as col1, -- This will return B.Col1 if A.Col1 is null
       ISNULL(A.col2, B.col2) as col2,
FROM table A INNER JOIN table B ON(A.col3 = B.col3)

编辑

根据您对此答案的评论,您可以这样做:

ALTER VIEW temp AS 

SELECT COALESCE(A.col1, D.col1) as col1,
COALESCE(A.col2, B.col2, C.col2, D.col2) as col2 
FROM table A 
INNER JOIN table1 B ON (A.col3=B.col3)
INNER JOIN table3 C ON (A.col3=C.col3)
INNER JOIN table4 D ON (A.col3=D.col3) 

注意:您写了 COALESCE(A.col1,null,null,D.col1),这等同于 COALESCE(A.col1, D.col1),因为 coalesce 函数将 return 它接收到的第一个不为空的参数。

像这样我们可以吗?

alter view temp as select COALESCE(A.col1,null,null,D.col1) as col1, COALESCE(A.col2,B.col2,C.col2,D.col2) as col2 from table A INNER JOIN table1 B INNER JOIN table3 C INNER JOIN table4 ON (A.col3=B.col3 and A.col3=C.col3 and A.col3=D.col3)