insert into temp table in databricks

Shambhu Rai 1,411 Reputation points
2023-09-08T18:08:38.8233333+00:00

Hi Expert,

how we can use the as CT in databricks sql queries

as CT on       --------??

How we can use ifobject temp table in databricks sql queries

 IF OBJECT_ID(N'tempdb..##RETHINK_temp') IS NOT NULL      ------??  

how to use insert into temp table after select statement in databricks sql queries

Please refer below sql query

CREATE PROCEDURE [dbo].[THINKNEW] @CurrentVersion BIGINT, @LastVersion BIGINT                  

AS                  

BEGIN                  

  IF OBJECT_ID(N'tempdb..##RETHINK_temp') IS NOT NULL      ------??                

BEGIN                      

DROP TABLE #RETHINK_temp                

END      

select                        

RETHINK.NO,     

RETHINK.START_DATE,     

RETHINK.SourceSystemName    

INTO #RETHINK_temp       ----insert into temo table            

From RETHINK as RETHINK                

RIGHT OUTER JOIN CHANGETABLE (CHANGES RETHINK,                   

@LastVersion)                

as CT on       --------??

RETHINK.NO=CT.NO     

AND RETHINK.START_DATE=CT.START_DATE    

AND RETHINK.SourceSystemName=CT.SourceSystemName    

where CT.SYS_CHANGE_VERSION <=                   

@CurrentVersion                   

and RETHINK.Isdeleted='Y'                  

DELETE RETHINK                  

FROM RETHINK RETHINK                  

INNER JOIN #RETHINK_temp RETHINK_temp                  

  ON      

RETHINK.NO=RETHINK_temp.NO     

AND RETHINK.START_DATE=RETHINK_temp.START_DATE    

AND RETHINK.SourceSystemName=RETHINK_temp.SourceSystemName       

and RETHINK.Isdeleted='Y'                  

END

GO


query:

1)the changetable is not any table then what is mentioned below

RIGHT OUTER JOIN CHANGETABLE (CHANGES RETHINK,                   @LastVersion)   -----------unable to understand this   


what does this below query means and two dots are saying

 IF OBJECT_ID(N'tempdb..##RETHINK_temp') IS NOT NULL  
Azure Data Lake Storage
Azure Data Lake Storage
An Azure service that provides an enterprise-wide hyper-scale repository for big data analytic workloads and is integrated with Azure Blob Storage.
1,559 questions
Azure Data Explorer
Azure Data Explorer
An Azure data analytics service for real-time analysis on large volumes of data streaming from sources including applications, websites, and internet of things devices.
576 questions
Azure Databricks
Azure Databricks
An Apache Spark-based analytics platform optimized for Azure.
2,514 questions
{count} votes

1 answer

Sort by: Most helpful
  1. AnnuKumari-MSFT 34,556 Reputation points Microsoft Employee Moderator
    2023-09-11T18:04:20.3133333+00:00

    Hi Shambhu Rai ,

    Thankyou for posting your query here.

    I understand that your query is related to using SQL queries in Databricks to insert data into temporary tables.

    We can't insert data into the temporary table but we can mimic the insert with union all (or) union(to remove duplicates).

    Example:

    #create temp view
    spark.sql("""create or replace temporary view temp_view_t as select 1 as no, 'aaa' as str""")
    
    spark.sql("select * from temp_view_t").show()
    #+---+---+
    #| no|str|
    #+---+---+
    #|  1|aaa|
    #+---+---+
    
    #union all with the new data
    spark.sql("""create or replace temporary view temp_view_t as select * from temp_view_t union all select 2 as no, 'bbb' as str""")
    
    spark.sql("select * from temp_view_t").show()                                                                     
    #+---+---+
    #| no|str|
    #+---+---+
    #|  1|aaa|
    #|  2|bbb|
    #+---+---+
    
    #to eliminate duplicates we can use union also. 
    spark.sql("""create or replace temporary view temp_view_t as select * from temp_view_t union select 1 as no, 'aaa' as str""")
    
    spark.sql("select * from temp_view_t").show()
    #+---+---+
    #| no|str|
    #+---+---+
    #|  1|aaa|
    #|  2|bbb|
    #+---+---+
    
    
    

    Hope it helps. Kindly accept the answer by clicking on Accept answer button. Thankyou


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.