• Couple of things:

    SET NOCOUNT ON;

    BEGIN

    This needs to be at the beginning before you declare your variables.

    Also, you're passing a string into xp_cmdshell that has "@PriorMonthStart" and "@PriorMonthEnd". You need to build the string in such a way that the values you want are passed, not the variable names:

    SET @cmd =

    '"SELECT ServerName as Server,

    LoginName as FailedLogin,

    ClientName as ComputerName,

    LoginDate

    FROM DBA.dbo.AuditDatabaseLogins

    WHERE AuditType = ''Login Failed''

    AND LoginDate between ''' + @PriorMonthStart + ''' and ''' + @PriorMonthEnd + '''

    ORDER BY LoginDate" -o C:\AuditReports\FailedLogins.txt'

    └> bt



    Forum Etiquette: How to post data/code on a forum to get the best help[/url]