I wrote the following script but it shows an error due to the + sign in #command argument. Could anyone suggest how I can get rid of this error?
USE msdb;
GO
--add a job
EXEC dbo.sp_add_job
#job_name = N'FullBackup';
GO
USE msdb;
GO
--add jobsteps to jobsteps
DECLARE #fileName VARCHAR(90);
DECLARE #db_name VARCHAR(20);
DECLARE #fileDate VARCHAR(20);
DECLARE #commandtxt VARCHAR(100);
SET #fileName = 'D:\SQL server\BackUp\';
--SET #db_name = 'AdventureWorks_';
SET #fileDate = CONVERT(VARCHAR(8), GETDATE(),112);
SET #fileName = #fileName + #db_name + RTRIM(#fileDate) + '.bak';
SET #commandtxt = N'''BACKUP DATABASE [AdventureWorks2012] TO DISK =N''' + #fileName + ''' WITH INIT';
EXEC sp_add_jobstep
#job_name = N'FullBackup',
#step_name = N'Weekly Full Backup',
#subsystem = N'TSQL',
#command = 'BACKUP DATABASE [AdventureWorks2012] TO DISK =N''' + #fileName + ''' WITH INIT',
#retry_attempts = 5,
#retry_interval = 5;
GO
--Create a Schedule for this job, backup, occurs once a week each friday at 11:59
EXEC sp_add_schedule
#schedule_name = N'WeeklyBackup1',
#freq_type = 8,
#freq_interval = 32,
#freq_recurrence_factor = 1,
#active_start_time = 235900;
GO
--attach the schedule to the job
EXEC sp_attach_schedule
#job_name = N'FullBackup',
#schedule_name = N'WeeklyBackup1';
GO
EXEC dbo.sp_add_jobserver
#job_name = N'FullBackup';
GO
Try this
SET #commandtxt = N'BACKUP DATABASE [AdventureWorks2012] TO DISK =N''' + #fileName + ''' WITH INIT'
EXEC sp_add_jobstep
#job_name = N'FullBackup',
#step_name = N'Weekly Full Backup',
#subsystem = N'TSQL',
#command = #Commandtxt,
#retry_attempts = 5,
#retry_interval = 5;
GO
.
.
.
Related
I have created a stored procedure to execute an ssis package in the catalog and set the package parameters dynamically at run time. I then create a job step to use the Operating system (CMDEXEC) Command to run the dtexec utility. The code is below:
ALTER PROCEDURE [Administration].[prc_Exec_LoadbenefitStatement_SSIS_TEST1]
#Destination varchar(250),
#FinancialFile varchar(250),
#StaticFile varchar(250),
#StatementType varchar(15),
#ExternalID varchar(20),
#SourceSystemID varchar(20)
AS
DECLARE #jid as uniqueidentifier
BEGIN TRANSACTION
SET NOCOUNT ON;
DECLARE #statement varchar(2000)
BEGIN
SET #statement = 'dtexec /ISServer "\SSISBD\AFFS\KhanyaStatements\Khanya_LoadBenefitStatement.dtsx" /SERVER "XXXXX\SQL01"'
SET #statement = #Statement + ' /Par "Destination";' + #Destination
SET #statement = #Statement + ' /Par "FinancialFile";' + #FinancialFile
SET #statement = #Statement + ' /Par "StaticFile";' + #StaticFile
SET #statement = #Statement + ' /Par "StatementType";' + #StatementType
SET #statement = #Statement + ' /Par "ExternalID";' + #ExternalID
SET #statement = #Statement + ' /Par "SourceSystemID";' + #SourceSystemID
END
DECLARE #jname varchar(128)
SET #jname = cast(newid() as char(36))
EXEC msdb.dbo.sp_add_job
#job_name = #jname,
#enabled = 1,
#category_name = 'KhanyaStatements_SSIS',
#delete_level = 3,
#job_id = #jid OUTPUT
EXEC msdb.dbo.sp_add_jobserver
#job_id = #jid,
#server_name = 'XXXXX\SQL01'
EXEC msdb.dbo.sp_add_jobstep
#job_id = #jid,
#step_name = 'Execute SSIS',
#subsystem = 'CMDEXEC',
#proxy_name = 'pr_KhanyaStatements',
#command = #statement
DECLARE #returncode tinyint
EXEC #returncode = msdb.dbo.sp_start_job
#job_id = #jid
SELECT #returncode
SET NOCOUNT OFF;
COMMIT TRANSACTION
I have created the Credential, Proxy and Job Category. When I execute the proc the return code is 0 for success but I can clearly see that the package did not execute. Please assist?
I received one requirement from client,if any take the database backup from SQL Server, backup will by default contains the password.
so No One can restore the database without password.
I suggests the TDE encryption solution but he is not agree with this.
anyone one know, Is there any SQL setting for this?
Thanks
Nirav
IDK if there is any built-in option (except TDE). We are using WinRAR for similar purpose :
EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE
GO
DECLARE #fileName varchar(1000)
DECLARE #pathName varchar(1000)
DECLARE #backupName varchar(1000)
DECLARE #backupFullName varchar(1000)
DECLARE #rarName varchar(1000)
DECLARE #compressCommand varchar(1000)
DECLARE #deleteBakCommand varchar(1000)
DECLARE #deleteRarCommand varchar(1000)
DECLARE #moveCommand varchar(1000)
DECLARE #deleteOldRarCommand varchar(1000)
SET #fileName = 'database' + CONVERT(varchar, DATEPART(weekday,getdate()))
set #pathName = 'D:\folder\'
SET #backupName = #fileName + '.bak'
SET #rarName = #fileName + '.rar'
SET #backupFullName =#pathName + #backupName
SET #compressCommand = 'C:\Progra~1\WinRAR\rar a -m5 -pPASSWORD '+ #pathName + #rarName + ' ' + #pathName+#backupName + '"'
SET #deleteBakCommand = 'del ' + #pathName + #backupName
SET #deleteRarCommand = 'del ' + #pathName + #rarName
SET #deleteOldRarCommand = 'del ' + #pathName + 'TARGET\db\' + #rarName
set #moveCommand = 'copy /y ' + #pathName+#rarName + ' ' + #pathName + 'TARGET\db\' + #rarName
EXEC master..xp_cmdshell #deleteBakCommand;
EXEC master..xp_cmdshell #deleteRarCommand;
BACKUP DATABASE DBNAME TO DISK = #backupFullName;
EXEC master..xp_cmdshell #deleteOldRarCommand;
EXEC master..xp_cmdshell #compressCommand;
EXEC master..xp_cmdshell #deleteBakCommand;
EXEC master..xp_cmdshell #moveCommand;
EXEC master..xp_cmdshell #deleteRarCommand;
GO
EXEC sp_configure 'xp_cmdshell', 0
There is something going wrong, but can't find the mistake...
I want to restore a database in a procedure, but can't do this with variables...
DECLARE #sql as NVARCHAR(1000)
DECLARE #targetDBname as NVARCHAR(20) = 'BO_9999'
DECLARE #backupFileNamePath as NVARCHAR(100) = '\\SRV015\C$\temp\BO_1767_Schulung_2016-07-13_22-36-28.bak'
DECLARE #masterDBname as NVARCHAR(20) = 'BO_1767_Schulung'
DECLARE #targetMDFfilePath as NVARCHAR(100) = 'Y:\CADTOOLS\DB\BO_9999.mdf'
DECLARE #masterLogname as NVARCHAR(30)= 'BO_1767_Schulung_log'
DECLARE #targetLogFilePath as NVARCHAR(100) = 'Y:\CADTOOLS\LOG\BO_9999_log.LDF'
SET #sql = '''RESTORE DATABASE ' + #targetDBname
SET #sql = #sql + ' FROM DISK = ''''' + #backupFileNamePath + ''''''
SET #sql = #sql + ' WITH FILE = 1'
SET #sql = #sql + ', MOVE ''''' + #masterDBname + ''''''
SET #sql = #sql + ' TO ''''' + #targetMDFfilePath + ''''''
SET #sql = #sql + ', MOVE ''''' + #masterLogname + ''''''
SET #sql = #sql + ' TO ''''' + #targetLogFilePath + ''''''
SET #sql = #sql + ', NOUNLOAD, REPLACE, STATS = 10'''
PRINT #sql
EXEC (#sql)
--THIS COMMAND RUNS BUT I CANT GET THE COMMAND IN AN EXEC WITH VARIABLES
--RESTORE DATABASE BO_9999 FROM DISK = '\\SRV015\C$\temp\BO_1767_Schulung_2016-07-13_22-36-28.bak' WITH FILE = 1, MOVE 'BO_1767_Schulung' TO 'Y:\CADTOOLS\DB\BO_9999.mdf', MOVE 'BO_1767_Schulung_log' TO 'Y:\CADTOOLS\LOG\BO_9999_log.LDF', NOUNLOAD, REPLACE, STATS = 10
Can anybody help me... was still looking lot's of pages in the net... but couldn't get this run?!
THANKS #all
it runns this way:
RESTORE DATABASE BO_9999 FROM DISK = '\SRV015\C$\temp\BO_1767_Schulung_2016-07-13_22-36-28.bak' WITH FILE = 1, MOVE 'BO_1767_Schulung' TO 'Y:\CADTOOLS\DB\BO_9999.mdf', MOVE 'BO_1767_Schulung_log' TO 'Y:\CADTOOLS\LOG\BO_9999_log.LDF', NOUNLOAD, REPLACE, STATS = 10
and this way:
RESTORE DATABASE #targetDBname FROM DISK = #backupFileNamePath WITH FILE = 1, MOVE #masterDBname TO #targetMDFfilePath, MOVE #masterLogname TO #targetLogFilePath, NOUNLOAD, REPLACE, STATS = 10
but in the end i need it like this to run it on another linked server
EXEC (#sql) at [server\instance]
???
How do I schedule a job using sp_add_job to run every 5 minutes indefinitely?
What are the exact values of #freq_type=4,#freq_interval=64, freq_subday_type=0x4,#freq_subday_interval=10, #freq_relative_interval=0 that I should be using ?
Try this.... #user2748147
Declare #job nvarchar(128),
#mycommand nvarchar(max),
#servername nvarchar(28),
#databasename nvarchar(50),
#startdate nvarchar(8),
#starttime nvarchar(8)
SET #job = 'myJob'
SET #mycommand = 'Select * from myTable'
SET #startdate = '20151219' -- The date December 19, 2015
SET #starttime = '160000' -- The time, 16:00:00
SET #servername = 'myservername'
SET #databasename = 'yourdatabasename'
--Add a job
EXEC dbo.sp_add_job
#job_name = #job ;
--Add a job step named process step. This step runs the stored procedure
EXEC sp_add_jobstep
#job_name = #job,
#step_name = N'process step',
#subsystem = N'TSQL',
#database_name = #databasename,
#command = #mycommand
--Schedule the job at a specified date and time
exec sp_add_jobschedule #job_name = #job,
#name = 'MySchedule',
#freq_type=4,
#freq_interval=4,
#freq_subday_type=0x4,
#freq_subday_interval = 5,
#active_start_date = #startdate,
#active_start_time = #starttime
-- Add the job to the SQL Server Server
EXEC dbo.sp_add_jobserver
#job_name = #job,
#server_name = #servername
We're trying to add versioning to a ms sql database. Since the database has now been deployed to over 400 branches and with the clients insistence in staggered rollouts it has become very difficult to manage which branches have which db fixes.
I've proposed we add a release_version extended property to all the stored procs in the databases and use that to help with the management. After scanning the web I've come up with the following script but can't seem to get it working right; can anybody assist in getting the script right?
SELECT 'EXEC sys.sp_addextendedproperty
#name = N''Release_Version'',
#value = N''1.0.0'',
#level0type = N''SCHEMA'',
#level0name = [' + ROUTINE_SCHEMA + '],
#level1type = N''PROCEDURE'',
#level1name = [' + ROUTINE_NAME + '];'
FROM information_schema.routines where ROUTINE_TYPE = 'PROCEDURE' ORDER BY ROUTINE_NAME
When I execute this I get a resultset of the correct execution statements but how do I actually execute them?
EXEC sys.sp_addextendedproperty #name = N'Release_Version', #value = N'1.0.0', #level0type = N'SCHEMA', #level0name = [dbo], #level1type = N'PROCEDURE', #level1name = [sp_AlterTicketHistoryTable];
EXEC sys.sp_addextendedproperty #name = N'Release_Version', #value = N'1.0.0', #level0type = N'SCHEMA', #level0name = [dbo], #level1type = N'PROCEDURE', #level1name = [sp_AlterTicketTransTable];
EXEC sys.sp_addextendedproperty #name = N'Release_Version', #value = N'1.0.0', #level0type = N'SCHEMA', #level0name = [dbo], #level1type = N'PROCEDURE', #level1name = [sp_BackupAcknowledgementHistory];
...
[NOTE]: My mistake ... I was under the impression that the statements would execute automatically, instead I need to loop through and specifically execute.