SQL SERVER 相关

while循环

declare @sum int;
declare @i int;
set @sum=0;
set @i=0;
while(@i<=100)
begin
  if(@i%2!=0)
  begin
  set @sum=@sum+@i;
  end  
set @i=@i+1;
end
select @sum;

事务

USE [shopdb]
GO
/****** Object:  Table [dbo].[test]    Script Date: 08/15/2018 15:29:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[test](
    [cid] [int] IDENTITY(1,1) NOT NULL,
    [balance] [int] NULL,
 CONSTRAINT [PK_test] PRIMARY KEY CLUSTERED 
(
    [cid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
USE [shopdb]
GO
ALTER TABLE [dbo].[test]  WITH CHECK ADD  CONSTRAINT [CK_test] CHECK  (([balance]>(10)))
begin transaction
declare @sumError int;
set @sumError =0;
update test set balance=balance-100 where cid=2;
set @sumError = @sumError+@@error;
update test set balance=balance+100 where cid=3;
set @sumError=@sumError+@@error;

if(@sumError<>0)
begin
  rollback transaction
end
else
begin
  commit transaction
end

猜你喜欢

转载自www.cnblogs.com/youguess/p/9481716.html