数据库的高级语法

发布 2020-01-02 09:25:28 阅读 6622

--变量定义和赋值。

全局变量:只读,由系统维护,作用域是单个连接。

-常见全局变量。

-@@error:返回执行的上一个语句的错误号,如果出错,错误号是大于0的整数,不出错则为0

print @@error

select * fro stuinfo

print @@error

select * from stuinfo

print @@error

-@@identity:在insert或select into语句执行后,返回最后一行的标识值,若无标识列,则为null

insert into [user] values('lisi','123456')

print @@identity

-@@rowcount:返回上一语句执行后受影响的行数。

select * from [user]

print @@rowcount

insert into [user]

select 'wangwu','123456' union all

select 'wangwu1','123456'

print @@rowcount

-局部变量。

-定义。declare @i varchar(10)

declare @x varchar(10),@y int

-赋值。-简单赋值(set赋值一次只能赋一个,而select可以批量赋值)

declare @i varchar(20),@j int

-set @i='abc'

-set @j=10

-print @i

select @i='def',@j=11

select @i,@j

-使用数据库中的数据赋值。

-当可以使用唯一确定行时,带上where(不带where默认取得第一行);当数据结果为多条时,使用top 1;或者、判断、提出警告、抛出异常(rainserror)

truncate table [user]

goinsert into [user]

select 'zhangsan','123456' union all

select 'lisi','123456' union all

select 'wangwu','123456'

godeclare @name varchar(20)

select @name=username

from [user]

--where userid=2

print @name

declare @name varchar(20)

select @name=username from [user]

if @@rowcount >=2

print 'error'

elseprint @name

declare @name varchar(20)

select @name=username

from [user]

where userid=13

print @name

-运算符和表达式。

-算术运算、逻辑运算、比较运算。

-a.+:作为连接符时,注意两边的类型。

print '1'+'2'

or and

-c.>、

-d.没有自增、自减。

-流程控制。

-1、标识**块,类似于{}

beginprint 'hello'

end-2、分支语句if...else...

declare @name varchar(20)

select @name=username from [user]

if @@rowcount >=2

print 'error'

elseprint @name

-3、循环。

-打印1-9

declare @i int=1

while(@i < 10)

beginprint @i

set @i=@i+1

end-4、无条件退出语句return:在存储过程中应带有状态值,一般0为成功,非0为失败。

beginprint 'hello'

return

print 'world'

end-5、转向:goto

print 'aaa'

print 'bbb'

a:print 'ccc'

goto a

-6、定时、延时。

waitfor time '10:50:50'

print 'ok'

waitfor delay '00:00:10'

print 'ok'

4、case语句:

create table booktype

btid int identity(1,1) primary key,btname varchar(20) not null

goinsert into booktype values('.net')

insert into booktype values('j**e')

insert into booktype values('sql')

gocreate table book

bid int identity(1,1) primary key,btitle varchar(20),bprice money,btid int references booktype(btid)

goinsert into book

select 'j**a核心技术',75,2 union all

select '数据库概论',37,3 union all

select 'j**a编程思想',60,2 union all

select 'sql高级应用',45,3 union all

select 'c#编程宝典',98,1

go-按书的**给出不同的评价。

-第一种做法。

select btitle,bprice,'低' as classfication from book where bprice < 40

select btitle,bprice,'中' as classfication from book where bprice between 40 and 80

select btitle,bprice,'高' as classfication from book where bprice >80

-第二种做法。

select btitle,bprice,classfication=case

when bprice< 40 then '低'

when bprice between 40 and 80 then '中'

when bprice >80 then '高'

endfrom book

-case简化。

select bid,btitle,bprice,btype=case btname

when 'j**e' then 'j**a方向'

when '.net' then '.net方向'

when 'sql' then '数据库方向'

else '未知'

endfrom book a

inner join booktype b on

-案例:同时修改1号和2号用户的密码。

update [user] set userpwd=case userid

when 1 then 'abcdef'

when 2 then 'helloworld'

endwhere userid in (1,2)

select * from [user]

-exists:是否存在。

if exists(select 1 from where name='databasename')

drop database databasename存储过程。

子查询——将一个查询结果作为另一个查询的查询基础。

create database studb

gouse studb

gocreate table department

depid int identity(1,1) primary key,depname varchar(20)

gocreate table students

stuno int identity(1,1) primary key,stuname varchar(20),sdep int references department(depid),stusex varchar(2)

gocreate table cource

cid int identity(1,1) primary key,cname varchar(20)

gocreate table score

[sid] int identity(1,1) primary key,stuno int references students(stuno),cid int references cource(cid),sscore int

goinsert into department values('计算机科学系')

insert into department values('生物工程系')

insert into students values('李家',1,'男')

insert into students values('王明',2,'男')

insert into students values('何艳',1,'女')

insert into students values('黄涛',2,'男')

insert into cource values('计算机基础')

insert into cource values('微生物学')

insert into score values(1,1,70)

insert into score values(1,2,80)

insert into score values(2,1,60)

insert into score values(2,2,50)

insert into score values(3,1,80)

insert into score values(3,2,90)

select

from students as a

inner join department as bon

数据库的语法

添加列 alter table table name add column name column properties 查询表结构 desc table name 新建 窗口 命令窗口。删除列 alter table table name drop column column name 修改列属性...

数据库操作语法

语法 insert into 表名 列名 values 列值 例 insert into strdents 姓名,性别,出生日期 values 开心朋朋 男 1980 6 15 注意 into可以省略 列名列值用逗号分开 列值用单引号因上 如果省略表名,将依次插入所有列。语法 insert into...

数据库 altertable语法

一 修改表 增加,删除,修改类型 1 如需在表中添加列,请使用下列语法 alter table add datatype 2 要删除表中的列,请使用下列语法 alter table drop column 某些数据库系统不允许这种在数据库表中删除列的方式 drop column column nam...