Combining two views in SQL Server


Let's talk about views in SQL Server. Views are nothing but virtual tables which covers the table in SQL Server.
Am creating two tables,

DROP TABLE VENKAT_TABLE
GO
CREATE TABLE VENKAT_TABLE(ID INT, NAME VARCHAR(100))
GO
INSERT INTO VENKAT_TABLE VALUES(1,'SUBA')
GO
INSERT INTO VENKAT_TABLE VALUES(1,'ARUN')
GO
DROP TABLE VENKAT_SECOND_TABLE
GO
CREATE TABLE VENKAT_SECOND_TABLE(ID INT, AGE INT)
GO
INSERT INTO VENKAT_SECOND_TABLE VALUES(1,20)
GO
INSERT INTO VENKAT_SECOND_TABLE VALUES(1,30)
GO

-- Now am creating the views for the tables.
-- Views are nothing but virtual table which is like a mask on the table. For security reasons, instead of allowing the user to access the tables directly we will use views.

CREATE VIEW VENKAT_VIEW
AS
SELECT * FROM VENKAT_TABLE
GO
CREATE VIEW VENKAT1_VIEW
AS
SELECT * FROM VENKAT_TABLE
GO
CREATE VIEW VENKAT_SECOND_VIEW
AS
SELECT * FROM VENKAT_SECOND_TABLE
GO

Joining two views
------------------------------------------------------------------------------------
SELECT V1. * FROM VENKAT_VIEW V1 INNER JOIN VENKAT_SECOND_VIEW V2
ON V1.ID=V2.ID

Merging the results sets of the view.
(For merging the result set, we can use union all or union operator)
------------------------------------------------------------------------------------
SELECT * FROM VENKAT1_VIEW UNION ALL
SELECT * FROM VENKAT_VIEW
Cheers,
Venkatesan Prabu .J
Head - http://www.kaashivinfotech.com/
http://venkattechnicalblog.blogspot.com/
Ebook Download
View all
Learn
View all