以下是我平时在工作中收藏的一些觉得有用的SQL语句,共大家使用

1.一句SQL语句获得当月天数:
select Day(dateadd(day,-1,convert(datetime,convert(char(07),dateadd(m,1,getdate()),120)+ '-01 ')))
select 32-Day(getdate()+(32-Day(getdate())))

2.说明:选择从10到15的记录:
select top 5 * from (select top 15 * from tableName order by id asc) table_别名 order by id desc

3.返回库中用户表:
select * from sysobjects where xtype='U'

4、说明:删除重复记录:
Delete from tablename here id not in (select max(id) from tablename group by col1,col2,...)

5.新建一个与a表一样的空的b表:
方法二:select * into b from a where 1 <> 1
方法二:select top 0 * into b from a

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

 

数据库表间数据复制

--1.表结构相同的表,且在同一数据库(如,table1,table2)

Sql :insert into table1 select * from table2 (完全复制)

insert into table1 select distinct * from table2(不复制重复纪录)

insert into table1 select top 5 * from table2 (前五条纪录)

 

 

按姓氏笔画排序

Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as

posted on 2010-11-28 00:50  kitea  阅读(158)  评论(0)    收藏  举报