----------------------------------------------------------------------------------------ide
--有輸入參數的存儲過程--
create proc GetComment
(@commentid int)
as
select * from Comment where CommentID=@commentid
--有輸入與輸出參數的存儲過程--
create proc GetCommentCount
@newsid int,
@count int output
as
select @count=count(*) from Comment where NewsID=@newsid
--返回單個值的函數--
create function MyFunction
(@newsid int)
returns int
as
begin
declare @count int
select @count=count(*) from Comment where NewsID=@newsid
return @count
end
--調用方法--
declare @count int
exec @count=MyFunction 2
print @count
--返回值爲表的函數--
Create function GetFunctionTable
(@newsid int)
returns table
as
return
(select * from Comment where NewsID=@newsid)
--返回值爲表的函數的調用--
select * from GetFunctionTable(2)
-----------------------------------------------------------------------------------------------------------------------------------
SQLServer 存儲過程當中不拼接SQL字符串實現多條件查詢
之前拼接的寫法
set @sql=' select * from table where 1=1 '
if (@addDate is not null)
set @sql = @sql+' and addDate = '+ @addDate + ' '
if (@name <>'' and is not null)
set @sql = @sql+ ' and name = ' + @name + ' '
exec(@sql)
下面是 不採用拼接SQL字符串實現多條件查詢的解決方案
第一種寫法是 感受代碼有些冗餘
if (@addDate is not null) and (@name <> '')
select * from table where addDate = @addDate and name = @name
else if (@addDate is not null) and (@name ='')
select * from table where addDate = @addDate
else if(@addDate is null) and (@name <> '')
select * from table where and name = @name
else if(@addDate is null) and (@name = '')
select * from table
第二種寫法是
select * from table where (addDate = @addDate or @addDate is null) and (name = @name or @name = '')
第三種寫法是
SELECT * FROM table where
addDate = CASE @addDate IS NULL THEN addDate ELSE @addDate END,
name = CASE @name WHEN '' THEN name ELSE @name END
-----------------------------------------------------------------------------------------------------------------------------------
SQLSERVER存儲過程基本語法
1、定義變量
declare @user1 nvarchar(50) |
declare @user2 nvarchar(50) |
select @user2 = Name from ST_User where ID=1 |
declare @user3 nvarchar(50) |
update ST_User set @user3 = Name where ID=1 |
2、表、臨時表、表變量
[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 #DU_User1 (ID,Oid,[Login],Rtx, Name ,[ Password ],State) values (100,2, 'LS' , '0000' , '臨時' , '321' , '特殊' ); |
select * into #DU_User2 from ST_User where ID<8 |
select * from #DU_User2 where ID<3 union select * from #DU_User1 |
[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 |
alter table #t add [myid] int NOT NULL IDENTITY(1,1) |
alter table #t add [myid1] uniqueidentifier NOT NULL default (newid()) |
select IDENTITY( int ,1,1) as ID, Name ,[Login],[ Password ] into #t from ST_User |
select ( select SUM (1) from ST_User where ID<= a.ID) as myID,* from ST_User a order by myID |
insert into @t values (1, '1' ) |
insert into @t values (2, '2' ) |
3、循環
4、條件語句
declare @week nvarchar(3) |
5、遊標
declare @Login varchar (50) |
declare user_cur cursor for select ID,Oid,[Login] from ST_User |
fetch next from user_cur into @ID,@Oid,@Login |
6、觸發器
觸發器中的臨時表:
Inserted
存放進行insert和update 操做後的數據
Deleted
存放進行delete 和update操做前的數據
Create trigger User_OnUpdate |
declare @msg nvarchar(50) |
select @msg = N '姓名從「' + Deleted. Name + N '」修改成「' + Inserted. Name + '」' from Inserted,Deleted |
insert into [LOG](MSG) values (@msg) |
drop trigger User_OnUpdate |
7、存儲過程
execute PR_Sum 1,2,@mysum output |
execute @mysum2= PR_Sum2 1,2 |
8、自定義函數
函數的分類:
1)標量值函數
2)表值函數
a:內聯表值函數
b:多語句表值函數
3)系統函數
create function FUNC_Sum1 |
create function FUNC_UserTab_1 |
return ( select * from ST_User where ID<@myId) |
create function FUNC_UserTab_2 |
[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 where ID<@myId |
select * from dbo.FUNC_UserTab_1(15) |
set @s=dbo.FUNC_Sum1(100,50) |
談談自定義函數與存儲過程的區別:
1、自定義函數:
1. 能夠返回表變量
2. 限制頗多,包括
不能使用output參數;
不能用臨時表;
函數內部的操做不能影響到外部環境;
不能經過select返回結果集;
不能update,delete,數據庫表;
3. 必須return 一個標量值或表變量
自定義函數通常用在複用度高,功能簡單單一,爭對性強的地方。
2、存儲過程
1. 不能返回表變量
2. 限制少,能夠執行對數據庫表的操做,能夠返回數據集
3. 能夠return一個標量值,也能夠省略return
存儲過程通常用在實現複雜的功能,數據操縱方面。
-----------------------------------------------------------------------------------------------------------------------------------
SqlServer存儲過程--實例
實例1:只返回單一記錄集的存儲過程。
表銀行存款表(bankMoney)的內容以下
Id |
userID |
Sex |
Money |
001 |
Zhangsan |
男 |
30 |
002 |
Wangwu |
男 |
50 |
003 |
Zhangsan |
男 |
40 |
要求1:查詢表bankMoney的內容的存儲過程
create procedure sp_query_bankMoney
as
select * from bankMoney
go
exec sp_query_bankMoney
注* 在使用過程當中只須要把T-Sql中的SQL語句替換爲存儲過程名,就能夠了很方便吧!
實例2(向存儲過程當中傳遞參數):
加入一筆記錄到表bankMoney,並查詢此表中userID= Zhangsan的全部存款的總金額。
Create proc insert_bank @param1 char(10),@param2 varchar(20),@param3 varchar(20),@param4 int,@param5 int output
with encryption ---------加密
as
insert into bankMoney (id,userID,sex,Money)
Values(@param1,@param2,@param3, @param4)
select @param5=sum(Money) from bankMoney where userID='Zhangsan'
go
在SQL Server查詢分析器中執行該存儲過程的方法是:
declare @total_price int
exec insert_bank '004','Zhangsan','男',100,@total_price output
print '總餘額爲'+convert(varchar,@total_price)
go
在這裏再囉嗦一下存儲過程的3種傳回值(方便正在看這個例子的朋友不用再去查看語法內容):
1.以Return傳回整數
2.以output格式傳回參數
3.Recordset
傳回值的區別:
output和return均可在批次程式中用變量接收,而recordset則傳回到執行批次的客戶端中。
實例3:使用帶有複雜 SELECT 語句的簡單過程
下面的存儲過程從四個表的聯接中返回全部做者(提供了姓名)、出版的書籍以及出版社。該存儲過程不使用任何參數。
USE pubs
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'au_info_all' AND type = 'P')
DROP PROCEDURE au_info_all
GO
CREATE PROCEDURE au_info_all
AS
SELECT au_lname, au_fname, title, pub_name
FROM authors a INNER JOIN titleauthor ta
ON a.au_id = ta.au_id INNER JOIN titles t
ON t.title_id = ta.title_id INNER JOIN publishers p
ON t.pub_id = p.pub_id
GO
au_info_all 存儲過程能夠經過如下方法執行:
EXECUTE au_info_all
-- Or
EXEC au_info_all
若是該過程是批處理中的第一條語句,則可以使用:
au_info_all
實例4:使用帶有參數的簡單過程
CREATE PROCEDURE au_info
@lastname varchar(40),
@firstname varchar(20)
AS
SELECT au_lname, au_fname, title, pub_name
FROM authors a INNER JOIN titleauthor ta
ON a.au_id = ta.au_id INNER JOIN titles t
ON t.title_id = ta.title_id INNER JOIN publishers p
ON t.pub_id = p.pub_id
WHERE au_fname = @firstname
AND au_lname = @lastname
GO
au_info 存儲過程能夠經過如下方法執行:
EXECUTE au_info 'Dull', 'Ann'
-- Or
EXECUTE au_info @lastname = 'Dull', @firstname = 'Ann'
-- Or
EXECUTE au_info @firstname = 'Ann', @lastname = 'Dull'
-- Or
EXEC au_info 'Dull', 'Ann'
-- Or
EXEC au_info @lastname = 'Dull', @firstname = 'Ann'
-- Or
EXEC au_info @firstname = 'Ann', @lastname = 'Dull'
若是該過程是批處理中的第一條語句,則可以使用:
au_info 'Dull', 'Ann'
-- Or
au_info @lastname = 'Dull', @firstname = 'Ann'
-- Or
au_info @firstname = 'Ann', @lastname = 'Dull'
實例5:使用帶有通配符參數的簡單過程 CREATE PROCEDURE au_info2 @lastname varchar(30) = 'D%', @firstname varchar(18) = '%' AS SELECT au_lname, au_fname, title, pub_name FROM authors a INNER JOIN titleauthor ta ON a.au_id = ta.au_id INNER JOIN titles t ON t.title_id = ta.title_id INNER JOIN publishers p ON t.pub_id = p.pub_id WHERE au_fname LIKE @firstname AND au_lname LIKE @lastname GO au_info2 存儲過程能夠用多種組合執行。下面只列出了部分組合: EXECUTE au_info2 -- Or EXECUTE au_info2 'Wh%' -- Or EXECUTE au_info2 @firstname = 'A%' -- Or EXECUTE au_info2 '[CK]ars[OE]n' -- Or EXECUTE au_info2 'Hunter', 'Sheryl' -- Or EXECUTE au_info2 'H%', 'S%' = 'proc2' 實例6:if...else |
存儲過程,其中@case做爲執行update的選擇依據,用if...else實現執行時根據傳入的參數執行不一樣的修改.
--下面是if……else的存儲過程:
if exists (select 1 from sysobjects where name = 'Student' and type ='u' )
drop table Student
go
if exists (select 1 from sysobjects where name = 'spUpdateStudent' and type ='p' )
drop proc spUpdateStudent
go
create table Student
(
fName nvarchar (10),
fAge
smallint ,fDiqu varchar (50),fTel int )goinsert into Student values ('X.X.Y' , 28, 'Tesing' , 888888)gocreate proc spUpdateStudent(@fCase int ,@fName nvarchar (10),@fAge smallint ,@fDiqu varchar (50),@fTel int )as update Studentset fAge = @fAge, -- 傳 1,2,3 都要更新 fAge 不須要用 case fDiqu = (case when @fCase = 2 or @fCase = 3 then @fDiqu else fDiqu end ),fTel = (case when @fCase = 3 then @fTel else fTel end )where fName = @fNameselect * from Studentgo-- 只改 Age exec spUpdateStudent@fCase = 1,@fName = N'X.X.Y' ,@fAge = 80,@fDiqu = N'Update' ,@fTel = 1010101-- 改 Age 和 Diqu exec spUpdateStudent@fCase = 2,@fName = N'X.X.Y' ,@fAge = 80,@fDiqu = N'Update' ,@fTel = 1010101-- 全改 exec spUpdateStudent@fCase = 3,@fName = N'X.X.Y' ,@fAge = 80,@fDiqu = N'Update' ,@fTel = 1010101