The SQL side uses the script to determine if the folder exists and creates

Source: Internet
Author: User
Tags db2 getdate

When doing a database backup job: You want to create a folder by date ...

The first thing to tell is whether the folder exists

DECLARE @PATH VARCHAR(255)--PathDECLARE @DATE VARCHAR(255)--date for example: 20171011SET @DATE=CAST(DATEPART(YYYY,GETDATE()) as VARCHAR)+CAST(DATEPART(MM,GETDATE()) as VARCHAR)+CAST(DATEPART(DD,GETDATE()) as VARCHAR)SET @PATH = 'E:\SQL2012_BAK\'+@DATE+'\'DECLARE @TEMP TABLE(AINTBINTCINT)--Create a virtual table to determine if a folder existsINSERT @TEMP EXEC [MASTER].. Xp_fileexist@PATHIF  not EXISTS(SELECT *  from @TEMP WHEREB=1) BEGIN    --The exec (SQL) method is used because the parameters of the xp_cmdshell function do not allow the use of variable stitching    DECLARE @EX NVARCHAR(255)    SET @EX='EXEC xp_cmdshell"'MKDIR'+@PATH+" '"    EXEC(@EX)END

If an error

15281  - 1 1 row SQL Server blocked access to the process "Sys.xp_cmdshell" of the component "xp_cmdshell" because this component was shut down as part of this server's security configuration. 

Use the following modification configuration item to wrap the script above

--Open xp_cmdshellGOsp_configure'SHOW Advanced OPTIONS',1RECONFIGUREGOsp_configure'xp_cmdshell',1RECONFIGUREGO//===================here is a script that uses xp_cmdshell//===================--Close xp_cmdshellGOsp_configure'xp_cmdshell',0RECONFIGUREGOsp_configure'SHOW Advanced OPTIONS',0RECONFIGUREGO

Finally paste the database backup command

--backing up the database--Here you can place backups of multiple databases without having to select the same library as the custom job stepDECLARE @BAKFILE NVARCHAR(255)SET @BAKFILE = @PATH+@DATE+'. DB1. BAK'BACKUP DATABASE [DB1]  to DISK = @BAKFILE  withCOMPRESSIONSET @BAKFILE = @PATH+@DATE+'. DB2. BAK'BACKUP DATABASE [DB2]  to DISK = @BAKFILE  withCOMPRESSION

In summary, the final version of the SQL BACKUP command is: (expand View)

-----------------------------Open xp_cmdshell---------------------------GOsp_configure'SHOW Advanced OPTIONS',1RECONFIGUREGOsp_configure'xp_cmdshell',1RECONFIGUREGO-----------------------------checks if a folder exists or does not exist then creates---------------------------DECLARE @PATH VARCHAR(255)--PathDECLARE @DATE VARCHAR(255)--date for example: 20171011SET @DATE=CAST(DATEPART(YYYY,GETDATE()) as VARCHAR)+CAST(DATEPART(MM,GETDATE()) as VARCHAR)+CAST(DATEPART(DD,GETDATE()) as VARCHAR)SET @PATH = 'E:\SQL2012_BAK\'+@DATE+'\'DECLARE @TEMP TABLE(AINTBINTCINT)--Create a virtual table to determine if a folder existsINSERT @TEMP EXEC [MASTER].. Xp_fileexist@PATHIF  not EXISTS(SELECT *  from @TEMP WHEREB=1) BEGIN    --The exec (SQL) method is used because the parameters of the xp_cmdshell function do not allow the use of variable stitching    DECLARE @EX NVARCHAR(255)    SET @EX='EXEC xp_cmdshell"'MKDIR'+@PATH+" '"    EXEC(@EX)END -----------------------------backing up the database---------------------------DECLARE @BAKFILE NVARCHAR(255)SET @BAKFILE = @PATH+@DATE+'. DB1. BAK'BACKUP DATABASE [DB1]  to DISK = @BAKFILE  withCOMPRESSIONSET @BAKFILE = @PATH+@DATE+'. DB2. BAK'BACKUP DATABASE [DB2]  to DISK = @BAKFILE  withCOMPRESSION-----------------------------Close xp_cmdshell---------------------------GOsp_configure'xp_cmdshell',0RECONFIGUREGOsp_configure'SHOW Advanced OPTIONS',0RECONFIGUREGO
View Code

The SQL side uses the script to determine if the folder exists and creates

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.