SQLServer-儲存過程
儲存過程
一、定義變數
--簡單賦值 declare @a int set @a=5 print @a --使用select語句賦值 declare @user1 nvarchar(50) select @user1='張三' print @user1 declare @user2 nvarchar(50) select @user2 = Name from ST_User where ID=1 print @user2 --使用update語句賦值 declare @user3 nvarchar(50) update ST_User set @user3 = Name where ID=1 print @user3
PS:個人補充。
SET 與 SELECT 的區別:
set | select | |
---|---|---|
同時對多個變數同時賦值 | 不支援 | 支援 |
表示式返回多個值時 | 出錯 | 將返回的最後一個值賦給變數 |
表示式未返回值 | 變數被賦null值 | 變數保持原值 |
二、表、臨時表、表變數
--建立臨時表1 create table #DU_User1 ( [ID] [int] NOT NULL, [Oid] [int] NOT NULL, [Login] [nvarchar](50) NOT NULL, [Rtx] [nvarchar](4) NOT NULL, [Name] [nvarchar](5) NOT NULL, [Password] [nvarchar](max) NULL, [State] [nvarchar](8) NOT NULL ); --向臨時表1插入一條記錄 insert into #DU_User1 (ID,Oid,[Login],Rtx,Name,[Password],State) values (100,2,'LS','0000','臨時','321','特殊'); --從ST_User查詢資料,填充至新生成的臨時表 select * into #DU_User2 from ST_User where ID<8 --查詢並聯合兩臨時表 select * from #DU_User2 where ID<3 union select * from #DU_User1 --刪除兩臨時表 drop table #DU_User1 drop table #DU_User2 --建立臨時表 CREATE TABLE #t ( [ID] [int] NOT NULL, [Oid] [int] NOT NULL, [Login] [nvarchar](50) NOT NULL, [Rtx] [nvarchar](4) NOT NULL, [Name] [nvarchar](5) NOT NULL, [Password] [nvarchar](max) NULL, [State] [nvarchar](8) NOT NULL, ) --將查詢結果集(多條資料)插入臨時表 insert into #t select * from ST_User --不能這樣插入 --select * into #t from dbo.ST_User --新增一列,為int型自增長子段 alter table #t add [myid] int NOT NULL IDENTITY(1,1) --新增一列,預設填充全球唯一標識 alter table #t add [myid1] uniqueidentifier NOT NULL default(newid()) select * from #t drop table #t --給查詢結果集增加自增長列 --無主鍵時: select IDENTITY(int,1,1)as ID, Name,[Login],[Password] into #t from ST_User select * from #t --有主鍵時: select (select SUM(1) from ST_User where ID<= a.ID) as myID,* from ST_User a order by myID --定義表變數 declare @t table ( id int not null, msg nvarchar(50) null ) insert into @t values(1,'1') insert into @t values(2,'2') select * from @t
三、迴圈
--while迴圈計算1到100的和
declare @a int
declare @sum int
set @a=1
set @sum=0
while @a<=100
begin
set @[email protected]
set @a+=1
end
print @sum
四、條件語句
--if,else條件分支 if(1+1=2) begin print '對' end else begin print '錯' end --when then條件分支 declare @today int declare @week nvarchar(3) set @today=3 set @week=case when @today=1 then '星期一' when @today=2 then '星期二' when @today=3 then '星期三' when @today=4 then '星期四' when @today=5 then '星期五' when @today=6 then '星期六' when @today=7 then '星期日' else '值錯誤' end print @week
五、遊標
declare @ID int
declare @Oid int
declare @Login varchar(50)
--定義一個遊標
declare user_cur cursor for select ID,Oid,[Login] from ST_User
--開啟遊標
open user_cur
while @@fetch_status=0
begin
--讀取遊標
fetch next from user_cur into @ID,@Oid,@Login
print @ID
--print @Login
end
close user_cur
--摧毀遊標
deallocate user_cur
六、觸發器
觸發器中的臨時表:
Inserted
存放進行insert和update 操作後的資料
Deleted
存放進行delete 和update操作前的資料
--建立觸發器
Create trigger User_OnUpdate
On ST_User
for Update
As
declare @msg nvarchar(50)
[email protected]記錄修改情況
select @msg = N'姓名從“' + Deleted.Name + N'”修改為“' + Inserted.Name + '”' from Inserted,Deleted
--插入日誌表
insert into [LOG](MSG)values(@msg)
--刪除觸發器
drop trigger User_OnUpdate
七、儲存過程
--建立帶output引數的儲存過程
CREATE PROCEDURE PR_Sum
@a int,
@b int,
@sum int output
AS
BEGIN
set @[email protected][email protected]
END
--建立Return返回值儲存過程
CREATE PROCEDURE PR_Sum2
@a int,
@b int
AS
BEGIN
Return @[email protected]
END
--執行儲存過程獲取output型返回值
declare @mysum int
execute PR_Sum 1,2,@mysum output
print @mysum
--執行儲存過程獲取Return型返回值
declare @mysum2 int
execute @mysum2= PR_Sum2 1,2
print @mysum2
八、自定義函式
函式的分類:
- 標量值函式
- 表值函式
a:內聯表值函式
b:多語句表值函式 - 系統函式
--新建標量值函式
create function FUNC_Sum1
(
@a int,
@b int
)
returns int
as
begin
return @[email protected]
end
--新建內聯表值函式
create function FUNC_UserTab_1
(
@myId int
)
returns table
as
return (select * from ST_User where ID<@myId)
--新建多語句表值函式
create function FUNC_UserTab_2
(
@myId int
)
returns @t table
(
[ID] [int] NOT NULL,
[Oid] [int] NOT NULL,
[Login] [nvarchar](50) NOT NULL,
[Rtx] [nvarchar](4) NOT NULL,
[Name] [nvarchar](5) NOT NULL,
[Password] [nvarchar](max) NULL,
[State] [nvarchar](8) NOT NULL
)
as
begin
insert into @t select * from ST_User where ID<@myId
return
end
--呼叫表值函式
select * from dbo.FUNC_UserTab_1(15)
--呼叫標量值函式
declare @s int
set @s=dbo.FUNC_Sum1(100,50)
print @s
--刪除標量值函式
drop function FUNC_Sum1
談談自定義函式與儲存過程的區別:
一、自定義函式:
- 可以返回表變數
- 限制頗多,包括
不能使用output引數;
不能用臨時表;
函式內部的操作不能影響到外部環境;
不能通過select返回結果集;
不能update,delete,資料庫表; - 必須return 一個標量值或表變數
自定義函式一般用在複用度高,功能簡單單一,爭對性強的地方。
二、儲存過程
- 不能返回表變數
- 限制少,可以執行對資料庫表的操作,可以返回資料集
- 可以return一個標量值,也可以省略return
儲存過程一般用在實現複雜的功能,資料操縱方面。
以上內容轉載自:https://www.cnblogs.com/chaoa/articles/3894311.html
在此感謝該文章筆者,整理齊全,感覺很適合用於回顧複習。特此轉載一篇。
Time:2018-12-13
sql 儲存過程命名規範
規範的命名可以提高開發和維護的效率,如果你正在建立一個新的儲存過程,請參考如下的命名規範。
句法:
儲存過程的命名有這個的語法:
[proc] [MainTableName] By [FieldName(optional)] [Action]
[proc] | [MainTableName] | By | [FieldName(optional)] | [Action] |
---|---|---|---|---|
[1] | [2] | [3] | [4] |
[1] 所有的儲存過程必須有字首“proc_”,所有的系統儲存過程都有字首“sp_”。
註釋:假如儲存過程以sp_ 為字首開始命名那麼會執行的稍微的緩慢,這是因為SQL Server將首先查詢系統儲存過程。
[2] 表名就是儲存過程主要訪問的物件。
[3] 可選欄位名就是條件子句。比如: proc_UserInfoByUserIDSelect
[4] 最後的行為動詞就是儲存過程要執行的任務。
如果儲存過程返回一條記錄那麼字尾是:Select
如果儲存過程插入資料那麼字尾是:Insert
如果儲存過程更新資料那麼字尾是:Update
如果儲存過程有插入和更新那麼字尾是:Save
如果儲存過程刪除資料那麼字尾是:Delete
如果儲存過程更新表中的資料 (ie. drop and create) 那麼字尾是:Create
如果儲存過程返回輸出引數或0,那麼字尾是:Output
以上內容轉載自:http://www.cnblogs.com/shiyh/p/9366383.html
在此感謝該文章筆者,很符合我的命名格式化的習慣,感覺是很有用的,特此轉載一篇。
Time:2018-12-13