如果..begin..begin tran..Rollback

时间:2017-10-13 14:06:35

标签: sql sql-server-2008-r2

我已经关注了代码,但是它出错了,请指导我如何嵌套if..begin..begin__tran__Rollback或commit

declare @acname as varchar(max),
    @gstcount as int

set @acname='Party1'

use TESTDB

set @gstcount = (select count(GSTIN) from M_STATEWISEGSTINACTAG where GSTIN<>'UNREGISTERED' AND ACCD=
(select accd from m_ledger where descr LIKE @acname))


if @gstcount = 1 
   begin 
    --multiple select statments 
    select .......
    --to display require info.
   end

 --Then Update Values
     begin tran;
       update M_tbl ........where....

    --Show result with updated value
       select * from M_tbl........where.........

    --Undo Changes
      ROLLBACK; 
else
 begin
      SELECT ........reuqir info

 end;

在此脚本中,我在关键字&#39;其他&#39;附近出现错误&#39;语法不正确。

1 个答案:

答案 0 :(得分:0)

试试这个:

DECLARE @acname AS VARCHAR(MAX), @gstcount AS INT;
SET @acname = 'Party1';
USE TESTDB;
SET @gstcount =
(
    SELECT COUNT(GSTIN)
    FROM M_STATEWISEGSTINACTAG
    WHERE GSTIN <> 'UNREGISTERED' AND ACCD =(SELECT accd FROM m_ledger WHERE descr LIKE @acname)
);
IF @gstcount = 1
    BEGIN
        BEGIN 
    --multiple select statments 

            SELECT .......
    --to display require info.
       END;

 --Then Update Values
        BEGIN TRAN;
          update M_tbl ........where....
    --Show result with updated value
          select * from M_tbl........where.........
    --Undo Changes
        ROLLBACK TRAN;
    END;
    ELSE
    BEGIN
        SELECT........reuqir info;
    END;