恢复仅通过更新验证循环
我正在运行Restore验证仅在表中验证循环,但是如果成功的话,我需要它来更新该表中的字段。这是我的代码:
DECLARE @Path NVARCHAR(max)
DECLARE @DatabaseName NVARCHAR(100)
DECLARE @NSql NVARCHAR(1000)
DECLARE @Update NVARCHAR(200)
DECLARE @DB_Text NVARCHAR(50)= 'Backup has been confirmed for'
BEGIN
DECLARE
@DatabaseId INT = 1,
@NumberOfDBs INT
SELECT @NumberOfDBs= COUNT(*) FROM dbo.RestoreVerifyDatabases
WHILE @DatabaseId<= @NumberOfDBs
BEGIN
SELECT * FROM dbo.RestoreVerifyDatabases WHERE DatabaseId= @DatabaseId
SET @DatabaseId = @DatabaseId + 1
END
SET @Path = (SELECT LastBackupFileName FROM RestoreVerifyDatabases WHERE DatabaseId =
@DatabaseId)
SET @DatabaseName = (SELECT DatabaseName FROM RestoreVerifyDatabases WHERE DatabaseId =
@DatabaseId)
SET @NSql = N'SELECT LastBackupFileName
FROM RestoreVerifyDatabases
WHERE DatabaseName = @DatabaseName
AND DatabaseId = @DatabaseId'
EXEC sp_executesql @NSql
IF @DatabaseId IS NULL
BEGIN
RAISERROR(N'Verify failed. Backup information for database N''@DatabaseName'' not
found.', 16, 1)
END
RESTORE VERIFYONLY
FROM @Path
WITH FILE = @DatabaseId, checksum
SET @Update= N'UPDATE RestoreVerifyDatabases
SET Confirmed = @DB_Text + @DatabaseName
WHERE DatabaseID = @DatabaseId'
EXEC sp_executesql @Update
END
Restore验证的循环和调用仅工作正常,但是更新未被调用。请帮忙。
I'm running a Restore Verify Only loop through a table, but I need it to UPDATE a field in that table if it's successful. Here is my code:
DECLARE @Path NVARCHAR(max)
DECLARE @DatabaseName NVARCHAR(100)
DECLARE @NSql NVARCHAR(1000)
DECLARE @Update NVARCHAR(200)
DECLARE @DB_Text NVARCHAR(50)= 'Backup has been confirmed for'
BEGIN
DECLARE
@DatabaseId INT = 1,
@NumberOfDBs INT
SELECT @NumberOfDBs= COUNT(*) FROM dbo.RestoreVerifyDatabases
WHILE @DatabaseId<= @NumberOfDBs
BEGIN
SELECT * FROM dbo.RestoreVerifyDatabases WHERE DatabaseId= @DatabaseId
SET @DatabaseId = @DatabaseId + 1
END
SET @Path = (SELECT LastBackupFileName FROM RestoreVerifyDatabases WHERE DatabaseId =
@DatabaseId)
SET @DatabaseName = (SELECT DatabaseName FROM RestoreVerifyDatabases WHERE DatabaseId =
@DatabaseId)
SET @NSql = N'SELECT LastBackupFileName
FROM RestoreVerifyDatabases
WHERE DatabaseName = @DatabaseName
AND DatabaseId = @DatabaseId'
EXEC sp_executesql @NSql
IF @DatabaseId IS NULL
BEGIN
RAISERROR(N'Verify failed. Backup information for database N''@DatabaseName'' not
found.', 16, 1)
END
RESTORE VERIFYONLY
FROM @Path
WITH FILE = @DatabaseId, checksum
SET @Update= N'UPDATE RestoreVerifyDatabases
SET Confirmed = @DB_Text + @DatabaseName
WHERE DatabaseID = @DatabaseId'
EXEC sp_executesql @Update
END
The looping and calling of Restore Verify Only works fine but the UPDATE is not getting called. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,您的代码有一些错误。正如我的评论所示,您可以通过
@databaseid
进行循环,然后立即在循环中增加ID。这意味着您的
@databaseid
只会增加直到达到ID的最大值,然后退出循环。然后,您的其余代码将执行一次(仅适用于该ID)。这在您的代码中发生:
因此,@databaseid从该循环中的1到50(只是一个随机数),然后以50的值存在。没有一个基础代码可以看到50个以外的任何值。
用于修复。这是这样的,应该看起来像这样:
这将确保“其他代码”进程@databaseid = 1,然后处理@databaseid = 2等。直到@databaseid = 50。
您也可以删除此行:
我怀疑您将其用于调试目的,但实际上并没有做任何事情。
这些行可以简化:
AS:
您也有一个raiseError。我怀疑这是因为您进行了计数(*),而不是选择实际现有的@databaseids。这在理想的情况下起作用,在该方案中,数据库永远不会被删除。但是,在REASL世界中,您会从数据库1到5,因为2、3和4被删除。您的计数仍将是50个数据库,但是由于这些差距,您会错过所有具有高于计数的数据库。您将尝试处理不再存在的数据库,并错过具有ID&GT的数据库; 50。
您可以写下循环以执行以下操作:
最后,您的动态代码无法正常工作。您正在添加字面文本,而不是参数的值。如果我在末尾打印@update的值,则显示为:
您的代码应该是:
输出:
There's a few things wrong with your code as far as I can see. As my comment indicated, you run a loop over the
@databaseID
, and then immediately increase the id with 1 within the loop.This means your
@databaseId
simply will increase until it reaches the maximum value of the id, and then exits the loop. The rest of your code will then execute once (only for that ID).That happens here in your code:
So @databaseID goes from 1 to 50 (just a random number) within that loop, then exists with a value of 50. None of the underlying code ever gets to see any value other than 50.
To fix that, the code should look like this:
This would ensure the "bunch of other code" processes @databaseid = 1, then processes @databaseid = 2, etc. until @databaseid = 50.
You can also remove this line:
I suspect you have it for debugging purposes, but it doesn't really do anything.
These lines can be simplified:
as:
You also have a RAISEERROR. I suspect that is because you do a count(*), rather than selecting actually existing @databaseids. This works in an ideal scenario, where databases never get deleted. In the reasl world, though, you'd go from databaseid 1 to 5, because 2, 3, and 4 were removed. Your count will still be 50 databases, but you'd miss all databases with ids above the count due to these gaps. You'd be trying to process the databaseid that no longer exists, and miss the ones with an id > 50.
You could instead write your loop to do something like the following:
Finally, your dynamic code isn't working. You are adding literal text rather than the value of the parameters. If I print the value of @Update at the end, it shows as:
Your code should be something like:
Which outputs: