--1、while循环
declare @sum int
declare @i int
set @i=1
set @sum=0
while(@i<101)
begin
set @sum =@sum+@i
set @i=@i+1
if(@i>90)
print @i
end
print @sum --2、goto语句
declare @num int
set @num=100
flag:
print @num
select @num=@num+1
while(@num<106)goto flag
print '------------------'
print @num --3、表变量 declare @mytable table(nam nvarchar(50),num nvarchar(50)) insert into @mytable select ClassName,ClassNum from dbo.tb_Class select * from @mytable --4、局部临时表,全局临时表 --局部临时表
create table #temp_tb(
id int,
pwd nvarchar(50)
) --全局临时表
create table ##temp_tb(
id int,
pwd nvarchar(50)
) --5、批量导入数据并创建临时表
select ClassName,ClassNum into #temp from dbo.tb_Class
select * from #temp --6、区间查询 between and (not between and) select * from tb_class where classnum between 1200 and 1500 select * from tb_class where classnum not between 1200 and 1500 --7、in变量,is null is not null,去重复distinct --8、随机查询、子查询 select top 1 * from tb_class order by newid() --9、行号 select '行号'=identity(int,1,1),classname,classnum into #temp from tb_class
select * from #temp --模糊查询 like not like "% _ []"通配符 select * from tb_class where className like '高%[^三]' select * from tb_class where des like '[a-z]%' select * from tb_class where ClassNum like '[^1]%' select * from tb_class where des like '%100/%%' escape '/' select * from tb_class where des+ClassName like '%[二在]%' --case 语句、类型转换 cast显示转换
select *,班级=
case
when ClassNum>1500 then '大班'
when ClassNum=1500 then '中班'
when ClassNum<1500 then cast(ClassNum as nvarchar(20))
end
from tb_class --类型转换 1,cast 2,Convert
select convert(nvarchar(20),getdate(),111)as 时间格式 --去除空格 rtrim ltrim select ltrim(' 中国 人 ')as title --截取字符串substring select substring('中华人民共和国',3,2) as title --字符插入sutff select stuff('中华共和国',3,0,'人民') as title --计算字符长度 len
select len('中华人民共和国') --字符串大小写lower upper
--字符串替换replace
select replace('中*华*人*民*共*和*国','*','-') --获取字符所在位置 select substring('010-99998888',0,charindex('-','010-99998888')) --日期函数
select year(getdate())
select month(getdate())
select day(getdate())
select datepart(year,getdate())
select datepart(hh,getdate()) select datename(dw,getdate()) as 今天星期几 select datename(ww,getdate()) as 本周是一年的第几周 select datename(yy,getdate()) as 年份 select datename(dy,getdate()) as 今天是一年中的第几天 select datediff(d,'2012-11-27','2012-11-30') as 相差天数
select datediff(hh,'2012-11-27','2012-11-30') as 相差小时 select getdate(),dateadd(d,3,getdate()) as 增加三天
select getdate(),dateadd(hh,3,dateadd(d,3,getdate())) as 增加三天在增加三小时 --排序order by 笔画排序:Chinese_prc_stroke_cs_as_ks_ws,音序排序:chinese_prc_cs_as select * from tb_class order by des collate Chinese_prc_stroke_cs_as_ks_ws select * from tb_class order by des collate Chinese_prc_cs_as --动态排序 declare @myorder int
set @myorder=2
select * from tb_class order by case @myorder
when 1 then classnum
when 2 then id
end
desc --聚合函数,where(作用单行) 和 having(作用多行)区别 select count(distinct classname) from tb_class --游标 sql语句面向一个集合操作,游标面向逐条记录的操作
declare cur_s cursor
for select * from tb_class
for read only --只读游标
for update of classname--更新游标 declare @cur_ss cursor--游标变量 open cur_s fetch next from cur_s while @@FETCH_STATUS=0
begin
fetch next from cur_s
end close cur_s
--释放游标
deallocate cur_s --存储过程是一组为了完成特定功能的SQL语句集合,创建Create、修改Alter、删除Drop
create procedure GetClassInfo
(
@id int,
@ClassName nvarchar(50),
@Sum int output--存储过程返回值一种情况
)
as
begin
select @Sum=sum(ClassNum)from tb_Class where id>@id and ClassName=@ClassName
declare @AllSum int
select @AllSum=sum(ClassNum)from tb_Class
return @AllSum--存储过程返回值另一种情况
end
go
declare @sum int
declare @AllSum int
EXEC @AllSum=GetClassInfo 1,'高三',@sum output
select @sum as 总数,@AllSum as 全部总数 --重命名存储过程
exec sp_rename 'GetClassInfo','ReNameGetInfo'
--监视存储过程
exec sp_monitor
--返回参数含义
-- *last_run: 上次运行时间
--
-- *current_run:本次运行的时间
--
-- *seconds: 自动执行存储过程后所经过的时间
--
-- *cpu_busy:计算机CPU处理该存储过程所使用的时间
--
-- *io_busy:在输入和输出操作上花费的时间
--
-- *idle:SQL Server已经空闲的时间
--
-- *packets_received:SQL Server读取的输入数据包数
--
-- *packets_sent:SQL Server写入的输出数据包数
--
-- *packets_error:SQL Server在写入和读取数据包时遇到的错误数
--
-- *total_read: SQL Server读取的次数
--
-- *total_write: SQLServer写入的次数
--
-- *total_errors: SQL Server在写入和读取时遇到的错误数
--
-- *connections:登录或尝试登录SQL Server的次数 --自动执行存储过程
--sp_procoption [@procName=] 'procedure', [@optionName=] 'option', [@optionValue=] 'value'
-- [@procName=] 'procedure': 即自动执行的存储过程
--
-- [@optionName=] 'option':其值是startup,即自动执行存储过程
--
-- [@optionValue=] 'value':表示自动执行是开(true)或是关(false)
exec sp_procoption @procName='masterproc', @optionName='startup', @optionValue='true' --自定义函数
--标量函数
create function myfunAdd(@a int,@b int)
returns int
as
begin
declare @c int
set @c=@a+@b
return @c
end
go
declare @a1 int,
@b1 int,
@c1 int
set @a1=8
set @b1=7
set @c1=School.dbo.myfunAdd(@a1,@b1)
print @c1 --表值函数
create function GetTable()
returns table
as
return (select * from tb_class) select * from School.dbo.GetTable() --触发器,特殊的存储过程,嵌套触发器、递归触发器 create trigger mytrigger
on tb_Class
for update,delete,insert--三种触发器
as
update tb_student set age=age+1 where id =1 update tb_Class set des='???' where id =1
delete from tb_Class where id=6 --事务
begin try
begin transaction
insert into tb_class values('特色班',100,'描述')
insert into tb_student([name],sex,age,classid) values('小刘',1,22,5)
commit transaction
end try
begin catch
rollback
end catch --保存事务
begin transaction
insert into tb_class values('特色二班',100,'描述')
insert into tb_student([name],sex,age,classid) values('小二',1,22,5)
save transaction save1
insert into tb_class values('特色三班',100,'描述')
insert into tb_student([name],sex,age,classid) values('小叁',1,22,5)
rollback transaction save1
commit transaction --事务并发控制
begin tran
select * from dbo.tb_Class with(holdlock)
waitfor delay '00:00:10'
commit tran update tb_Class set ClassNum=200 where id=12
select * from tb_Class --视图 --创建约束,主键约束、外键约束、唯一约束、check约束、default约束 --创建表设置字段
create table ss
(
id int identity(1,1) not null constraint pk_id primary key ) --修改表设置字段
alter table dbo.tb_Class
add constraint pk_id primary key(id)
-- drop constraint pk_id --删除主键
--add constraint un_ss unique(id)--唯一约束
add constraint ch_sex check (like '[0-1]')
add constraint de_sex default 1 for sex alter table dbo.tb_student
add constraint fk_classid
foreign key(classid)
references tb_Class (id)

SQL基础语法等的更多相关文章

  1. ASP.NET实现二维码 ASP.Net上传文件 SQL基础语法 C# 动态创建数据库三(MySQL) Net Core 实现谷歌翻译ApI 免费版 C#发布和调试WebService ajax调用WebService实现数据库操作 C# 实体类转json数据过滤掉字段为null的字段

    ASP.NET实现二维码 using System;using System.Collections.Generic;using System.Drawing;using System.Linq;us ...

  2. Spring mybatis源码篇章-动态SQL基础语法以及原理

    通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-Mybatis的XML文件加载 前话 前文通过Spring中配置mapperLocations属性来进行对m ...

  3. SQL基础语法(二)

    SQL SELECT 语句 本章讲解 SELECT 和 SELECT * 语句. SQL SELECT 语句 SELECT 语句用于从表中选取数据. 结果被存储在一个结果表中(称为结果集). SQL ...

  4. SQL基础语法笔记教程整理

    PS:本文适用SQL Server2008语法. 一.关系型数据库和SQL 实际上准确的讲,SQL是一门语言,而不是一个数据库. 什么是SQL呢?简而言之,SQL就是维护和使用关系型数据库中的的数据的 ...

  5. SQL基础语法提纲

    一.SQL需知5点 1.SQL是Structured Query Language的缩写,是用来访问关系型数据库的,非过程化的,高级编程语言. 2.SQL具有语法高度综合统一,高度的非过程化,对集合进 ...

  6. SQL 基础语法笔记教程整理

    最近从图书馆借了本介绍 SQL 的书,打算复习一下基本语法,记录一下笔记,整理一下思路,以备日后复习之用. PS:本文适用 SQL Server2008 语法. 首先,附一个发现的 MySQL 读书笔 ...

  7. sql基础语法大全 转载过来的,出处忘了!

    一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...

  8. sql 基础语法使用

    SQL的一些基础查询语法    基础.限定.模糊查询     关键字都是大写. 使用 BETWEENN AND 的时候小的数字或者日期放到  AND(并且)  的面前,大的一个放到AND 后面. 示例 ...

  9. SQL基础语法(五)

    SQL INSERT INTO 语句INSERT INTO 语句 INSERT INTO 语句用于向表格中插入新的行. 语法:INSERT INTO 表名称 VALUES (值1, 值2,....) ...

  10. SQL基础语法(三)

    SQL WHERE 子句 WHERE 子句用于规定选择的标准. WHERE 子句 如需有条件地从表中选取数据,可将 WHERE 子句添加到 SELECT 语句. 语法SELECT 列名称 FROM 表 ...

随机推荐

  1. 【知识点】业务连接服务(BCS)认证概念整理

    业务连接服务(BCS)认证概念整理 I. BDC认证模型 BDC服务支持两种认证模型:信任的子系统,模拟和代理. 在信任的子系统模型中,中间层(通常是Web服务器)通过一个固定的身份来向后端服务器取得 ...

  2. zmap使用笔记

    zmap使用笔记 zmap, 一个网络端口开放性的快速扫描工具.至于这个工具的特色,配置参数,和比的工具的对比,不做介绍.只记录一下近期使用过程中,遇到的问题.软件版本:2.1.1 传言与现实 传言: ...

  3. jquery datatable 参数

    DataTables(http://www.datatables.net/)应该是我到目前为止见过的,功能最强大的表格解决方案(当然,不计算其它整套框架中的table控件在内). 先把它主页上写的特性 ...

  4. ES6中的Class

    对于javascript来说,类是一种可选(而不是必须)的设计模式,而且在JavaScript这样的[[Prototype]] 语言中实现类是很蹩脚的. 这种蹩脚的感觉不只是来源于语法,虽然语法是很重 ...

  5. Python图像处理库:Pillow 初级教程

    Python图像处理库:Pillow 初级教程 2014-09-14 翻译 http://pillow.readthedocs.org/en/latest/handbook/tutorial.html ...

  6. JAVA编程规则【转自java编程思想】

    本附录包含了大量有用的建议,帮助大家进行低级程序设计,并提供了代码编写的一般性指导: (1) 类名首字母应该大写.字段.方法以及对象(句柄)的首字母应小写.对于所有标识符,其中包含的所有单词都应紧靠在 ...

  7. asp.net下拉列表绑定数据库多个字段

    select ID, CONVERT(varchar(10),TBName) +','+RoomName+ ',最大人数:'+CONVERT(varchar(10),MAXNum) as ClassR ...

  8. ORA-01704: string literal too long

    update mkt_page_links set longdescription = ' {some html text > 4000 char} ' where menuidno = 310 ...

  9. pcA降维算法

    http://ufldl.stanford.edu/wiki/index.php/主成分分析 if ~exist('train_IM_all','var')||~exist('train_LA_all ...

  10. 信息安全系统设计基础课程实践:简单TUI游戏设计

    简单TUI游戏设计                目       录               一                      Curses库简介与基本开发方法             ...