SQLSERVER存儲過程基本語法


  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    存儲過程一般用在實現復雜的功能,數據操縱方面。

轉載:http://www.cnblogs.com/chaoa/articles/3894311.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM