DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE
 
 
SITE SEARCH SQLUSA.com VIDEOS
 
 
SQL E/BOOKS   WORLD, USA & SQL NEWS   FORMAT
SCRIPTS SQL 2005 SQL 2008 ARTICLES
How to architect math drill sproc for addition?

Execute the following Microsoft SQL Server Transact-SQL (T-SQL) script in Management Studio (SSMS) Query Editor, SQLCMD or other client software to create and apply an addition drills stored procedure:

USE AdventureWorks

GO

 

-- SQL Server stored procedure

-- T-SQL while

-- SQL random function

CREATE PROC sprocMathTrainerAddition(

           @DrillNumber INT,

           @Level       INT,

           @Result      BIT)

AS

  BEGIN

    DECLARE  @Counter SMALLINT

    

    DECLARE  @Drill  TABLE(

                           SEQUENCE INT,

                           [Left]   INT,

                           [Right]  INT,

                           Result   VARCHAR(15)

                           )

    

    SET @Counter = 1

    

    WHILE @Counter <= @DrillNumber

      BEGIN

        INSERT @Drill

        SELECT @Counter,

               @Level * RAND(),

               @Level * RAND(),

               ''

        

        SET @Counter = @Counter + 1

      END

    

    SELECT convert(VARCHAR,SEQUENCE) + '. ' + convert(VARCHAR,[Left]) +

            ' + ' + convert(VARCHAR,[Right]) + ' = '

    FROM   @Drill

    

    IF @Result = 1

      BEGIN

        UPDATE @Drill

        SET    Result = convert(VARCHAR,([Left] + [Right]))

        

        SELECT convert(VARCHAR,SEQUENCE) + '. ' + convert(VARCHAR,[Left]) +

            ' + ' + convert(VARCHAR,[Right]) + ' = ' + Result

        FROM   @Drill

      END

  END

 

GO

 

EXEC sprocMathTrainerAddition

  60 ,

  30 ,

  1

 

GO

 

/* Partial results

 

1. 18 + 18 =

2. 1 + 19 =

3. 20 + 0 =

4. 14 + 11 =

5. 15 + 11 =

6. 29 + 17 =

7. 3 + 26 =

8. 12 + 18 =

*/

Exam Prep 70-461
SQL 2016 DESIGN & PROGRAMMING
 
 
 
DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE