[转载]SQL常用语句

--创建索引:create [unique] index idxname on tabname(col….)
--删除索引:drop index idxname
--注:索引是不可更改的,想更改必须删除重新建。

--UNION 运算符
--当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。

--复制表
--select * into b from a where 1<>1
--select top 0 * into b from a
--select vale1, value2 into b from a
--insert into b(a, b, c) select d,e,f from b;

--两张关联表,删除主表中已经在副表中没有的信息
--delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )

--日程安排提前五分钟提醒
--SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5

--一条sql 语句搞定数据库分页
--select top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段 = a.主键字段 order by a.排序字段

--包括所有在 TableA中但不在 TableB和TableC中的行并消除所有重复行而派生出一个结果表
--(select a from tableA ) except (select a from tableB) except (select a from tableC)

--随机取出10条数据
--select top 10 * from tablename order by newid()

--删除重复记录
--delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)
--select distinct * into temp from tablename
--delete from tablename
--insert into tablename select * from temp

--SQL SERVER中直接循环写入数据
--declare @i int
--set @i=1
--while @i<30
--begin
--   insert into test (userid) values(@i)
--    set @i=@i+1
--end
--要求就裱中所有沒有及格的成績,在每次增長0.1的基礎上,使他們剛好及格:
--Name     score

--Zhangshan   80

--Lishi       59

--Wangwu      50

--Songquan    69

--while((select min(score) from tb_table)<60)

--begin

--update tb_table set score =score*1.01

--where score<60

--if  (select min(score) from tb_table)>60

--  break

-- else

--    continue

--end

--isnull( check_expression , replacement_value )如果check_expression為空,則返回replacement_value的值,不為空,就返回check_expression字符操作类



原文地址:https://www.cnblogs.com/LD1018/p/8036602.html