简体   繁体   English

删除和添加列时纠正错误处理

[英]Correct error handling when dropping and adding columns

I have a table that is currently using a couple of columns named DateFrom and DateTo . 我有一个表,当前正在使用几个名为DateFromDateTo的列。 I'm trying to replace them with a single NewDate column, populated for existing rows with the value from DateFrom . 我试图用一个NewDate列替换它们,并使用DateFrom的值填充现有行。

I need good error/transaction handling as, if the change fails, I don't want a halfway in-between table, I want to revert. 我需要良好的错误/事务处理,因为如果更改失败,我不希望在表中间进行操作,我想还原。

I've tried a number of things but can't get it to work properly. 我已经尝试了很多方法,但是无法使其正常工作。 Any help is appreciated as I'm far from experienced with this. 感谢您提供任何帮助,因为我对此经验还远远不够。

I started with 我开始

BEGIN TRAN

ALTER TABLE TableName
ADD NewDate DATETIME

IF @@ERROR = 0 AND @@TRANCOUNT = 1
UPDATE TableName
SET NewDate = ValidFrom
....

This fails immediately as NewDate is not currently a column in the table. 由于NewDate不在表中的列,因此此操作立即失败。 Fine, so I add a GO in there. 很好,因此我在其中添加了GO This breaks it into two batches and it now runs, except it makes the @@ERROR check pointless. 这将其分为两批,并且现在可以运行,只不过它使@@ERROR检查变得毫无意义。 I also can't use a local variable as those are lost after GO as well. 我也不能使用局部变量,因为那些局部变量也会在GO之后丢失。 Ideally I'd like to use a TRY...CATCH to avoid checking errors after each statement but I can't use a GO with that as it needs to be one batch. 理想情况下,我想使用TRY...CATCH来避免在每条语句后检查错误,但是我不能使用GO ,因为它需要一批。

None of the articles I've found talk about this situation (error handling with GO ). 我没有找到有关这种情况的文章(使用GO处理错误)。 So the question is: Is there any way I can get the transaction-with-error-handling approach I'm looking for when adding and updating a column (which seems to necessitate a GO somewhere)? 所以问题是:添加和更新列(似乎需要在某个地方执行GO )时,是否有任何方法可以获得我正在寻找的带错误处理事务方法?

Or am I going to have to settle for doing it in several batches, without the ability to roll back to my original table if anything goes wrong? 还是我将不得不分批完成该工作,而如果出现任何问题又无法回滚到原始表?

Why are you worried about creating the new column in the transaction? 您为什么担心在事务中创建新列? Just create the column and then populate it. 只需创建列,然后填充它即可。 You don't even need an explicit tran when populating it. 填充它时甚至不需要显式tran。 If it fails (which is very unlikely), just do the update again. 如果失败(极不可能),只需再次执行更新即可。

I would do the following steps 我将执行以下步骤

  1. Add new column 添加新列
  2. Update new column 更新新栏
  3. Check to see if the data in the new column looks correct 检查新列中的数据是否正确
  4. Drop the old columns no longer needed (you may want to check where these columns are being used before dropping them eg are they used in any stored procedures, reports, front-end application code) 删除不再需要的旧列(删除它们之前,您可能需要检查这些列在何处使用,例如,它们是否在任何存储过程,报表,前端应用程序代码中使用)

Also, it is worth adding more context to your question. 另外,值得为您的问题添加更多上下文。 I assume you are testing a script against a test database and will later apply the script to a prod database. 我假设您正在针对测试数据库测试脚本,然后将其应用于产品数据库。 Is the prod database very big? prod数据库很大吗? Very busy? 很忙? Mission critical? 关键任务? Backed up on a schedule? 是否按计划备份?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM