sql进阶

时间:2023-03-08 15:53:31
--1.变量的声明和赋值
declare @Start DateTime,
@End DateTime
set @Start = '1999-9-9'
set @End = '2000-1-1'
select @Start,@End
--2.表变量
declare @tb1 table
(
ID int,
Name varchar(20),
Age int
)
insert into @tb1 values(1,'小明',7);
insert into @tb1 values(2,'小红',8);
select * from @tb1
--3.临时表--创建
if OBJECT_ID('tempdb..#tmp1') is not null drop table #tmp1
Create table #tmp1
(
ID int,
Name varchar(20)
)
insert into #tmp1 values(1,'Ami')
insert into #tmp1 values(2,'DaMing')
select * from #tmp1
--4.临时表--select into
if OBJECT_ID('tempdb..#tmp2') is not null drop table #tmp2
select * into #tmp2
from
(
select * from @tb1
) as tmp
select * from #tmp2
--5.isnull()函数
select isnull(null,0)
--6.if else
if(@Start > '2008-1-1')
begin
select '大于'
end
else select '小于'
--7.case xxx when
declare @sex int
set @sex =1
select
case @sex
when 1 Then '男'
when 0 Then '女'
end
--8.case when
select
case @sex
when 1 then '男'
when 0 then '女'
end
--9.union all 和 left join
--left join 还是比较熟悉的,是左连接,一个表从右边过来接上左边的表。
--union all 就算是【向下连接】了
select '小明'
union all
select '小红'
union all
select '小刚'
--10.调用另一个存储过程
--exec sp_StorageSum @Start,@End,'全部'