当前位置:Gxlcms > 数据库问题 > SQL Server(六)——索引、视图和SQL编程

SQL Server(六)——索引、视图和SQL编程

时间:2021-07-01 10:21:17 帮助过:17人阅读

1.索引

 

添加索引,设计界面,在任何一列前右键--索引/键--点击进入添加某一列为索引

 

2.视图

 

视图就是我们查询出来的虚拟表

 

创建视图:create view 视图名          

 

              as          

 

              SQL查询语句,分组,排序,in 等都不能写

 

视图的用法: select * from 视图名

 

3.SQL编程

 

(1)定义变量:declare @变量名 数据类型    

 

         例:declare @a int

 

(2)变量赋值:set @变量名 = 值     

 

         例:set @a=10

 

set @a = 10  --赋值,不打印

 

select @a;  --打印在结果集中

 

print @a;   --打印在消息框中

 

declare @name varchar(20) 

set @name=‘宝马‘ 

select * from car where Name like ‘%‘+@name+‘%‘


if ... else 的用法,if后面没有小括号,花括号用begin end 替代

if 判断条件

begin   

要执行的语句

end

else

begin   

要执行的语句

end

 

case用法

 

declare @ccname varchar(20) 

set @ccname = ‘宝马‘ 

select * from Car where Name like

case --switch...case的开头 

when @ccname=‘宝马‘ then ‘%宝马%‘ 

when @ccname=‘奥迪‘ then ‘%奥迪%‘ else ‘%‘ 

end --switch...case的结尾

 

SQL Server(六)——索引、视图和SQL编程

标签:serve   case用法   点击   sele   虚拟   declare   查询   判断   print   

人气教程排行