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
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
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’
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 ) go
insert into Student values (‘X.X.Y’ , 28, ‘Tesing’ , 888888) go
create proc spUpdateStudent ( @fCase int , @fName nvarchar (10), @fAge smallint , @fDiqu varchar (50), @fTel int ) as update Student set 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 = @fName select * from Student go