Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jbates99
Constraint Violating Yak Guru
396 Posts |
Posted - 2010-05-24 : 15:19:25
|
| hi all,I'm creating a report via SQLCMD and the report name needs to contain the current date so I chose to store the complete path and file name in a variable.**But when I specify the variable name in sqlcmd, the report file is not generated. **Print @ReportPath shows C:\AuditReports\RamcoFailedLogins2010524.txtwhich looks valid. The folder AuditReports does exist so I don't know why the report is not being created.Any ideas, please?Thanks, JohnDECLARE @curDate datetimeDECLARE @PriorMonthStart datetimeDECLARE @PriorMonthEnd datetimeDECLARE @sqlcmd varchar(1000);DECLARE @cmd varchar(1000);DECLARE @ReportPath varchar(200);SET NOCOUNT ON;BEGINSELECT @curDate = GETDate()-- derive last day of the prior monthSET @PriorMonthEnd = dateadd(ms,+1,DATEADD(mm, DATEDIFF(mm,0,getdate() ), 0))Print @PriorMonthEnd -- derive first day of prior monthSET @PriorMonthStart = dateadd(ms,+1,DATEADD(mm, DATEDIFF(m,0,getdate() ) -1, 0))Print @PriorMonthStart -- Construct report path and nameSet @ReportPath = 'C:\AuditReports\' + 'RamcoFailedLogins' + Convert(nvarchar(4),DATEPART(yyyy, getdate()))+ Convert(nvarchar(2),DATEPART(mm, getdate()))+ Convert(nvarchar(2),DATEPART(dd, getdate())) + '.txt'--Set @ReportPath = 'C:\AuditReports\' + 'RamcoFailedLogins' + @PriorMonthEnd + '.txt' get conversion errorprint @ReportPathSET @sqlcmd = 'sqlcmd -Q ' SET @cmd = '"SELECT CAST(ServerName as char(15)) as Server, CAST(LoginName as char(22)) as FailedLogin, CAST(ClientName as char(20)) as ComputerName, LoginDate from DBA.dbo.AuditDatabaseLogins WHERE AuditType = ''Login Failed'' AND LoginDate between ''' + convert(varchar(10), @PriorMonthStart, 112) + ''' and ''' + convert(varchar(10), @PriorMonthEnd, 112) + ''' " -o @ReportPath'SET @sqlcmd = @sqlcmd + @cmd;Print @sqlcmd;EXEC xp_cmdshell @sqlcmd,no_output;END |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
jbates99
Constraint Violating Yak Guru
396 Posts |
Posted - 2010-05-24 : 16:04:27
|
| Thanks for your reply, Tara.Print @ReportPath shows:C:\AuditReports\RamcoFailedLogins2010524.txt?? I don't understand why the month is not 2 digits 05 rather than 5sqlcmd -Q "SELECT CAST(ServerName as char(15)) as Server, CAST(LoginName as char(22)) as FailedLogin, CAST(ClientName as char(20)) as ComputerName, LoginDate from DBA.dbo.AuditDatabaseLogins WHERE AuditType = 'Login Failed' AND LoginDate between '20100401' and '20100501' " -o @ReportPathThanks, John |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2010-05-24 : 16:14:57
|
| I fixed @ReportPath as well as @sqlcmd.DECLARE @curDate datetimeDECLARE @PriorMonthStart datetimeDECLARE @PriorMonthEnd datetimeDECLARE @sqlcmd varchar(1000);DECLARE @cmd varchar(1000);DECLARE @ReportPath varchar(200);SET NOCOUNT ON;BEGINSELECT @curDate = GETDate()-- derive last day of the prior monthSET @PriorMonthEnd = dateadd(ms,+1,DATEADD(mm, DATEDIFF(mm,0,getdate() ), 0))Print @PriorMonthEnd -- derive first day of prior monthSET @PriorMonthStart = dateadd(ms,+1,DATEADD(mm, DATEDIFF(m,0,getdate() ) -1, 0))Print @PriorMonthStart -- Construct report path and nameSet @ReportPath = 'C:\AuditReports\' + 'RamcoFailedLogins' + Convert(varchar(8), GETDATE(), 112) + '.txt'--Set @ReportPath = 'C:\AuditReports\' + 'RamcoFailedLogins' + @PriorMonthEnd + '.txt' get conversion errorprint @ReportPathSET @sqlcmd = 'sqlcmd -Q ' SET @cmd = '"SELECT CAST(ServerName as char(15)) as Server, CAST(LoginName as char(22)) as FailedLogin, CAST(ClientName as char(20)) as ComputerName, LoginDate from DBA.dbo.AuditDatabaseLogins WHERE AuditType = ''Login Failed'' AND LoginDate between ''' + convert(varchar(10), @PriorMonthStart, 112) + ''' and ''' + convert(varchar(10), @PriorMonthEnd, 112) + ''' " -o ' + @ReportPathSET @sqlcmd = @sqlcmd + @cmd;Print @sqlcmd;--EXEC xp_cmdshell @sqlcmd,no_output;ENDTara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
jbates99
Constraint Violating Yak Guru
396 Posts |
Posted - 2010-05-24 : 16:39:01
|
| ah, yours is a much cleaner way of stringing the date.Set @ReportPath = 'C:\AuditReports\' + 'RamcoFailedLogins' + Convert(varchar(8), GETDATE(), 112) + '.txt'Thanks very much, TKizer! |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
binsel
Starting Member
5 Posts |
Posted - 2010-06-30 : 08:25:31
|
| Tara,I have script running against multiple SQL Servers. It creates one output file per sql instance and the instance names are in the file names. Do you know how I can add MACHINE NAMES to the output files?I tried using %COMPUTERNAME% but it gave me the same comp. name I ran the script from.set dt=noneset tm=nonefor /F "tokens=2-4 delims=/ " %%i in ('date /t') do set dt=%%i%%j%%kfor /F "tokens=5-6 delims=:. " %%i in ('echo.^| time ^| find "current" ') do set tm=%%i%%jfor /F %%a in (SQL2005servers.txt) do sqlcmd -E -S %%a -d master -i c:\SEC\SEC-SQLSr_2005_2008_201006220000.sql -Y30 -s "|" -o c:\SEC\OUT2005\SQL_Server_%COMPUTERNAME%_%%a_%dt%%tm%.outThanks,Burhan |
 |
|
|
|
|
|
|
|