--声明两个变量,使用select关键字给变量赋值,值来源自数据表
declare @Name nvarchar(50)
declare @email nvarchar(50)
select @Name = (select clientid from client where clientid = '1'),@email = (select email from client where clientid = '1')
print @Name
print @email
--查询出Users表中username列包含"["的记录,使用2种方法
select * from Users where username like '%\[%' escape '\'
select * from Users where username like '%[[]%'
--使用IF Else 判断两个数的大小
declare @i1 int
declare @i2 int
select @i1 = 100 ,@i2 = 20
if(@i1>@i2)
begin
print convert(varchar(50),@i1)+' 比'+convert(varchar(50),@i2)+' 大'
end
else
begin
print convert(varchar(50),@i1)+' 比'+convert(varchar(50),@i2)+' 小'
end
--使用Case When 将Score表中的score_1字段转义输出为
select *,备注 = case
when score_1<60 then '不及格'
when score_1>=60 and score_1<=80 then '良好'
when score_1>80 and score_1<=100 then '优秀'
else '异常(Error)'
end
from Score
--利用循环控制(While,Continue,Break)打印输出以下字符:
declare @num int
set @num = 0
while(@num<100)
begin
set @num =@num + 1
if(@num=6 or @num = 7)
continue;
if(@num>9)
break;
print @num
end