前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SQLserver数据库之存储过程、游标等操作(3)

SQLserver数据库之存储过程、游标等操作(3)

作者头像
闻语博客
发布2021-01-21 17:32:27
8170
发布2021-01-21 17:32:27
举报
文章被收录于专栏:闻语博客闻语博客

1.赋值

代码语言:javascript
复制
--简单赋值
declare @a int
set @a=5
print @a

--使用select语句赋值
declare @b nvarchar(50)
select @b='张三'
print @b

declare @b1 nvarchar(50)
select @b1=EmpName from student where EmpId=18
print @b1

--使用update语句赋值
declare @b2 nvarchar(50)
update student set @b2=EmpName where EmpId=18
print @b2

2.表、临时表、表变量

代码语言:javascript
复制
--删除临时表
drop table #DB_U;;
drop table #DB_U2;
drop table #DB_U3;

--创建临时表
create table #DB_U(
[id][int]not null,
[name][nvarchar](5)not null
);

--向临时表插入信息
insert into #DB_U(id,name) values(1,'tom');

--从#DB_U表查询数据填充到新生成的#DB_U2表
select * into #DB_U2 from #DB_U where id<8;

--两临时表联合查询
select * from #DB_U2 where id<3 union select * from #DB_U;

--将查询表的数据插入到临时表中
insert into #DB_U select * from student;

--添加一个新列myid,自增长字段
alter table #DB_U add [myid] int not null identity(1,1);

--添加一个新列myid1,默认全球唯一标识
alter table #DB_U add[myid1] uniqueidentifier not null default(newid());

--给查询结果集增加自增长列(无主键时)
select IDENTITY(int,1,1) as id into #DB_U3 from student;
--给查询结果集增加自增长列(有主键时)
select(select SUM(1) from student where EmpId<=a.id) as myid from #DB_U 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

select * from #DB_U;
select * from #DB_U2;
select * from #DB_U3;

3.循环

代码语言:javascript
复制
--while循环
declare @a int
declare @sum int
set @a=1
set @sum =0
while @a<=100
begin
set @sum +=@a
set @a+=1
end
print @sum

4.条件语句

代码语言:javascript
复制
--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

5.游标

代码语言:javascript
复制
declare @EmpId int
declare @EmpName varchar (50)

--定义一个游标
declare user_cur cursor for select EmpId,EmpName from student

--打开游标
open user_cur
while @@fetch_status=0
begin
--读取游标
fetch next from user_cur into @EmpId,@EmpName
print @EmpId
end
--关闭游标
close user_cur
--摧毁游标
deallocate user_cur

6.触发器

代码语言:javascript
复制
--创建触发器
--在student上创建<strong>INSERT触发器</strong>stu_insert,
--要求在student表中插入记录时(要求每次只能插入一条记录),
--这个触发器都将更新Users表中的UName列。并测试触发器stu_insert。
create trigger stu_insert
on student
for insert
as
update Users set UName=UName+1
where Uid=(select Uid from inserted)

--插入日志表
insert into student values ('嘻嘻嘻')

--删除触发器
drop trigger stu_insert

7.创建带output参数的存储过程

代码语言:javascript
复制
--创建带output参数的存储过程
CREATE PROCEDURE PR_Sum
@a int,
@b int,
@sum int output
AS
BEGIN
set @sum =@a+@b
END

--创建Return返回值存储过程
CREATE PROCEDURE PR_Sum2
@a int ,
@b int
AS
BEGIN
Return @a+@b
END

--执行存储过程获取output型返回值
declare @mysum int
execute PR_Sum 21,22,@mysum output
print @mysum

--执行存储过程获取Return型返回值
declare @mysum2 int
execute @mysum2= PR_Sum2 1,2
print @mysum2

8.自定义函数

代码语言:javascript
复制
--新建标量值函数
create function FUNC_Sum1(
@a int,
@b int
)
returns int
as
begin
return @a+@b
end

--调用标量值函数
declare @s int
set @s=dbo.FUNC_Sum1(100,50)
print @s

--删除标量值函数
drop function FUNC_Sum1
--------------------------------------------
--新建内联表值函数
create function FUNC_UserTab_1(
@myId int
)
returns table
as
return ( select * from student where EmpId<@myId)

--调用表值函数
select * from dbo.FUNC_UserTab_1(15)
--------------------------------------------
--新建多语句表值函数
create function FUNC_UserTab_2(
@myId int
)
returns @t table(
[EmpId] [int] NOT NULL ,
[EmpName] [nvarchar](8) NOT NULL
)
as
begin
insert into @t select * from student where EmpId<@myId
return
end

--调用多语句表值函数
select * from dbo.FUNC_UserTab_2(15)

--更改字段类型长度
ALTER TABLE 表名 ALTER COLUMN 列名 VARCHAR(255)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020 年 12 月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档