Can i have example of declare table and temp table in sql ? and how the update operation can be used on it with real life scenario ?
Syntax of creating temp table in SQL server is as below
CREATE TABLE #table_name ( column1 datatype [ NULL | NOT NULL ], column2 datatype [ NULL | NOT NULL ], ... );
In the above table # specifies that it is temp table
LOCAL TEMPORARY TABLES are prefixed with the # character (ie: #employees).
This #employees table is stored in tempdb and SQL Server will automatically delete this table when the SQL Server session no longer requires it
create table #TempEvent ( EventID int, EventTitle Varchar(50), EventStartDate DateTime, EventEndDate DatetIme )
For Updating Temp Table, suppose you have two tables Table1 and table2, where table 2 is temp one, then syntax for updating would be
UPDATE t1 SET t1.sales=t2.sales FROM table1 t1 INNER JOIN table2 t2 ON t1.MONTH=t2.MONTH
Take a look at these examples
Also, please read about CTE in SQL, using WITH in SQL
Hope this helps.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly