sql server 存储过程基本语法(转)

  1 一、定义变量
  2 --简单赋值 
  3 declare @a int
  4 set @a=5 
  5 print @a 
  6   
  7 --使用select语句赋值 
  8 declare @user1 nvarchar(50) 
  9 select @user1='张三'
 10 print @user1 
 11 declare @user2 nvarchar(50) 
 12 select @user2 = Name from ST_User where ID=1 
 13 print @user2 
 14   
 15 --使用update语句赋值 
 16 declare @user3 nvarchar(50) 
 17 update ST_User set @user3 = Name where ID=1 
 18 print @user3
 19  
 20 
 21 二、表、临时表、表变量
 22 
 23 --创建临时表1 
 24 create table #DU_User1 
 25 ( 
 26      [ID] [int]  NOT NULL, 
 27      [Oid] [int] NOT NULL, 
 28      [Login] [nvarchar](50) NOT NULL, 
 29      [Rtx] [nvarchar](4) NOT NULL, 
 30      [Name] [nvarchar](5) NOT NULL, 
 31      [Password] [nvarchar](max) NULL, 
 32      [State] [nvarchar](8) NOT NULL
 33 ); 
 34 --向临时表1插入一条记录 
 35 insert into #DU_User1 (ID,Oid,[Login],Rtx,Name,[Password],State) values (100,2,'LS','0000','临时','321','特殊'); 
 36   
 37 --从ST_User查询数据,填充至新生成的临时表 
 38 select * into #DU_User2 from ST_User where ID<8 
 39   
 40 --查询并联合两临时表 
 41 select * from #DU_User2 where ID<3 union select * from #DU_User1 
 42   
 43 --删除两临时表 
 44 drop table #DU_User1 
 45 drop table #DU_User2
 46  
 47 --创建临时表 
 48 CREATE TABLE #t 
 49 ( 
 50     [ID] [int] NOT NULL, 
 51     [Oid] [int] NOT NULL, 
 52     [Login] [nvarchar](50) NOT NULL, 
 53     [Rtx] [nvarchar](4) NOT NULL, 
 54     [Name] [nvarchar](5) NOT NULL, 
 55     [Password] [nvarchar](max) NULL, 
 56     [State] [nvarchar](8) NOT NULL, 
 57 ) 
 58   
 59 --将查询结果集(多条数据)插入临时表 
 60 insert into #t select * from ST_User 
 61 --不能这样插入 
 62 --select * into #t from dbo.ST_User 
 63   
 64 --添加一列,为int型自增长子段 
 65 alter table #t add [myid] int NOT NULL IDENTITY(1,1) 
 66 --添加一列,默认填充全球唯一标识 
 67 alter table #t add [myid1] uniqueidentifier NOT NULL default(newid()) 
 68   
 69 select * from #t 
 70 drop table #t
 71 --给查询结果集增加自增长列 
 72   
 73 --无主键时: 
 74 select IDENTITY(int,1,1)as ID, Name,[Login],[Password] into #t from ST_User 
 75 select * from #t 
 76   
 77 --有主键时: 
 78 select (select SUM(1) from ST_User where ID<= a.ID) as myID,* from ST_User a order by myID
 79 --定义表变量 
 80 declare @t table
 81 ( 
 82     id int not null, 
 83     msg nvarchar(50) null
 84 ) 
 85 insert into @t values(1,'1') 
 86 insert into @t values(2,'2') 
 87 select * from @t
 88  三、循环
 89 
 90 --while循环计算1到100的和 
 91 declare @a int
 92 declare @sum int
 93 set @a=1 
 94 set @sum=0 
 95 while @a<=100 
 96 begin
 97     set @sum+=@a 
 98     set @a+=1 
 99 end
100 print @sum
101 四、条件语句
102 
103 --if,else条件分支 
104 if(1+1=2) 
105 begin
106     print ''
107 end
108 else
109 begin
110     print ''
111 end
112   
113 --when then条件分支 
114 declare @today int
115 declare @week nvarchar(3) 
116 set @today=3 
117 set @week=case
118     when @today=1 then '星期一'
119     when @today=2 then '星期二'
120     when @today=3 then '星期三'
121     when @today=4 then '星期四'
122     when @today=5 then '星期五'
123     when @today=6 then '星期六'
124     when @today=7 then '星期日'
125     else '值错误'
126 end
127 print @week
128  
129 
130 五、游标
131 
132 declare @ID int
133 declare @Oid int
134 declare @Login varchar(50) 
135   
136 --定义一个游标 
137 declare user_cur cursor for select ID,Oid,[Login] from ST_User 
138 --打开游标 
139 open user_cur 
140 while @@fetch_status=0 
141 begin
142 --读取游标 
143     fetch next from user_cur into @ID,@Oid,@Login 
144     print @ID 
145     --print @Login 
146 end
147 close user_cur 
148 --摧毁游标 
149 deallocate user_cur
150 六、触发器
151 
152   触发器中的临时表:
153 
154   Inserted 
155   存放进行insert和update 操作后的数据 
156   Deleted 
157   存放进行delete 和update操作前的数据
158 
159 --创建触发器 
160 Create trigger User_OnUpdate  
161     On ST_User  
162     for Update 
163 As 
164     declare @msg nvarchar(50) 
165     --@msg记录修改情况 
166     select @msg = N'姓名从“' + Deleted.Name + N'”修改为“' + Inserted.Name + '' from Inserted,Deleted 
167     --插入日志表 
168     insert into [LOG](MSG)values(@msg) 
169       
170 --删除触发器 
171 drop trigger User_OnUpdate
172 七、存储过程
173 
174 --创建带output参数的存储过程 
175 CREATE PROCEDURE PR_Sum 
176     @a int, 
177     @b int, 
178     @sum int output
179 AS
180 BEGIN
181     set @sum=@a+@b 
182 END
183   
184 --创建Return返回值存储过程 
185 CREATE PROCEDURE PR_Sum2 
186     @a int, 
187     @b int
188 AS
189 BEGIN
190     Return @a+@b 
191 END
192       
193 --执行存储过程获取output型返回值 
194 declare @mysum int
195 execute PR_Sum 1,2,@mysum output
196 print @mysum 
197   
198 --执行存储过程获取Return型返回值 
199 declare @mysum2 int
200 execute @mysum2= PR_Sum2 1,2 
201 print @mysum2
202  
203   
204 
205 八、自定义函数
206 
207   函数的分类:
208 
209     1)标量值函数
210 
211     2)表值函数
212 
213         a:内联表值函数
214 
215         b:多语句表值函数
216 
217     3)系统函数
218 
219   
220 
221 --新建标量值函数 
222 create function FUNC_Sum1 
223 ( 
224     @a int, 
225     @b int
226 ) 
227 returns int
228 as
229 begin
230     return @a+@b 
231 end
232   
233 --新建内联表值函数 
234 create function FUNC_UserTab_1 
235 ( 
236     @myId int
237 ) 
238 returns table
239 as
240 return (select * from ST_User where ID<@myId) 
241   
242 --新建多语句表值函数 
243 create function FUNC_UserTab_2 
244 ( 
245     @myId int
246 ) 
247 returns @t table
248 ( 
249     [ID] [int] NOT NULL, 
250     [Oid] [int] NOT NULL, 
251     [Login] [nvarchar](50) NOT NULL, 
252     [Rtx] [nvarchar](4) NOT NULL, 
253     [Name] [nvarchar](5) NOT NULL, 
254     [Password] [nvarchar](max) NULL, 
255     [State] [nvarchar](8) NOT NULL
256 ) 
257 as
258 begin
259     insert into @t select * from ST_User where ID<@myId 
260     return
261 end
262   
263 --调用表值函数 
264 select * from dbo.FUNC_UserTab_1(15) 
265 --调用标量值函数 
266 declare @s int
267 set @s=dbo.FUNC_Sum1(100,50) 
268 print @s 
269   
270 --删除标量值函数 
271 drop function FUNC_Sum1
272 谈谈自定义函数与存储过程的区别:
273 
274 一、自定义函数:
275 
276   1. 可以返回表变量
277 
278   2. 限制颇多,包括
279 
280     不能使用output参数;
281 
282     不能用临时表;
283 
284     函数内部的操作不能影响到外部环境;
285 
286     不能通过select返回结果集;
287 
288     不能update,delete,数据库表;
289 
290   3. 必须return 一个标量值或表变量
291 
292   自定义函数一般用在复用度高,功能简单单一,争对性强的地方。
293 
294 二、存储过程
295 
296   1. 不能返回表变量
297 
298   2. 限制少,可以执行对数据库表的操作,可以返回数据集
299 
300   3. 可以return一个标量值,也可以省略return
301 
302    存储过程一般用在实现复杂的功能,数据操纵方面。

猜你喜欢

转载自www.cnblogs.com/yt501/p/10267064.html
今日推荐