SQLSERVER存储过程基本语法
发布时间:2020-12-12 13:53:16 所属栏目:MsSql教程 来源:网络整理
导读:一、定义变量 -- 简单赋值 declare @a int set @a = 5 print @a 使用select语句赋值 @user1 nvarchar ( 50 ) select @user1 = ' 张三 ' @user1 @user2 @user2 = Name from ST_User where ID 1 @user2 使用update语句赋值 @user3 update ST_User @user3 @user
一、定义变量 --简单赋值 declare @a int set @a=5 print @a 使用select语句赋值 @user1 nvarchar(50) select @user1='张三' @user1 @user2 @user2 = Name from ST_User where ID1 @user2 使用update语句赋值 @user3 update ST_User @user3 @user3 二、表、临时表、表变量 创建临时表1 create table #DU_User1 ( [ID] int] NOT NULL,Oid] Loginnvarchar](50) Rtx4) Name5) Password](max) State8) NULL ); 向临时表1插入一条记录 insert into #DU_User1 (ID,Oid,],Rtx,Name,State) values (100,2,0); line-height:1.5!important">LS',0); line-height:1.5!important">0000临时321特殊'); 从ST_User查询数据,填充至新生成的临时表 select * into #DU_User2 <8 查询并联合两临时表 from #DU_User2 3 union from #DU_User1 删除两临时表 drop table #DU_User1 table #DU_User2 创建临时表 CREATE TABLE #t ( 将查询结果集(多条数据)插入临时表 into #t from ST_User 不能这样插入 --select * into #t from dbo.ST_User 添加一列,为int型自增长子段 alter table #t add myid] int NULL IDENTITY(1,0); line-height:1.5!important">1) 添加一列,默认填充全球唯一标识 myid1uniqueidentifier NULL default(newid()) from #t table #t 给查询结果集增加自增长列 无主键时: select IDENTITY(int,0); line-height:1.5!important">1)as ID,255); line-height:1.5!important">from #t 有主键时: select (SUM(1) <= a.ID) as myID,255); line-height:1.5!important">from ST_User a order by myID 定义表变量 @t table ( id not null,msg 50) null ) into values(1') 2from @t 三、循环 while循环计算1到100的和 @sum @sum0 while <=100 begin +=@a +=end @sum 四、条件语句 if,else条件分支 if(1+2) print 对else 错end when then条件分支 @today @week 3) @today3 @weekcase when 1 then 星期一' 2 星期二星期三4 星期四5 星期五6 星期六7 星期日else 值错误@week 五、游标 @ID @Oid @Login varchar(50) 定义一个游标 declare user_cur cursor for select ID,128); line-height:1.5!important">打开游标 open user_cur @@fetch_statusbegin 读取游标 fetch next from user_cur @ID,@Oid,0); line-height:1.5!important">@Login @ID print @Login close user_cur 摧毁游标 deallocate user_cur 六、触发器 触发器中的临时表: Inserted 存放进行insert和update 操作后的数据 Deleted 存放进行delete 和update操作前的数据 创建触发器 Create trigger User_OnUpdate On ST_User Update As @msg 50) @msg记录修改情况 @msg = N姓名从“' + Deleted.Name + N”修改为“+ Inserted.Name + ”' from Inserted,Deleted 插入日志表 into LOG](MSG)values(@msg) 删除触发器 trigger User_OnUpdate 七、存储过程 创建带output参数的存储过程 PROCEDURE PR_Sum int,@b int output AS BEGIN =+@b END 创建Return返回值存储过程 PROCEDURE PR_Sum2 Return END 执行存储过程获取output型返回值 @mysum execute PR_Sum @mysum output @mysum 执行存储过程获取Return型返回值 @mysum2 execute @mysum2= PR_Sum2 2 @mysum2 八、自定义函数 函数的分类: 1)标量值函数 2)表值函数 a:内联表值函数 b:多语句表值函数 3)系统函数 新建标量值函数 function FUNC_Sum1 ( int ) returns as return 新建内联表值函数 function FUNC_UserTab_1 ( @myId table return (<@myId) 新建多语句表值函数 function FUNC_UserTab_2 ( table ( NULL ) @myId return 调用表值函数 from dbo.FUNC_UserTab_1(15) 调用标量值函数 @s @s=dbo.FUNC_Sum1(@s 删除标量值函数 function FUNC_Sum1 谈谈自定义函数与存储过程的区别: 一、自定义函数: 1. 可以返回表变量 2. 限制颇多,包括 不能使用output参数; 不能用临时表; 函数内部的操作不能影响到外部环境; 不能通过select返回结果集; 不能update,delete,数据库表; 3. 必须return 一个标量值或表变量 自定义函数一般用在复用度高,功能简单单一,争对性强的地方。 二、存储过程 1. 不能返回表变量 2. 限制少,可以执行对数据库表的操作,可以返回数据集 3. 可以return一个标量值,也可以省略return 存储过程一般用在实现复杂的功能,数据操纵方面。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |