内联函数,每个视图或函数中的列名必须是唯一的
Inline Function, Column names in each view or function must be unique
我加入了两个 table 现在我想在函数中保存该语句以便我可以再次使用它
然后再次。我尝试按如下方式创建函数:-
create function fn_electricalsem1and2()
returns table
as
return (
Select *
from [Electrical Semester 1 Regular] as T1
inner join[Electrical Semester 2 Regular] As T2 On T1.department_id= T2.department_id
)
现在我收到错误
Column names in each view or function must be unique. Column name
'Name' in view or function 'fn_electricalsem1and2' is specified more
than once.
到现在为止我还没有做任何函数,但为什么我得到错误 function must be unique?
It is giving error because your function is returning a table and in a table all the column name should be unique.
要解决您需要更换的问题
Select *
至
Select t1.column1 as Col1, t1.column2, t2.column1 as Col2, ...and so on
在您的情况下,department_id
在两个 table 中都可用。 table 也都包含一个名称列:name
.
在以下查询中,
我们将首先创建表格并填充一些数据。
CREATE TABLE [Electrical Semester 1 Regular](department_id INT);
CREATE TABLE [Electrical Semester 2 Regular](department_id INT);
INSERT INTO [Electrical Semester 1 Regular]
VALUES(1);
INSERT INTO [Electrical Semester 2 Regular]
VALUES(1);
当我们执行以下查询时,您不会遇到任何错误;
Select *
from [Electrical Semester 1 Regular] as T1
inner join[Electrical Semester 2 Regular] As T2 On T1.department_id= T2.department_id
+---------------+---------------+
| department_id | department_id |
+---------------+---------------+
| 1 | 1 |
+---------------+---------------+
但是,由于 [Electrical Semester 1 Regular] 和 [Electrical Semester 的列名相同,因此以下查询 returns 出错2 常规]。在 SQL 服务器中,如果我们想创建一个视图或函数,我们必须使用别名
提供唯一的名称
create function fn_electricalsem1and2()
returns table
as
return (
Select *
from [Electrical Semester 1 Regular] as T1
inner join[Electrical Semester 2 Regular] As T2 On T1.department_id= T2.department_id
)
在 SQL 服务器中,如果我们想要创建一个视图或函数,我们必须提供唯一的名称。您可以使用别名启用它,这样您就可以像下面这样更改您的查询;
create function fn_electricalsem1and2()
returns table
as
return (
Select t1.department_id as t1_department_id , t2.department_id as t2_department_id
from [Electrical Semester 1 Regular] as T1
inner join[Electrical Semester 2 Regular] As T2 On T1.department_id= T2.department_id
)
GO
select * from fn_electricalsem1and2()
+------------------+------------------+
| t1_department_id | t2_department_id |
+------------------+------------------+
| 1 | 1 |
+------------------+------------------+
我加入了两个 table 现在我想在函数中保存该语句以便我可以再次使用它 然后再次。我尝试按如下方式创建函数:-
create function fn_electricalsem1and2()
returns table
as
return (
Select *
from [Electrical Semester 1 Regular] as T1
inner join[Electrical Semester 2 Regular] As T2 On T1.department_id= T2.department_id
)
现在我收到错误
Column names in each view or function must be unique. Column name 'Name' in view or function 'fn_electricalsem1and2' is specified more than once.
到现在为止我还没有做任何函数,但为什么我得到错误 function must be unique?
It is giving error because your function is returning a table and in a table all the column name should be unique.
要解决您需要更换的问题
Select *
至
Select t1.column1 as Col1, t1.column2, t2.column1 as Col2, ...and so on
在您的情况下,department_id
在两个 table 中都可用。 table 也都包含一个名称列:name
.
在以下查询中, 我们将首先创建表格并填充一些数据。
CREATE TABLE [Electrical Semester 1 Regular](department_id INT);
CREATE TABLE [Electrical Semester 2 Regular](department_id INT);
INSERT INTO [Electrical Semester 1 Regular]
VALUES(1);
INSERT INTO [Electrical Semester 2 Regular]
VALUES(1);
当我们执行以下查询时,您不会遇到任何错误;
Select *
from [Electrical Semester 1 Regular] as T1
inner join[Electrical Semester 2 Regular] As T2 On T1.department_id= T2.department_id
+---------------+---------------+
| department_id | department_id |
+---------------+---------------+
| 1 | 1 |
+---------------+---------------+
但是,由于 [Electrical Semester 1 Regular] 和 [Electrical Semester 的列名相同,因此以下查询 returns 出错2 常规]。在 SQL 服务器中,如果我们想创建一个视图或函数,我们必须使用别名
提供唯一的名称create function fn_electricalsem1and2()
returns table
as
return (
Select *
from [Electrical Semester 1 Regular] as T1
inner join[Electrical Semester 2 Regular] As T2 On T1.department_id= T2.department_id
)
在 SQL 服务器中,如果我们想要创建一个视图或函数,我们必须提供唯一的名称。您可以使用别名启用它,这样您就可以像下面这样更改您的查询;
create function fn_electricalsem1and2()
returns table
as
return (
Select t1.department_id as t1_department_id , t2.department_id as t2_department_id
from [Electrical Semester 1 Regular] as T1
inner join[Electrical Semester 2 Regular] As T2 On T1.department_id= T2.department_id
)
GO
select * from fn_electricalsem1and2()
+------------------+------------------+
| t1_department_id | t2_department_id |
+------------------+------------------+
| 1 | 1 |
+------------------+------------------+