数据库复习总结(17)-T-Sql编程

时间:2023-03-09 03:36:18
数据库复习总结(17)-T-Sql编程

T-SQL(SQL SERVER)

百度百科:(即 Transact-SQL,是 SQL 在 Microsoft SQL Server 上的增强版,它是用来让应用程序与 SQL Server 沟通的主要语言。T-SQL 提供标准 SQL 的 DDL 和 DML 功能,加上延伸的函数、系统预存程序以及程式设计结构(例如 IF 和 WHILE)让程式设计更有弹性)

(1)变量
                 -》声明:declare 变量名 类型--变量名要求以 @ 开头
                 -》设置:set/select 变量名=值
                 -》输出:print/select 变量名
                 -》全局变量:使用双 @ 符号,系统内置变量
                       @@version --数据库版本
                       @@identity --进行插入后调用,返回最亲的标识值
                       @@servername --服务器名称
                       @@error --返回执行的上一个 Transact-SQL 语句的错误号,如果没有错误则返回0
                       @@rowcount --返回受上一语句影响的行数
(2)选择语句if

数据库复习总结(17)-T-Sql编程

(3)循环语句while(只有这一个)

数据库复习总结(17)-T-Sql编程
(4)异常处理语句
                begin try...end try
                begin catch...end catch

--1、变量
declare @name nvarchar(10)--声明
set @name='武大'--赋值
print @name--输出
--选择语句
declare @id int
set @id=10
if @id>5
begin
--满足条件时,执行如下代码
print 'ok'
end
else
begin
--不满足条件时,执行如下代码
print 'no'
end --循环
declare @id int
set @id=1
while @id<10
begin
print @id
set @id=@id+1
end --输出1-10之间的所有偶数
declare @num int
set @num=1
while @num<11
begin
if @num%2=0
begin
print @num
end
set @num=@num+1
end --异常处理
begin try
delete from ClassInfo
end try
begin catch
print @@error
end catch