select from Category
go
—go—用于执行批处理代码
select from Product
—declare 用于声明变量
declare @a int
declare @b int
—设置(赋值)
set @a=1s
print @a
select @a
—2
declare @c varchar(50)
declare @d money
select top 1 @c= ProductName,@d=Price from Product order by Price desc
print @c
print @d
—输出
—print
—select
—请打印卖得最多的商品名称
declare @id int ,@cc varchar(50)
select top 1 @id=Productid from OrderDetails
group by ProductID
order by sum(Quantity) desc
select @cc=ProductName from Product where ProductID=@id
print @cc
————————————————-全局变量 @@(双@符号)——————————————————-
—@@identity 获取上一次操作的主键(id)身份
—@@rowcount 获取上一次操作的影响行
—insert into Product (ProductID)values(‘小陈’)
—select @@identity
—select @@rowcount
print @@error ——-获取错误编写
print @@language ——-获取数据库语言
print @@version ——-获取系统版本和数据库版本
print @@servername ——-获取服务器名称
if(1=1)
begin —开始
print(‘真的’)
end —结束
else
begin
print(‘假的’)
end
if exists(select * from Product where Price=49)
begin
print(‘有’)
end
else
begin
print(‘没有’)
end
select * from Category
—查询一下“儿童玩具”分类属于那个大分类
select *from Category a right outer join Category b on a.CategoryID=b.P_Cate where b.CategoryName=’儿童玩具’
select * from Category
select Categoryid,CategoryName,
case p_cate
when 1 then ‘母婴’
when 2 then ‘手机数码’
when 3 then ‘箱包’
when 4 then ‘百货’
else ‘其他’ end as ‘类别’
from Category
—当客户是男的,就显示先生,否则就显示女士
select Customername,
case gender
when ‘男’ then’公子’
when ‘女’ then ‘小姐’ end as ‘称呼’
from Customer
————————-100元以下显示便宜,100到200显示一般,大于200显示很贵
select Productname,price,
case
when price<100 then '便宜'
when price<=200 then ‘一般’
when price>200 then ‘很贵’ end as ‘贵不贵’ from Product
—-循环while——————————————————————-
print ‘嘉嘉是个大帅哥’
declare @aa int
set @aa=0
while (@aa<5)
begin
print ‘嘉嘉是个大帅哥’
set @aa=@aa+1
end
—-输出从十到一————————————
declare @bb int
set @bb=10
while (@bb>0)
begin
print @bb
set @bb=@bb-1
end
—小练习:输出20以内能被3整除的数
declare @dd int
set @dd=20
while (@dd>0)
begin
if @dd%3=0
begin
print @dd
end
set @dd=@dd-1
end
—输出10以内的偶数
declare @ee int
set @ee=10
while (@ee>0)
begin
if @ee%2=1
begin
set @ee=@ee-1
continue
end
else
begin
print @ee
set @ee=@ee-1
end
end
