经典SQL语句收藏

以下是我平时在工作中收藏的一些觉得有用的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 )


不断更新中...

Tag标签: SQL
posted on 2008-12-31 12:59 发布:水寒冰 阅读(653) 评论(0) 收藏 所属分类: SQL
  • 评论
  • 点击刷新
  • [使用Ctrl+Enter键可以直接提交]

表情图标

[smile][confused][cool][cry][eek][angry][wink][sweat][lol][stun][razz][redface][rolleyes][sad][yes][no][heart][star][music][idea]
Advertise
Category
Time Counter

离十一还有

Recent Article
Statistics
Recent Comments
Archive
Links
Support
《良机》 鲜果阅读器订阅图标
 
TOP