Text version of the video
http://csharp-video-tutorials.blogspo...
Slides
http://csharp-video-tutorials.blogspo...
All SQL Server Text Articles
http://csharp-video-tutorials.blogspo...
All SQL Server Slides
http://csharp-video-tutorials.blogspo...
All SQL Server Tutorial Videos
https://www.youtube.com/playlist?list...
All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenka...
All Dot Net and SQL Server Tutorials in Arabic
https://www.youtube.com/c/KudvenkatAr...
In this video we will discuss the implications of creating temp tables in dynamic sql
Temp tables created by dynamic SQL are not accessible from the calling procedure. They are dropped when the dynamic SQL block in the stored procedure completes execution.
Let us understand this with an example. Notice in the example below, all the following 3 operations are in the block of dynamic sql code.
1. Creating the Temp Table
2. Populating the Temp Table
3. Select query on the Temp Table
Create procedure spTempTableInDynamicSQL
as
Begin
Declare @sql nvarchar(max)
Set @sql = 'Create Table #Test(Id int) insert into #Test values (101) Select * from #Test'
Execute sp_executesql @sql
End
So when we execute the above procedure we are able to access data from the Temp Table.
Execute spTempTableInDynamicSQL
Now, let's move the SELECT statement outside of the dynamic sql code block as shown below and ALTER the stored procedure.
Alter procedure spTempTableInDynamicSQL
as
Begin
Declare @sql nvarchar(max)
Set @sql = 'Create Table #Test(Id int) insert into #Test values (101)'
Execute sp_executesql @sql
Select * from #Test
End
At this point, execute the stored procedure. Notice, we get the error - Invalid object name ' #Test'. This is because temp tables created by dynamic SQL are not accessible from the calling procedure. They are dropped when the dynamic SQL block in the stored procedure completes execution.
Execute spTempTableInDynamicSQL
On the other hand, dynamic SQL block can access temp tables created by the calling stored procedure. Let's prove this by modifying the stored procedure as shown below.
Alter procedure spTempTableInDynamicSQL
as
Begin
Create Table #Test(Id int)
insert into #Test values (101)
Declare @sql nvarchar(max)
Set @sql = 'Select * from #Test'
Execute sp_executesql @sql
End
At this point, execute the stored procedure. Notice that we are able to access the temp table, which proves that dynamic SQL block can access temp tables created by the calling stored procedure.
Execute spTempTableInDynamicSQL
Summary
1. Temp tables created by dynamic SQL are not accessible from the calling procedure.
2. They are dropped when the dynamic SQL block in the stored procedure completes execution.
3. On the other hand, dynamic SQL block can access temp tables created by the calling stored procedure
http://csharp-video-tutorials.blogspo...
Slides
http://csharp-video-tutorials.blogspo...
All SQL Server Text Articles
http://csharp-video-tutorials.blogspo...
All SQL Server Slides
http://csharp-video-tutorials.blogspo...
All SQL Server Tutorial Videos
https://www.youtube.com/playlist?list...
All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenka...
All Dot Net and SQL Server Tutorials in Arabic
https://www.youtube.com/c/KudvenkatAr...
In this video we will discuss the implications of creating temp tables in dynamic sql
Temp tables created by dynamic SQL are not accessible from the calling procedure. They are dropped when the dynamic SQL block in the stored procedure completes execution.
Let us understand this with an example. Notice in the example below, all the following 3 operations are in the block of dynamic sql code.
1. Creating the Temp Table
2. Populating the Temp Table
3. Select query on the Temp Table
Create procedure spTempTableInDynamicSQL
as
Begin
Declare @sql nvarchar(max)
Set @sql = 'Create Table #Test(Id int) insert into #Test values (101) Select * from #Test'
Execute sp_executesql @sql
End
So when we execute the above procedure we are able to access data from the Temp Table.
Execute spTempTableInDynamicSQL
Now, let's move the SELECT statement outside of the dynamic sql code block as shown below and ALTER the stored procedure.
Alter procedure spTempTableInDynamicSQL
as
Begin
Declare @sql nvarchar(max)
Set @sql = 'Create Table #Test(Id int) insert into #Test values (101)'
Execute sp_executesql @sql
Select * from #Test
End
At this point, execute the stored procedure. Notice, we get the error - Invalid object name ' #Test'. This is because temp tables created by dynamic SQL are not accessible from the calling procedure. They are dropped when the dynamic SQL block in the stored procedure completes execution.
Execute spTempTableInDynamicSQL
On the other hand, dynamic SQL block can access temp tables created by the calling stored procedure. Let's prove this by modifying the stored procedure as shown below.
Alter procedure spTempTableInDynamicSQL
as
Begin
Create Table #Test(Id int)
insert into #Test values (101)
Declare @sql nvarchar(max)
Set @sql = 'Select * from #Test'
Execute sp_executesql @sql
End
At this point, execute the stored procedure. Notice that we are able to access the temp table, which proves that dynamic SQL block can access temp tables created by the calling stored procedure.
Execute spTempTableInDynamicSQL
Summary
1. Temp tables created by dynamic SQL are not accessible from the calling procedure.
2. They are dropped when the dynamic SQL block in the stored procedure completes execution.
3. On the other hand, dynamic SQL block can access temp tables created by the calling stored procedure
Temp tables in dynamic sql asp.net core docker | |
176 Likes | 176 Dislikes |
33,533 views views | 524K followers |
Science & Technology | Upload TimePublished on 9 May 2017 |
Không có nhận xét nào:
Đăng nhận xét