SQL Server Programming Part 6 – WHILE Loops

25
13



If you’d like to help fund Wise Owl’s conversion of tea and biscuits into quality training videos you can click this link to make a donation. Thanks for watching!

By Andrew Gould

– In SQL Server there is only one type of loop: a WHILE loop. This video teaches you how to use them, from the basic syntax of the WHILE statement, through how to use a SELECT statement within a loop. We’ll also cover how to use the BREAK command to exit from a loop, what to do when you inevitably find yourself in an endless loop and, finally, a quick introduction to using loops with cursors.

You can download the script to create the Movies database used in this video at the following link:

You can see the range of resources and courses we offer in SQL Server here:

Visit www.wiseowl.co.uk for more online training resources in Microsoft Excel, Microsoft Access, Microsoft PowerPoint, Microsoft Word, Microsoft Project, Microsoft Publisher, Microsoft Visio, SQL Server, Reporting Services, Analysis Services, Visual Studio, ASP.NET, VB.NET, C# and more!

Nguồn: https://chaoticpharmacology.com/

Xem thêm bài viết khác: https://chaoticpharmacology.com/cong-nghe/

25 COMMENTS

  1. Thank you !
    you videos are very helpful !
    could you please tell me how to go through all rows of one column and print them using loop

  2. I needed to learn store procedures, then I was needing everything in this tutorial in this exact order… you deserve the subscribe!

  3. I know ur a brit but ur english is terrible for a global audience. Im american n i cant tell what ur tryin to say dude. Asians make better sense than u

  4. HI i have a problem in sql wherein i want to update column value between 10 and 20, there are 100 rows and just want to update next columns between 10 and 20
    can you help me how to figure it out

  5. Very good videos with clear didactic style. I am a highly experienced Oracle pl/sql programmer who wants to have the skill level to convert to a t-sql programmer since my current employability is bit limited. What experience to you have with Oracle developers becoming t-sql/SQL Server programmers in terms of required effort and success rate in finding SQL Server roles? Thx.

  6. Thanks you are the best …. Guys! other way for better solution for seeing 0 film 9 and 10 oscars line
    set @numberFilms = (select COUNT(*) from tblFilm f where f.FilmOscarWins = @count)
    if @numberFilms != 0
    begin
    print cast(@numberFilms as varchar(3)) + ' Films have ' + Cast(@count as varchar(2)) + ' Oscars'
    end

  7. Hi there,

    Enjoying and learning from the video, however, when I downloaded the updated db from the link in the info, running the code in SSMS didn't work and I had to modify the code to get it to work. Below is the code I used

    DECLARE @Counter INT
    DECLARE @MaxFilmOscarWins INT
    DECLARE @NumFilms INT

    SET @MaxFilmOscarWins = (SELECT MAX(FilmOscarWins) FROM [dbo].[tblFilm])
    SET @Counter = 0

    WHILE @Counter<=@MaxFilmOscarWins
    BEGIN
    SET @NumFilms =
    (SELECT COUNT (*) FROM tblFilm WHERE FilmOscarWins = @Counter)

    PRINT CAST(@NumFilms AS VARCHAR(3)) + ' films have won ' +
    CAST(@Counter AS VARCHAR(2)) + ' Oscars. '

    SET @Counter = @Counter + 1
    END

    Hope this helps.

    Mike

  8. hello,
    sorry to bother you, but im asking is this how we automate a stored procedure to run automaticlly, it is not working:

    use master
    go
    CREATE PROCEDURE MyBackgroundTask
    AS
    BEGIN
        — SET NOCOUNT ON added to prevent extra result sets from
        — interfering with SELECT statements.
        SET NOCOUNT ON;

        — The interval between cleanup attempts
        declare @timeToRun nvarchar(50)
        set @timeToRun= '03:33:33'

        while 1 = 1
        begin
            waitfor time @timeToRun
            begin
                execute [MyDatabaseName].[dbo].[MyDatabaseStoredProcedure];
            end
        end
    END

    ————

    sp_procoption    @ProcName= 'MyBackgroundTask',
                   @OptionName= 'startup',
                   @OptionValue= 'on'

  9. This is a good video showing how it works, but you need to start the video stating clearly that a while loop,recursive CTE's and cursors must be done as last resort and only if you have convinced the DBA that there is absolutely no other way.

  10. Yours are very very helpful thank you, but you might ignore the 'print statement' and go for sql database and show the loop in manipulating the data instead. most of the people are not looking to print messages in sql.

  11. Really awl some, I just have a question about the "if ……break" clause, according to the result, the result set will be truncated if @NumFilms=0, but the result for the films that won 11 Oscars is truncated at the same time! We just want to delete the @NumFilms=0 result.  How to solve it?? 

LEAVE A REPLY

Please enter your comment!
Please enter your name here