MSSQL Transaction[事务] and Procedure[存储过程]
1 --事务分三种 2 --1.显示事务 3 --我们手动begin transaction ...... commit transaction/rollback transaction 4 --上面这种写法叫做“显示事务” 5 6 --2.隐式事务 7 SET IMPLICIT_TRANSACTIONS { ON | OFF }隐式事务 8 9 10 --3.自动提交事务,SQL Server默认使用的是自动提交事务. 11 --我们每次执行一条sql语句的时候,sql server都会自动帮我们打开一个事务 12 --如果该sql语句执行不出错,则sql server自动提交该事务commit 13 --如果该sql语句执行出错了,那么sql server则自动回滚该事务。rollback 14 15 --1:创建第一个简单的存储过程 16 CREATE PROC usp_helloworld 17 AS 18 BEGIN 19 PRINT 'hello world!' 20 END 21 22 EXEC usp_helloworld 23 24 --2:带参数的存储过程 25 CREATE PROC usp_showmsg 26 @msg nvarchar(300) ---多个参数使用,隔开。 27 AS 28 BEGIN 29 PRINT @msg 30 END 31 32 33 EXEC usp_showmsg N'XXX' 34 35 EXEC usp_showmsg @msg=N'11111111111' 36 37 38 39 --3:简单的加法存储过程 40 CREATE PROC usp_add 41 @n int, 42 @m int 43 AS 44 BEGIN 45 PRINT @n+@m 46 END 47 48 EXEC usp_add 100,200 49 50 DROP PROC usp_add 51 52 --4:带默认值的存储过程 53 ALTER PROC usp_add 54 @num1 int = 100, --为参数加默认值 55 @num2 int = 200 56 AS 57 BEGIN 58 PRINT @num1 + @num2 59 END 60 61 62 63 --5:部分带默认值的存储过程 64 ALTER PROC usp_add 65 @num1 int = 100, --为参数加默认值 66 @num2 int 67 AS 68 BEGIN 69 PRINT @num1 + @num2 70 END 71 72 exec usp_add @num2=50 73 74 --6:存储过程中的输出参数 75 SELECT * FROM TblStudent 76 77 ALTER PROC usp_selectByAge 78 @age int, 79 @count int OUTPUT -- 输出参数 80 AS 81 BEGIN 82 --1. 根据@age进行查询 83 SELECT * FROM TblStudent where tSAge>@age 84 85 --2.返回一共查询出了多少条数据 86 SET @count=(SELECT COUNT(*) FROM TblStudent where tsage>@age) 87 END 88 89 90 DECLARE @c int 91 EXEC usp_selectByAge @age=20,@count=@c OUTPUT 92 PRINT @c 93 94 95 --7:经典写法:转账使用存储过程来编写 96 --存储过程 97 CREATE PROC usp_transfer 98 @from char(4), 99 @to char(4), 100 @money money, 101 @state bit OUTPUT --1表示转账成功,0表示转账失败! 102 AS 103 BEGIN 104 DECLARE @balance money,@sum int=0 105 SET @state=0 106 --1.查询@from中的余额 107 SET @balance=(SELECT balance FROM bank WHERE bank.cId=@from) 108 --2.检查余额是否充足 109 IF @balance-@money>10 BEGIN 110 ----3.开始事务进行转账 111 --打开事务 112 BEGIN TRANSACTION 113 --1.减钱 114 UPDATE bank SET balance=bank.balance-@money where bank.cId=@from 115 SET @sum=@sum+@@error 116 --2.加钱 117 UPDATE bank SET balance=bank.balance+@money where bank.cId=@to 118 SET @sum=@sum+@@error 119 120 IF @sum=0 BEGIN 121 SET @state=1 --设置转账成功 122 COMMIT TRAN 123 END 124 ELSE 125 ROLLBACK TRAN 126 127 END 128 END 129 --调用存储过程 130 DECLARE @ok bit 131 EXEC usp_transfer @from='0002',@to='0001',@money=800,@state=@ok OUTPUT 132 SELECT @ok 133 134 SELECT * FROM bank 135 136 137 138 --8:经典:分页存储过程 139 SELECT * FROM Customers order by Customers.CustomerID asc 140 141 --创建存储过程 142 --参数: 143 --每页大小pagesize 144 --当先用户要查看第几页 145 --总页数 146 --总条数 147 CREATE PROC usp_get_customers_by_page 148 @pagesize int=5, --每页多少条 149 @pageindex int=1, --用户当前要看第几页 150 @recordcount int OUTPUT, --总记录条数,输出参数 151 @pagecont int OUTPUT --总页数,输出参数 152 AS 153 BEGIN 154 --1. 分页查询语句 155 SELECT 156 T.CustomerID, 157 T.CompanyName, 158 T.City, 159 T.[Address], 160 T.Phone 161 FROM (SELECT *,rn=ROW_NUMBER()OVER(ORDER by CustomerID ASC) FROM Customers) AS T 162 WHERE T.rn BETWEEN @pagesize*(@pageindex-1)+1 and @pagesize*@pageindex 163 164 --2.返回总条数 165 SET @recordcount=(SELECT COUNT(*) FROM Customers) 166 167 --3.计算总页数,并返回 168 SET @pagecont=CEILING(@recordcount*1.0/@pagesize); 169 END 170 171 --调用存储过程 172 DECLARE @pc int,@rc int 173 EXEC usp_get_customers_by_page 7,13,@rc OUTPUT,@pc OUTPUT 174 PRINT @pc 175 PRINT @rc 176 177 178 179 --9:增删改查:存储过程 180 --insert 181 CREATE PROC usp_insert_newperson 182 @name nvarchar(50), 183 @age int 184 AS 185 BEGIN 186 INSERT into NewPerson VALUES(@name,@age) 187 END 188 ------------------------------ 189 --delete 190 CREATE PROC usp_delete_newperson 191 @autoId int 192 AS 193 BEGIN 194 DELETE from NewPerson where NewPerson.autoId=@autoId 195 END 196 ------------------------------ 197 --update 198 CREATE PROC usp_update_newperson 199 @autoId int, 200 @name nvarchar(50), 201 @age int 202 AS 203 BEGIN 204 UPDATE NewPerson set uName=@name,age=@age WHERE NewPerson.autoId=@autoId 205 END 206 --------------------------------------- 207 --select 208 CREATE PROC usp_select_newperson 209 AS 210 BEGIN 211 SELECT * FROM NewPerson 212 END 213 ------------------------------------- 214 CREATE view vw_newperson 215 AS 216 SELECT * FROM NewPerson 217 218 219 --10:其它----- 220 SELECT * FROM Contacts 221 SELECT * FROM deleted 222 223 INSERT into NewPerson (NewPerson.uName,age) 224 OUTPUT INSERTED.autoId VALUES('王子斌',20) 225 226 ----inserted------------------- deleted----------------------- 227 228 SELECT * FROM Customers 229 230 DELETE from Customers where Customers.CustomerID='CHOPS' 231 232 233 SELECT TOP 0 * INTO CustomersBak FROM Customers 234 235 SELECT * FROM CustomersBak 236 237 ALTER TRIGGER tri_customers_after_delete ON Customers 238 AFTER DELETE 239 AS 240 BEGIN 241 INSERT INTO CustomersBak SELECT * FROM DELETED 242 END 243 244 SELECT * FROM Customers 245 246 247 DELETE from Customers where Customers.CustomerID IN('CACTU','CENTC','ERNSH') 248 249 250 251 CREATE TABLE A20140222 ( A VARCHAR(10)) 252 253 254 DECLARE @SQL varchar(500) 255 SET @SQL='CREATE TABLE T_'+CONVERT(VARCHAR(10),GETDATE(),112)+' ( A VARCHAR(10))' 256 PRINT @SQL 257 EXEC(@SQL ) 258 259 PRINT CONVERT(VARCHAR(10),GETDATE(),112) 260 261 262 263 264 265
code write the life, programe change the world