join 3 views in one view

Shambhu Rai 1,406 Reputation points
2023-02-03T04:01:38.42+00:00

Hi Expert,

i wanted to join three views in one using temp table i.e.

Create view test1 as

select col1,col2 into #temp1 from view1

select col1,col2 into #temp2 from view2

select col1,col2 into #temp3 from view3

select col1,col2 from #temp1 a join #temp2 b on a.col1=b.col1

how can i do this

Azure SQL Database
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,714 questions
SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
2,799 questions
SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,453 questions
{count} votes

Accepted answer
  1. CosmogHong-MSFT 22,941 Reputation points Microsoft Vendor
    2023-02-07T01:32:18.1333333+00:00

    Hi @Shambhu Rai

    Try something like this:

    CREATE PROC SP_TEST1
    AS
    SELECT col1, col2 INTO #temp1 FROM view1;
    SELECT col1, col2 INTO #temp2 FROM view2;
    SELECT col1, col2 INTO #temp3 FROM view3;
    SELECT col1, col2 INTO #temp4
    FROM (SELECT col1,col2 FROM #temp1 a JOIN #temp2 b ON a.col1 = b.col1) c
    JOIN #temp3 d ON c.col1 = d.col1;
    GO
    EXEC SP_TEST1;
    GO
    CREATE VIEW view_test
    AS
    SELECT * FROM #temp4
    GO
    
    

8 additional answers

Sort by: Most helpful
  1. Samy Abdul 3,366 Reputation points
    2023-02-03T05:11:56.1366667+00:00
    1 person found this answer helpful.
    0 comments No comments

  2. CosmogHong-MSFT 22,941 Reputation points Microsoft Vendor
    2023-02-03T05:54:36.7666667+00:00

    Hi @Shambhu Rai

    You don't need to use temp table, just use view1/view2/view3 as dataset.

    Create view test1 as
    select a.col1,b.col2 from view1 a join view2 b on a.col1=b.col1
    

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  3. Olaf Helper 40,816 Reputation points
    2023-02-03T06:14:58.18+00:00

    how can i do this

    In that way with a #temp table you can't, no way, any kind of DML statements are forbidden in a view.

    Use a UNION ALL operator to combine the result of the 3 views, see Set Operators - UNION (Transact-SQL)

    1 person found this answer helpful.
    0 comments No comments

  4. CosmogHong-MSFT 22,941 Reputation points Microsoft Vendor
    2023-02-03T06:25:45.48+00:00

    Hi @Shambhu Rai

    A view consists of a single SELECT statement. You cannot create or drop tables in a view.

    If you need to do something like that, then try using stored procedure.

    1 person found this answer helpful.
    0 comments No comments