In the following query,
we will create the tables at first and populate them some data.
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);
When we execute the following query you will not experience any error ;
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 |
+---------------+---------------+
However, the following query returns an error because of the same column names of the [Electrical Semester 1 Regular] and [Electrical Semester 2 Regular]. In the SQL Server if we want to create a view or function we have to give unique names with alias
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
)
In the SQL Server if we want to create a view or function we have to give unique names. You enable it with an alias so you can change your query like the below;
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 |
+------------------+------------------+