asked on
---
USE msdb; --Because all jobs will hanl
GO
EXEC sp_add_jobstep
@job_name = N'Weekly Sales Data Backup',
@step_name = N'Set database to read only',
@subsystem = N'TSQL',
@command = N'ALTER DATABASE SALES SET READ_ONLY',
@retry_attempts = 5,
@retry_interval = 5 ;
--
then it will output a script,How it does this? Do you have an example?
ASKER
ASKER
sqlcmd -S ServerName\instanceName -E -i C:\ScriptName.sql
ASKER
ASKER
ASKER
so this means do not use CMDEXEC type ?I didn't say that. I said that you can add more parameters to the command.
how about one thing if I can't generate error message, which is I check if B.sql exists first, then execute b.sql, then delete b.sql.You can check if the job step run successfully or not and perform a task accordling.
if b.sql doesn't exist already means executing A.sql has problem, right ?Sorry, didn't understand your question.
ASKER
sorry, please give me an example.Man, already did:https://www.experts-exchange.com/questions/28972616/execute-a-MS-SQL-script-as-a-schedule-SQL-job.html?anchorAnswerId=41819512#a41819512
so each time I run that job I expect A.sql generate a new b.sql, so do you understand what I mean now ?Yes but you're not using properly the job step properties. Can you post a screenshot of what you have now?
ASKER
ASKER
so still relies on result.txt ?If you used that file name as the output parameter (-o), then yes.
ASKER
ASKER
so no email anyway,Where the email requirement comes from?
so I have to relies on the job steps failure alert other than that, right?Ofc. We are talking about a SQL job, right?
ASKER
I might not in front of my PC all the time, email is the only thing let me know what is the status.Then use the job feature to notify in case of failure.
I want to integrate the script inside the job, this is the way to automate sth, right?Right. In SQL Server automation usually means SQL job ;)
ASKER
ASKER
ASKER
anyway to delete a .sql file on local hdd using SQL job ?Yes. Create a new job step with the Operating System type (as in https://www.experts-exchange.com/questions/28972616/execute-a-MS-SQL-script-as-a-schedule-SQL-job.html?anchorAnswerId=41819343#a41819343) and then type the delete command.
overwrite the existing C:\ScriptName.sql if it does exists ?-i is the input script so it won't write the file but read from it.
ASKER
ASKER
ASKER
I found this:Where did you find it. In one of my above comments? :)
just add the path in front of Result.txt?Yes. C:\Windows\System32 is the path where sqlcmd is stored so it is using it as default location.
ASKER
ASKER
ASKER
ASKER
Msg 50000, Level 1, State 1Looks like instead of PRINT they used RAISEERROR() function.
ASKER
ASKER
ASKER
ASKER
ASKER
sqlcmd -b <Script>
IF ERRORLEVEL 1 GOTO Errorx;
Errorx: HandleError Here
Completex : SELECT 'Script Completed'
ASKER
sqlcmd -b <Script>
IF ERRORLEVEL 1 GOTO Errorx;
Errorx: PRINT 'Error'
Completex : PRINT 'Script Completed'
ASKER
noonol, I mean where can I see the error, man... application log ?I'm not sure why are you went to this path when I already mentioned about the output.txt file that you can provide in the output parameter. And that was more than a month ago!
ASKER
ASKER
sqlcmd -S ServerName\instanceName -E -i C:\ScriptName.sql
ASKER
basically at this moment I am still using this:This is not complete if you compare it with the solution you choose:
ASKER
you mean the whole command is not completed ?Well, why you choose my comment as solution if isn't what you're actually using? Isn't the output that you wanted to have?
ASKER
Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.
TRUSTED BY