Posted on: January 2, 2020 Posted by: admin Comments: 32

Replacing cursors using joins in sql server Part 64

Text version of the video


All SQL Server Text Articles

All SQL Server Slides

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

In Part 63, we have discussed about cursors. The example, in Part 63, took around 45 seconds on my machine. Please watch Part 63, before proceeding with this video. In this video we will re-write the example, using a join.

Update tblProductSales
set UnitPrice =
When Name = ‘Product – 55’ Then 155
When Name = ‘Product – 65’ Then 165
When Name like ‘Product – 100%’ Then 10001
from tblProductSales
join tblProducts
on tblProducts.Id = tblProductSales.ProductId
Where Name = ‘Product – 55’ or Name = ‘Product – 65’ or
Name like ‘Product – 100%’

When I executed this query, on my machine it took less than a second. Where as the same thing using a cursor took 45 seconds. Just imagine the amount of impact cursors have on performance. Cursors should be used as your last option. Most of the time cursors can be very easily replaced using joins.

To check the result of the UPDATE statement, use the following query.
Select Name, UnitPrice from
tblProducts join
tblProductSales on tblProducts.Id = tblProductSales.ProductId
where (Name=’Product – 55′ or Name=’Product – 65′ or
Name like ‘Product – 100%’)


Xem thêm bài viết:


32 People reacted on this

  1. Watch a 30 mins video in 3 mins. The BEST extension in google chrome store.
    Plus,the state of the art Artificial Intelligence algorithms automatically analyzes videos to locate and pull the precise location of key points of interest like – topics, scenes, people, sentiments, brands, expressions, labels and much more. Allowing you to rapidly gain intelligent insights from any video.

  2. My learning lasts about 1month, i will end this whole course in 2 months. I know much more nowadays. Thank U Venkat. I recommended this course to all of my friends.

  3. Hi venkt, really your video is helping me a lot I'm from Liberia west Africa.
    with your video i'm becoming a big boy in technology in class in T–SQL thanks a lot.

  4. hello venkat garu your videos are superb. i have been watching this videos for past 5 months thank you for making this videos. please do more videos

  5. Hello venkat . . .your vedios are simply awsome and please share if you possible total DBA or Apps tech vedios
    . . .if you stay in hyd I wanted to take coaching with you. . . please do rply??????

  6. Thank you very much for taking time to give feedback. In the description of this video, I have included the link for ASP .NET, C#, and SQL Server playlists. All the videos are arranged in logical sequence in these playlists, which could be useful to you. Please share the link with your friends who you think would also benefit from them. If you like these videos, please click on the THUMBS UP button below the video. For email alerts, when new videos are uploaded, you may subscribe to my channel.

  7. Hi Sir,

    Your way of teaching is really very very nice.
    I have learned SQL server through your videos.
    Thanks a lot for your time
    Keep uploading.
    God bless you

  8. Hi sir..Thank You very much for these videos.. It Helped me a lot, every example was clear and provides the understanding of the concept in a great details.. I forwarded this videos to all my friends and they also find them very useful.. Please Keep Posting such videos.. Thank You Sir..:)

  9. Good Morning Sir
    your videos are superb , from these video series i learned lot
    Can you please upload video on crystal report and reporting services
    i am waiting for your reply
    thank you very much

Leave a Comment