• Can anyone provide me the error free code to reorganize and rebuild indexes depending on frag %

    to create job.

    I am getting errors when i ran the provided code:

    Msg 154, Level 15, State 1, Procedure ReorgRebuildIndex, Line 76

    a USE database statement is not allowed in a procedure, function or trigger.

    Msg 156, Level 15, State 1, Procedure ReorgRebuildIndex, Line 79

    Incorrect syntax near the keyword 'ALTER'.

    Msg 102, Level 15, State 1, Procedure ReorgRebuildIndex, Line 79

    Incorrect syntax near ''.

    Msg 102, Level 15, State 1, Procedure ReorgRebuildIndex, Line 80

    Incorrect syntax near 'BASE'.

    Msg 102, Level 15, State 1, Procedure ReorgRebuildIndex, Line 83

    Incorrect syntax near ''.

    Msg 102, Level 15, State 1, Procedure ReorgRebuildIndex, Line 84

    Incorrect syntax near 'BASE'.

    Msg 319, Level 15, State 1, Procedure ReorgRebuildIndex, Line 85

    Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

    Msg 154, Level 15, State 1, Procedure ReorgRebuildIndex, Line 109

    a USE database statement is not allowed in a procedure, function or trigger.

    Msg 105, Level 15, State 1, Procedure ReorgRebuildIndex, Line 110

    Thanks