微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SQLSERVER存储过程基本语法

一、定义变量
--简单赋值
declare @a int
set @a=5
print @a
  
--使用select语句赋值
@user1 nvarchar(50)
select @user1='张三'
print @user1
@user2 nvarchar(50)
@user2 = Name from ST_User where ID=1
print @user2
--使用update语句赋值
@user3 nvarchar(50)
update @user3 = ID=1
print @user3

 

二、表、临时表、表变量

--创建临时表1
create table #DU_User1
(
     [ID] [intNOT NULL,
[Oid] [] sql color1" style="white-space:pre-wrap; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; margin:0px!important; outline:rgb(0,
[Login] [nvarchar](50) [Rtx] [nvarchar](4) [Name] [nvarchar](5) Password] [nvarchar](max) [State] [nvarchar](8) NULL
);
--向临时表1插入一条记录
insert into #DU_User1 (ID,Oid,[Login],Rtx,sql keyword" style="white-space:pre-wrap; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; margin:0px!important; outline:rgb(0,[],State) values (100,2,'LS''0000''临时''321''特殊'  
--从ST_User查询数据,填充至新生成的临时表
* #DU_User2 ID<8
--查询并联合两临时表
ID<3 union #DU_User1
--删除两临时表
drop #DU_User2
 
--创建临时表
CREATE TABLE #t
    )
--将查询结果集(多条数据)插入临时表
#t ST_User
--不能这样插入
--select * into #t from dbo.ST_User
--添加一列,为int型自增长子段
alter add [myid] int NULL IDENTITY(1,1)
--添加一列,认填充全球唯一标识
[myid1] uniqueidentifier NULL default(newid())
#t
#t
--给查询结果集增加自增长列
--无主键时:
IDENTITY(sql keyword" style="white-space:pre-wrap; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; margin:0px!important; outline:rgb(0,1,1)as ID,monospace!important; min-height:auto!important">] --有主键时:
(select SUM(1) ID<= a.ID) myID,* ST_User a order by myID
--定义表变量
@t table
id not nullmsg nvarchar(50) null
)
values(1,monospace!important; min-height:auto!important; color:blue!important">'1'(2,monospace!important; min-height:auto!important; color:blue!important">'2'@t

 三、循环

--while循环计算1到100的和
@sum int
@a=1
sum=0
while @a<=100
begin
    +=@a
@a+=1
end
print @sum

四、条件语句

--if,else条件分支
if(1+1=2)
print '对'
end
else
'错'
--when then条件分支
@today @week nvarchar(3)
@today=3
@week=case
when @today=1 then '星期一'
@today=2 '星期二'
@today=3 '星期三'
@today=4 '星期四'
@today=5 '星期五'
@today=6 '星期六'
@today=7 '星期日'
else '值错误'
print @week

五、游标

@ID @Oid @Login varchar(50)
--定义一个游标
user_cur cursor for sql keyword" style="white-space:pre-wrap; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; margin:0px!important; outline:rgb(0,[Login] --打开游标
open user_cur
while @@fetch_status=0
begin
--读取游标
fetch next @ID,@Oid,@Login
print @ID
    --print @Login
close --摧毁游标
deallocate user_cur

六、触发器

  触发器中的临时表:

  Inserted 
  存放进行insert和update 操作后的数据 
  Deleted 
  存放进行delete 和update操作前的数据

--创建触发器
Create trigger User_OnUpdate 
On ST_User 
Update 
As 
@msg nvarchar(50)
--@msg记录修改情况
@msg = N'姓名从“' + Deleted.Name + N'”修改为“' + Inserted.+ '”' Inserted,Deleted
--插入日志表
[LOG](MSG)(@msg)
      
--删除触发器
User_OnUpdate

七、存储过程

--创建带output参数的存储过程
PROCEDURE PR_Sum
@b int output
AS
BEGIN
=@a+@b
END
--创建Return返回值存储过程
PR_Sum2
AS
BEGIN
Return @a+@b
END
--执行存储过程获取output型返回值
@mysum execute PR_Sum 1,@mysum output
print @mysum
--执行存储过程获取Return型返回值
@mysum2 @mysum2= PR_Sum2 1,2
print @mysum2
 

  

八、自定义函数

  函数分类

    1)标量值函数

    2)表值函数

        a:内联表值函数

        b:多语句表值函数

    3)系统函数

  

--新建标量值函数
function FUNC_Sum1
returns as
return @a+@b
--新建内联表值函数
FUNC_UserTab_1
@myId as
ID<@myId)
--新建多语句表值函数
FUNC_UserTab_2
(
returns table
NULL
ID<@myId
return
--调用表值函数
dbo.FUNC_UserTab_1(15)
--调用标量值函数
@s @s=dbo.FUNC_Sum1(100,50)
print @s
--删除标量值函数
FUNC_Sum1

谈谈自定义函数与存储过程的区别:

一、自定义函数

  1. 可以返回表变量

  2. 限制颇多,包括

    不能使用output参数;

    不能用临时表;

    函数内部的操作不能影响到外部环境;

    不能通过select返回结果集;

    不能update,delete,数据库表;

  3. 必须return 一个标量值或表变量

  自定义函数一般用在复用度高,功能简单单一,争对性强的地方。

二、存储过程

  1. 不能返回表变量

  2. 限制少,可以执行对数据库表的操作,可以返回数据集

  3. 可以return一个标量值,也可以省略return

   存储过程一般用在实现复杂的功能,数据操纵方面。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐