前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >简单的语法知识

简单的语法知识

作者头像
liulun
发布2022-05-08 17:51:37
2130
发布2022-05-08 17:51:37
举报
文章被收录于专栏:liulun

1 先举个例子

代码语言:javascript
复制
use myshop
declare @ident int
insert into orders
(customerid,orderdate)
values
(25,dateadd(day,-1,getdate()))--当前时间减去1天,就是昨天
select @ident = @@identity
insert into details
(orderid,productid,unitprice,quantity)
values
(@ident,1,50,25)
select 'the orderid of the inserted row is' + convert(varchar(8),@ident)

把数据插入定单表,得到新插入的记录的id然后插入定单明细表

2 定义变量给变量赋值 declare @myval int; set @myval = 10; set @myval1 = @myval2*12; --下面语句是在查询中赋值变量 select @myval = max(id)+1 form mytable

3 if和else

代码语言:javascript
复制
use xland
if not exists(select id from mytable)
    print '暂时没有数据'
else
    print cast(@@rowcount as varchar)+'被检索到'

上面语句执行结果是   0行被检索到 4 begin和end 如果条件内部要跟代码块(多条语句) 就要用到begin和end

5 case语句 第一种case语句

代码语言:javascript
复制
use AdventureWorks
select SalesOrderID ,Position = Case SalesOrderID%10
    when 1 then 'First'
    when 2 then 'Second'
    when 3 then 'third'
    else 'some thing else'
end 
from Sales.SalesOrderHeader

第二种case语句

代码语言:javascript
复制
use AdventureWorks
select SalesOrderID ,Position = Case
    when  SalesOrderID%10 = 1 then 'First'
    when  SalesOrderID%10 = 2 then 'Second'
    when  SalesOrderID%10 = 3 then 'third'
    else 'some thing else'
end 
from Sales.SalesOrderHeader

第一种CASE  when子句中可以跟表达式 第二种case   when子句中表达式要返回true 或者false 当前面的条件和后面的条件同时满足时 按前面的条件处理,不需要break语句

6 循环语句 while 1=1 begin   waitfor time '01:00'     update mytable set num = 12 where id = 13 end 上面语句是在每天凌晨1点执行指定的操作 while允许break和continue的存在

代码语言:javascript
复制
use xland
if not exists(select id from mytable)
    print '没?有?检?索?到?数?据?'
else
    begin
        select id from mytable 
        print cast(@@rowcount as varchar)+'行?被?检?索?到?'
    end

上面语句执行结果是:2行被检索到

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2008-12-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档