SQL Server(二):T-SQL语言概述
1、T_SQL语言属于SQL语言中的一种:
SQL结构化查询语言
所有的数据库编程语言均对ANSI SQL向下兼容,如MS SQL Server的SQL语言、Oracle的PL/SQL语言
2、SQL语言主要包括三类:
1)DCL——数据控制语言:主要用于控制权限
Grant:赋权
Deny:拒绝
Revoke:恢复初始默认
2)DDL——数据定义语言:主要用于定义数据库对象
Create:创建数据库对象
Alter:修改数据库对象的定义
Drop:删除数据库对象
3)DML——数据操纵语言:主要用于操纵数据
Insert:添加数据
Update:修改数据
Delete:删除数据
Select:查询数据(有一些资料中将Select单独分类为DQL数据查询语言)
3、变量
在SQL Server中定义变量,变量名前加@(局部变量)或@@(全局变量)
使用Declare声明变量,使用Set或Select语句为变量赋值。如:
 
4、函数
在SQL Server中函数可以分为以下三类:
1)标量函数:确定的参数,一个返回值,如常规的函数均属于标量函数。
        
 declare @value int
declare @value int
 set @value=100
set @value=100
 declare @valueString varchar(10)
declare @valueString varchar(10)
 set @valueString=Convert(varchar(10),@value)
set @valueString=Convert(varchar(10),@value)
 print('Value is '+@valueString)
print('Value is '+@valueString)
2)聚焦函数:参数为一个集合(表中的列),返回为一个值,如数学上的统计函数均属于标量函数。
 Select sum(UnitPrice) as [SUM] --取所有单价的和
Select sum(UnitPrice) as [SUM] --取所有单价的和
 from Products
from Products

 Select avg(UnitPrice) as [AVG] --取所有单价的平均值
Select avg(UnitPrice) as [AVG] --取所有单价的平均值
 from Products
from Products

 Select max(UnitPrice) as [Max] --取所有单价的最大值
Select max(UnitPrice) as [Max] --取所有单价的最大值
 from Products
from Products

 Select min(UnitPrice) as [Min] --取所有单价的最小值
Select min(UnitPrice) as [Min] --取所有单价的最小值
 from Products
from Products

 Select count(Region) as [Count] --取所有Region不为空的行数
Select count(Region) as [Count] --取所有Region不为空的行数
 from Employees
from Employees

 Select count(*) as [Count] --取员工表所有的行数
Select count(*) as [Count] --取员工表所有的行数
 from Employees
from Employees
3)行集函数:参数为确定的参数,返回为一个“结果集”。
 select * from
select * from 
 OpenQuery(
OpenQuery(
 OracleSvr,  --打开一个链接服务器
    OracleSvr,  --打开一个链接服务器
 'SELECT ENAME, EMPNO FROM SCOTT.EMP'  --在链接服务器上执行查询语句
    'SELECT ENAME, EMPNO FROM SCOTT.EMP'  --在链接服务器上执行查询语句
 )     --将OpenQuery返回的结果集作为查询的源
)     --将OpenQuery返回的结果集作为查询的源
 
5、语句
1)Begin...End:相当于C、Java、C#中的一对大括号,表示范围限定,没有具体含义,如果其中只有一条语句则可以省略。
2)While:循环语句
 --计算1+2+3+
--计算1+2+3+ +100
+100
 declare @i int
declare @i int
 declare @sum int
declare @sum int
 set @i=1
set @i=1
 set @sum=0
set @sum=0
 while @i<=100
while @i<=100
 begin
    begin
 set @sum=@sum+@i
        set @sum=@sum+@i
 set @i=@i+1
        set @i=@i+1
 end
    end
 Print(@sum)
Print(@sum)
3)If...Else:条件语句
 declare @rowCount int
declare @rowCount int
 select @rowCount=count(*) from SomeTable
select @rowCount=count(*) from SomeTable
 if @rowCount=0
if @rowCount=0
 begin
    begin
 Print('没有数据')
        Print('没有数据')
 end
    end
 else if @rowCount>0 and @rowCount<100
else if @rowCount>0 and @rowCount<100
 begin
    begin
 Print('100条以内记录')
        Print('100条以内记录')
 end
    end
 else
else
 begin
    begin
 Print('100条以上记录')
        Print('100条以上记录')
 end
    end
4)Case语句:属于行级语句(前三种属于语句级),相当于一个函数的作用
 Select ProductID,ProductName,UnitPrice,
Select ProductID,ProductName,UnitPrice,
 Level=
    Level=
 case
        case
 when UnitPrice<=30 then 'Low Price'
            when UnitPrice<=30 then 'Low Price'
 when UnitPrice>30 and UnitPrice<=90 then 'Mid Price'
            when UnitPrice>30 and UnitPrice<=90 then 'Mid Price'
 else 'High Price'
            else 'High Price'
 end
        end
 from Products
from Products
SQL结构化查询语言
所有的数据库编程语言均对ANSI SQL向下兼容,如MS SQL Server的SQL语言、Oracle的PL/SQL语言
2、SQL语言主要包括三类:
1)DCL——数据控制语言:主要用于控制权限
Grant:赋权
Deny:拒绝
Revoke:恢复初始默认
2)DDL——数据定义语言:主要用于定义数据库对象
Create:创建数据库对象
Alter:修改数据库对象的定义
Drop:删除数据库对象
3)DML——数据操纵语言:主要用于操纵数据
Insert:添加数据
Update:修改数据
Delete:删除数据
Select:查询数据(有一些资料中将Select单独分类为DQL数据查询语言)
3、变量
在SQL Server中定义变量,变量名前加@(局部变量)或@@(全局变量)
使用Declare声明变量,使用Set或Select语句为变量赋值。如:
 1 declare @i int
declare @i int
2 set @i=100
set @i=100
3
4 declare @sum int
declare @sum int
5 select @sum=sum(UnitPrice)
select @sum=sum(UnitPrice)
6 from Products
from Products
7
8 declare @price int
declare @price int
9 select @price=UnitPrice
select @price=UnitPrice
10 from Products
from Products
11 where ProductID=1
where ProductID=1
12
13 declare @singlePrice int
declare @singlePrice int
14 select @singlePrice=UnitPrice
select @singlePrice=UnitPrice
15 from Products
from Products
16
17 declare @sumPrice int
declare @sumPrice int
18 set @sumPrice=0
set @sumPrice=0
19 select @wumPrice=@sumPrice+UnitPrice
select @wumPrice=@sumPrice+UnitPrice
20 from Products
from Products
 declare @i int
declare @i int2
 set @i=100
set @i=1003

4
 declare @sum int
declare @sum int5
 select @sum=sum(UnitPrice)
select @sum=sum(UnitPrice)6
 from Products
from Products7

8
 declare @price int
declare @price int9
 select @price=UnitPrice
select @price=UnitPrice10
 from Products
from Products11
 where ProductID=1
where ProductID=112

13
 declare @singlePrice int
declare @singlePrice int14
 select @singlePrice=UnitPrice
select @singlePrice=UnitPrice15
 from Products
from Products16

17
 declare @sumPrice int
declare @sumPrice int18
 set @sumPrice=0
set @sumPrice=019
 select @wumPrice=@sumPrice+UnitPrice
select @wumPrice=@sumPrice+UnitPrice20
 from Products
from Products4、函数
在SQL Server中函数可以分为以下三类:
1)标量函数:确定的参数,一个返回值,如常规的函数均属于标量函数。
 declare @value int
declare @value int set @value=100
set @value=100 declare @valueString varchar(10)
declare @valueString varchar(10) set @valueString=Convert(varchar(10),@value)
set @valueString=Convert(varchar(10),@value) print('Value is '+@valueString)
print('Value is '+@valueString)2)聚焦函数:参数为一个集合(表中的列),返回为一个值,如数学上的统计函数均属于标量函数。
 Select sum(UnitPrice) as [SUM] --取所有单价的和
Select sum(UnitPrice) as [SUM] --取所有单价的和 from Products
from Products
 Select avg(UnitPrice) as [AVG] --取所有单价的平均值
Select avg(UnitPrice) as [AVG] --取所有单价的平均值 from Products
from Products
 Select max(UnitPrice) as [Max] --取所有单价的最大值
Select max(UnitPrice) as [Max] --取所有单价的最大值 from Products
from Products
 Select min(UnitPrice) as [Min] --取所有单价的最小值
Select min(UnitPrice) as [Min] --取所有单价的最小值 from Products
from Products
 Select count(Region) as [Count] --取所有Region不为空的行数
Select count(Region) as [Count] --取所有Region不为空的行数 from Employees
from Employees
 Select count(*) as [Count] --取员工表所有的行数
Select count(*) as [Count] --取员工表所有的行数 from Employees
from Employees3)行集函数:参数为确定的参数,返回为一个“结果集”。
 select * from
select * from  OpenQuery(
OpenQuery( OracleSvr,  --打开一个链接服务器
    OracleSvr,  --打开一个链接服务器 'SELECT ENAME, EMPNO FROM SCOTT.EMP'  --在链接服务器上执行查询语句
    'SELECT ENAME, EMPNO FROM SCOTT.EMP'  --在链接服务器上执行查询语句 )     --将OpenQuery返回的结果集作为查询的源
)     --将OpenQuery返回的结果集作为查询的源
5、语句
1)Begin...End:相当于C、Java、C#中的一对大括号,表示范围限定,没有具体含义,如果其中只有一条语句则可以省略。
2)While:循环语句
 --计算1+2+3+
--计算1+2+3+ +100
+100 declare @i int
declare @i int declare @sum int
declare @sum int set @i=1
set @i=1 set @sum=0
set @sum=0 while @i<=100
while @i<=100 begin
    begin set @sum=@sum+@i
        set @sum=@sum+@i set @i=@i+1
        set @i=@i+1 end
    end Print(@sum)
Print(@sum)3)If...Else:条件语句
 declare @rowCount int
declare @rowCount int select @rowCount=count(*) from SomeTable
select @rowCount=count(*) from SomeTable if @rowCount=0
if @rowCount=0 begin
    begin Print('没有数据')
        Print('没有数据') end
    end else if @rowCount>0 and @rowCount<100
else if @rowCount>0 and @rowCount<100 begin
    begin Print('100条以内记录')
        Print('100条以内记录') end
    end else
else begin
    begin Print('100条以上记录')
        Print('100条以上记录') end
    end4)Case语句:属于行级语句(前三种属于语句级),相当于一个函数的作用
 Select ProductID,ProductName,UnitPrice,
Select ProductID,ProductName,UnitPrice, Level=
    Level= case
        case when UnitPrice<=30 then 'Low Price'
            when UnitPrice<=30 then 'Low Price' when UnitPrice>30 and UnitPrice<=90 then 'Mid Price'
            when UnitPrice>30 and UnitPrice<=90 then 'Mid Price' else 'High Price'
            else 'High Price' end
        end from Products
from Products
    本文版权归作者所有,未经同意,请勿用作商业用途。
 
                    
                     
                    
                 
                    
                 
 SQL Server教学第二讲,T_SQL基础。
包含的内容有:T_SQL基础、语句分类、变量、函数、条件语句、循环语句等。
SQL Server教学第二讲,T_SQL基础。
包含的内容有:T_SQL基础、语句分类、变量、函数、条件语句、循环语句等。
     
                
            
        