This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing an interesting interview question and answer for SQL Database Developer.
Most of the guys knew about the SQL Server Local Temp Table and Global Temp Table.
But same as like Temp table, you can also create Temp Stored procedure or Temp function in locally or globally.
Please don’t confuse or surprise with this kind of question because in few scenarios you should create a Temp stored procedure or function.
Few facts:
- Use single # for a local temporary SP. Once you close your session, it will drop automatically
- Use double ## for a global temporary SP
- Temporary SP behaves like normal SP
- You should create temporary SP for testing purpose
- During deployment, if you have a script that uses repetitive code, you can create a temporary SP
- You can create a global temporary SP so that you can connect/execute your test code from all the databases
- You can create temporary SP for temporary maintenance work
Create a sample global temporary SP:
1 2 3 4 5 6 7 8 9 |
CREATE PROC ##TempProc AS DECLARE @TempTable TABLE (col1 INT) INSERT INTO @TempTable(col1) VALUES (1),(2),(3) SELECT * FROM @TempTable RETURN 0 |
Execute:
1 |
EXEC ##TempProc |
Leave a Reply