1.如何刪除表中的重複記錄?(這裏指記錄的每個字段都要相同)
select distinct * into #temp from tab
delete tab
insert tab select * from #temp
drop table #temp
2.怎樣返回數據庫中用戶表的表單名
select name from sysobjects where xtype='U'
select name from sysobjects where xtype = 'u' and status >=0
3.返回兩個表中共有的所有記錄
select * from testTable as a inner join TestTableChild as b on a.id = b.parentid
4.返回兩個表裏共有的記錄,且不重複select a.id,a.name,b.name from testTable as a inner join TestTableChild as b on a.id = b.parentid group by a.id,a.name,b.name
5.向一個表A中插入記錄,並且插入的記錄在A中不存在(通過一個字段來判斷)insert into trace_users (tracekey,muteSMS,CreateTime,traceuser,tracetime,traceSlot,traceduration) Select 'TRACE_TIMER',0,getdate(),mobileid,getdate(),'30','0' from Epm_EmployeeList where corpid = 10001 and not exists (select traceuser from trace_users ) and mobileid like '13%' and len(mobileid) = 11
6、根據出生日期,算出年齡DATEDIFF(month, T.Birthday, GETDATE()) AS MONTHS //得到月份
MONTHS /12 取整就是年齡
7、等待時間再執行語句
waitfor delay '00:00:05'
select * from studentinfowaitfor time ’23:08:00
select * from employee
8、指定值的範圍查詢stockname like '[a-zA-Z]%' --------- ([]指定值的範圍)
stockname like '[^F-M]%' --------- (^排除指定範圍)
9、從表中獲取值並插入另一張表中insert into table2 (a) select a from table1
10、備份與恢復數據庫backup database SCardDB to disk = 'F:\SCardDB.2006年10月10日(105748).bak'restore database kangda from disk='d:\backup.bak‘
11、對查詢結果隨機排序
SELECT * FROM Northwind..Orders ORDER BY NEWID()
12、按姓氏筆畫排序
Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as
13獲取某一個表的所有字段
select name from syscolumns where id=object_id('表名')
14、記錄轉換select *,case Type when 1 then '移動' when 2 then '聯通' when 3 then '小靈通' end as TypeName from abc
15、按拼音首字母排序select * from 表名 order by 列名 Collate Chinese_PRC_CS_AS_KS_WS
SQL常用語句積累
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.