sql server 全面教程(读书笔记)
第四章,Transact-SQL
P80 cast和convert 直接在查询分析器里输入 select cast(12345 as char),convert(int,3.14),convert(bit,12.33) select cast(100+88 as char),convert(varchar(10),getdate()) select getdate(), convert(char(12),getdate(),1), --几种时间格式 convert(char(24),getdate(),100), convert(char(12),getdate(),112)
连接查询 1.等值连接 from t1,t2 where t1.id=t2.id from t1 join t2 on t1.id=t2.id 2.非等值连接 from t1,t2 where t1.id<t2.id 3.自连接 select a.id,b.id from t1 a,t1 b where a.uid=b.id 4.外部连接 内连接,是指挑选出符合连接条件的数据,参与连接的表是平等的。 外连接,Outer Join,外连接,参与连接的表有主从之分。 以主表中的每行数据去匹配从表的数据列,对于不符合条件的, 以NULL填充,如果是bit类型,则用0 5.以主表的位置,分左连接和右连接。 6.嵌套查询 select * from table where id in (select id from table1) 7.谓词exists select * from table1 where exists (select id from orders where id>222) 8.having select * from table1 having id<11 *************group by理解 select id,avg(uid) from ttest group by id 这里select后面,只能跟一个聚合函数,比如avg(uid), 或者要分组的字段本身,比如id。 *************having理解 having后面必须跟一个聚合函数,比如avg(id) P232合并查询止
081205
select id1 as 编号,name1 as 姓名 from table1 where lever=2 union select id2,name2 from table2 order by 1或order by 编号 注意这里,必须要多个表中查询的字段类型一致。
P234 1.存储查询结果 use pangu exec sp_dboptionpangu,select into,true select id,name into table1 from table2,table3 where id=uid select * from talbe1 //这里需要设置select into为ture 2.存储查询结果到变量中 declare @name varchar(50) declare @account varchar(30) use pangu select @name=fname,@account=num from firms where id=1001 select @name as fname,@account as num --居然可以这样显示? P235全文检索止
P245第11章,数据更新 1.用存储过程插入数据 use pangu insert into table(id,name) execute( select id,sum(num) from table1 ) 2.带子查询的删除语句 delete from table1 where id=( select id from table2 where..) 3.删除当前游标行数据 delete from t1 where current of d_cursor 4.truncate table t1 5.带子查询的更新语句 update t1 set wage=wage+100 where id=( select id from t2 where lever=3) 6.使用连接信息更新数据 update t1 set wage=wage+50 from t1,t2 where t1.id=t2.id P257事务止
P265存储过程和触发器 1.两类,系统存储过程和用户自定义过程。 系统过程存储在master数据库中,以sp_为前缀。 P270止
