SQL Cursors - how and when to use them

Поделиться
HTML-код
  • Опубликовано: 16 сен 2024
  • In this video, I will show you how to create a cursor in SQL and explain when it should be used using an example.

Комментарии • 50

  • @AlejandroBelloRD
    @AlejandroBelloRD 2 года назад +12

    So detailed and calmly-paced, absolute newbies at the end of this video should end up knowing 95% of all cursor practical uses. Period. Subscribed!

  • @ohiovstheworld
    @ohiovstheworld 3 года назад +14

    In an SQL c lass and took a 0 on the Cursors assignment because I just couldn't wrap my head around it, and my instructor was no help. I wish I'd have watched this video, thanks for the perfect explanation!

    • @JamesOliver
      @JamesOliver  3 года назад +2

      You're very welcome. :) Appreciate the kind words.

    • @aholmes1397
      @aholmes1397 3 года назад +3

      Learn english before SQL.

    • @saltyfish7626
      @saltyfish7626 2 года назад +5

      @@aholmes1397 Why should he?

  • @SandeepYadav-j8e4c
    @SandeepYadav-j8e4c Год назад +1

    So fortunate to find exact scenario that fits my need. Thanx James.

  • @The_abdelhafid
    @The_abdelhafid 2 года назад +2

    I watched multiple videos about Cursors , finally I found the one that explains cursors in a simple way

  • @chammu6936
    @chammu6936 3 года назад +2

    This is the clear explanation of why we have to use cursor. Thanks alot !

  • @drecksackblase3530
    @drecksackblase3530 11 месяцев назад

    this is the first SQL tutorial i found on youtube which is not from an indian guy, thank you

  • @muhajirikhsanushabri2928
    @muhajirikhsanushabri2928 2 года назад +7

    We can just use summary of group by right? I still dont get the value for the cursor

  • @jamesheyne3727
    @jamesheyne3727 3 года назад +7

    Fantastic video, if you keep making videos like this you will help a lot of people and find serious success.

  • @user-ow5hb4bu9z
    @user-ow5hb4bu9z Год назад

    That’s awesome James . Definitely to use cursor . Thank you for sharing your knowledge. This is typical use case for cursor

  • @paulfunigga
    @paulfunigga 2 года назад +12

    why not group the second table by account_number and calculate the sum of payments for each account number, first, then join this result of that with the balance table and subtract aggregated results?

  • @shubhamsahu54
    @shubhamsahu54 3 года назад +2

    Nice & Smooth explanation.
    well done.

  • @m4ng4n
    @m4ng4n 2 года назад

    won yourself a suscriber just because of this video, huge props man

  • @DraconaiMac
    @DraconaiMac 7 месяцев назад

    VERY nice......... clear and concise. Thank you!

  • @danaewardrup3922
    @danaewardrup3922 Год назад

    Thank you for explaining so clearly! Great walk through

    • @Shankar-zi3yk
      @Shankar-zi3yk 7 месяцев назад

      hello mam have u noticed he is calling the table with different columnnames how could it happen

  • @diogopinheiro6530
    @diogopinheiro6530 Месяц назад

    You are the MAN!!!

  • @satori8626
    @satori8626 Год назад +1

    You said that the payments data can be aggregated, but that is missing the point. But I think it is a great point, since after watching this video, I still don't know a case scenario where a cursor would be the best option. I would only use cursors as a last resort, since they perform poorly and complicates debugging.

  • @chaoticrealm777
    @chaoticrealm777 2 года назад

    You have tastefully placed fast-forward segments when you type your code. Take my sub!!!

  • @saudhj3449
    @saudhj3449 2 года назад

    You made my life easy .. thank you so much

  • @alexingino5738
    @alexingino5738 3 года назад +1

    You've earned yourself a sub my friend

  • @manoman2008
    @manoman2008 Год назад

    ***** 5Star to you. awesome comparison, finally found the real reason after so many years.

  • @bartoszcwiakala8916
    @bartoszcwiakala8916 5 месяцев назад

    Love it, thanks!

  • @brotherx6205
    @brotherx6205 2 года назад

    excellent video and explanation

  • @riccarrasquilla379
    @riccarrasquilla379 Месяц назад

    thanks for the video

  • @nicolasjimenezq369
    @nicolasjimenezq369 2 года назад

    That was beautiful!

  • @blazinghellwheels
    @blazinghellwheels 2 года назад +9

    If anyone at my work used a cursor instead of just using SUM to perform this update I would fail their code review immediately and have a teaching session with them
    Cursors can dynamically update if the source is updated mid update and can global depending on configuration unless you specify LOCAL FAST_FORWARD.
    People also tend to use them to call stored procedures per row in triggers and more frequently then not, it has lead to more time fixing issues than time saved by that sole developer in having to think about a more adequate solution.
    I've received multiple late night calls to fix performance issues where jobs stalled as a result of devs doing this kind of stuff
    The only time I think I've actually seen a decent argument for cursors was for operating on DMV objects such as forcing index and more importantly statistics rebuilds and I'd bet you can still just do that with a while loop at the loss of ignoring changes during the loop firing (forcing it to be processed on the next call to that job)
    I've literally wrote a temporally stable audit table code generator that generates
    1. Audit table for a table
    2. Audit Foreign Keys connecting Audit tables by AuditIDs in the same manner as the original table
    3. Triggers for INSERT, UPDATE, and DELETE operations
    And I STILL never needed to use a CURSOR.
    If you're familiar with the idea of "bad parts" of a language such as "with" or "this" in JS it's the exact same concept.
    You can write better code by constraining yourself to not use the bad parts of a language and Cursors are a horrible part of the SQL language

  • @aishwaryadevan7402
    @aishwaryadevan7402 2 года назад

    Nicely done! Thanks

  • @matthewwallen5305
    @matthewwallen5305 6 месяцев назад

    Well produced video, but it’s the equivalent to very professionally explaining how to set yourself on fire

  • @megnasudeep3372
    @megnasudeep3372 3 года назад

    Very well explained.

  • @HauLe-sv6sy
    @HauLe-sv6sy 2 года назад

    Thank you so much, it is very helpful.

  • @tsunamio7750
    @tsunamio7750 7 месяцев назад

    I just love the WHERE-statement in the UPDATE; that will be applied n-times for each entry. I hope MS SQL offers some cashing natively, because that looks like a killer app.

  • @priyankabalaji5990
    @priyankabalaji5990 3 года назад

    Amazing explanation

  • @siddharthb2226
    @siddharthb2226 2 года назад +1

    va thalaiva va thalaiva va thalaiva

  • @trr12
    @trr12 3 года назад +1

    nice video, but you didnt explain why you were writing each line, for instance why do you have to deallocate the cursor? or, why do you declare the cursor and then write "FOR" and then a query? is that FOR, I suppose referring to the whole query that you write ahead of it? Thanks

  • @rajeevjha3714
    @rajeevjha3714 2 года назад

    gr8 job

  • @cabaasmaxamed9300
    @cabaasmaxamed9300 2 года назад

    you can solve it trigger that problem because when it makes insert subtract each other using trigger

  • @Shankar-zi3yk
    @Shankar-zi3yk 7 месяцев назад

    Hey in check u used columname but it in resukt set we get only after_payment how could it happen

  • @MarkoVasev
    @MarkoVasev 3 года назад

    What keyboard are you using?

  • @ukaszkiepas57
    @ukaszkiepas57 2 года назад

    love ya

  • @makubexgospel504
    @makubexgospel504 3 года назад

    Gotta ask, how did you get it to run even with errors?

    • @tdzinkdroid
      @tdzinkdroid 2 года назад

      They are not actually errors. His IntelliSense appears to have not been updated.

  • @eRRor4hack
    @eRRor4hack 7 месяцев назад

    Script to try yourself:
    create table Account_Balance(
    Account_Number int,
    Balance int);
    insert into Account_Balance values(1,100),
    (2,150),
    (3,200),
    (4,250),
    (5,1000),
    (6,5000)
    select * from Account_Balance
    create table Account_Payments(
    Date date,
    Account_Number int,
    Payment int);
    insert into Account_Payments values('2021-02-06',1,50),
    ('2021-02-06',2,25),
    ('2021-02-06',2,125),
    ('2021-02-06',3,100),
    ('2021-02-06',4,50),
    ('2021-02-06',5,500),
    ('2021-02-06',6,250),
    ('2021-02-06',6,250)
    select * from Account_Payments

  • @frankyfantasticmedia9718
    @frankyfantasticmedia9718 Год назад

    is this similar for MySQL? I tried to look for MySQL but I can't understand anything from all those videos as their English is Horrible!

  • @Isaiah094
    @Isaiah094 Год назад

    This is what happens when you don’t have foreign keys like TransactionID & TransactionType lol

  • @TheApgreyd
    @TheApgreyd Год назад

    Okay? Okay? Okay?

  • @irvingceron1016
    @irvingceron1016 Год назад

    Great video. Lose that cheesy corporate intro, it's so gosh darn cringe. Corporatey stuff like that is soulless