TSQL: IF ELSE, For Loop

Categories: Database; Tagged with: ; @ November 2nd, 2012 16:39

IF:

CREATE PROCEDURE spTestDavid
@userID int
AS
    IF EXISTS(SELECT * FROM UserDavid WHERE ID = @userID)
        PRINT 'User EXISTS: ' + CAST(@userID AS VARCHAR(32))
    ELSE
        PRINT 'User CANNOT found' + CAST(@userID AS VARCHAR(32))

 

WHILE:

CREATE PROCEDURE spTestDavid2
@COUNTER int
AS
    WHILE @COUNTER > 0
    BEGIN
        EXEC spTestDavid @userID = @COUNTER
        SET @COUNTER = @COUNTER - 1;
    END

EXEC spTestDavid2 @COUNTER = 30

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.