在脚本中设置变量和使用 [英] Setting Variable and Use in Script

查看:72
本文介绍了在脚本中设置变量和使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能很容易,但是我还没有弄清楚.我需要做的是声明一个变量,将该变量设置为特定的日期格式,然后在执行数据库备份或还原时将该变量用作文件名的一部分.

这是我到目前为止的内容:

This is probably easy, but I haven''t figured it out yet. What I need to do is declare a variable set the variable to be a specific date format, and then use that variable as part of a file name when doing a database backup or restore.

Here''s what I have so far:

DECLARE @FILEDATE VARCHAR(8)
SET @FILEDATE =
	(SELECT CONVERT(VARCHAR(8), GETDATE(), 112) AS [YYYYMMDD]);

-上面的方法可以正常工作-如果我选择@filedate,它将完全返回我需要的内容.

-以下是我遇到使用问题的地方.

-- The above works fine - if I select @filedate it returns exactly what I need.

-- The following is where I have the use problem/question.

backup database Northwinds to disk='d:\temp\northwinds@filedate.bak' WITH INIT


上面生成的是d:\ temp \ northwinds@filedate.bak,而不是所需的d:\ temp \ northwinds20111007.bak

我将使用基本结构来处理我正在处理的各种备份和还原脚本.

非常感谢您的协助和指导!


What the above is generating is d:\temp\northwinds@filedate.bak, not the desired d:\temp\northwinds20111007.bak

I''ll be using the basic structure for a variety of backup and restore scripts that I''m working on.

Your assistance and guidance is greatly appreciated!

推荐答案

尝试一下:
Try this:
DECLARE @BackupName varchar(100);
SET @BackupName = 'd:\temp\northwinds'+ convert(char(8), getdate(), 112) + '.BAK';
BACKUP DATABASE Northwinds TO DISK = @BackupName WITH INIT


这篇关于在脚本中设置变量和使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆