multiple select in view

Shambhu Rai 1,406 Reputation points
2023-01-30T17:02:04.5166667+00:00

Hi Expert,

i wanted to store the data in temp table and then use in another to improve performance

i.e.

select col1,col2,col3 into #temp from table1

select col1,col2 ,table1.col3from view a join #temp on a. col1=b.col1

expected output: select col1,col2,col3 from view

getting error single select statement is allowed and How to delete the drop #temp table after view created

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,675 questions
SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,452 questions
SQL Server Analysis Services
SQL Server Analysis Services
A Microsoft online analytical data engine used in decision support and business analytics, providing the analytical data for business reports and client applications such as Power BI, Excel, Reporting Services reports, and other data visualization tools.
1,243 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Naomi 7,361 Reputation points
    2023-01-30T17:40:53.87+00:00

    This is not a valid statement, you cannot use temp tables in SQL view, you can only use common table expressions as part of the view. Otherwise you may want to create a stored procedure for your purpose.

    2 people found this answer helpful.

  2. Shambhu Rai 1,406 Reputation points
    2023-01-30T17:56:53.1+00:00

    and how to tune this query using view to get early results as table 1 has huge data due to whcih wants to load in temp table

    2 people found this answer helpful.
    0 comments No comments

  3. PercyTang-MSFT 12,426 Reputation points Microsoft Vendor
    2023-01-31T03:04:13.7566667+00:00

    Hi @Shambhu Rai

    the issue is there is one view in this query and another is table... how to update the view

    At your request, I did a test.

    select col1,col2 ,table1.col3 from view as a join #temp as b on a.col1=b.col1
    

    This query has a result output. But the question is how to update this data into the view.

    I wondered if I could transfer the data from this query to a new temp table again, and then update the view with the new table data. Like this.

    select col1,col2,table1.col3 into #test from view as a join #temp as b on a.col1 = b.col1;
    alter view view as select * from #test;
    

    However, an error was reported. ‘Views or functions are not allowed on temporary tables. Table names that begin with '#' denote temporary tables’.

    So I wondered if I could replace the temporary table with a new table. But you mentioned above that temporary tables had to be used due to the large amount of data. It seems to be stuck in an endless loop.

    Best regards,

    Percy Tang

    1 person found this answer helpful.