表连接、TSQL基本编程、存储过程

表连接:
将多个表的数据一次性查询出来,共同显示

子查询方式:

select (select 列 from 名where 名.列 = 名.列)列 from 名

笛卡尔积:

select 列,列from 名,名 where 名.列 =名.列

join on:

seect 列,列 from 名 join 名 on 名.列=名.列
inner join on ;
left join on;左表中右表没有的显示完整
right join on ;右表中左表没有的显示完整

注意:必须添加where条件或on后面跟条件,两个表的关系列

纵连接:
union

注意:纵连接的表,列数必须一样,对应列的数据类型要一样

---------------------------------------------------------------

TSQL基本编程:
定义变量:
declare @变量名 类型;


赋值:
set @变量名=值;
select @变量名 =值

打印在消息框中:
print @变量名

映射在结果集中:
select @ 变量名

分支语句写法:
if 条件
begin
select @变量名
end
else
begin
select @变量名
end

select @变量名

循环语句写法:

初始条件
while 循环条件
begin
循环体
select +=@变量名 状态改变
end

----------------------------------------------------------

存储过程:
就是函数


如何定义:
create proc 名
参数
as
declare @变量名 数据类型
select @变量名= 变量名运算
return @变量名

如何使用:exec --执行函数

declare @变量名 数据类型
exec 函数名 @变量名

select @变量名

保存在哪:数据库--可执行性--存储过程

如何将数据库查出来的数据存入变量中,并在存储过程中使用

select *from 名
declare @变量名
select @变量名=max(列)from名

exec @变量名=函数 @变量名

原文地址:https://www.cnblogs.com/songfengyao/p/5581220.html