在SQL

时间:2018-08-04 05:52:45

标签: sql sql-server

我必须在生产数据库中运行删除和更新查询,并且where条件过于复杂而无法应用。我已经创建了查询,但是在执行之前我想测试该查询。因此,我想在应用实际的删除查询之前回滚查询。该怎么做?

2 个答案:

答案 0 :(得分:3)

使用BEGIN TRANS启动事务,然后执行查询。

然后查看查询结果,如果它们是您想要的,则可以提交事务,否则可以使用ROLLBACK命令将其回滚。

这里是一个例子:

BEGIN TRAN t1 -- Begin the transaction

      DELETE * FROM Table_Name WHERE col=1  -- Do the "scary operation" in 
      --the production environment
      SELECT * FROM Table_Name -- Make sure it did what you thought it should 
      have.
      -- Then depending on results: 

COMMIT TRAN t1 -- Satisfied with results - Commits the transaction (delete 
operation)

ROLLBACK TRAN t1 -- Results not as expected - Rollback the transaction.

答案 1 :(得分:2)

bool OutputKnownWords()
{
    while (m_stream.good())
    {
        if (Take("MIDDLE"))
            std::cout << "Found middle" << std::endl;
        else if (Take("BEGIN"))
            std::cout << "Found begin" << std::endl;
        else if (Take("END"))
            std::cout << "Found end" << std::endl;
        else if (Take(" "))
            std::cout << "parsed out space" << std::endl;
        else
            // stream can only be not good at EOF
            return !m_stream.good();
    }
    return true;
}