SQL的变量分为:局部变量(一个@定义)和全局变量(两个@定义)
--先声明 再赋值 再使用
--数据库声明变量 declare @名字 类型 如果想赋值 可以直接赋值 或者是通过 set赋值
declare @num int =10
select @num
--print @num
declare @name nvarchar(10)
set @name='马伦';
print @name
--全局变量
--查询的是刚刚的执行的错误消息值
select @@ERROR
--查询服务器名称
print '服务器名称: '+@@SERVERNAME
--查询数据库版本信息
select @@VERSION
--实例演示
declare @avg int =0
set @avg=(select AVG(tMath) from TblScore)
if(@avg>60)
begin
select top 3 * from TblScore order by tMath desc
end
else
begin
select top 3 * from TblScore order by tMath asc
end