国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

sql語句

2024-07-21 02:44:52
字體:
來源:轉載
供稿:網友
 1. 說明:復制表(只復制結構,源表名:a,新表名:b) SQL:select * into bfrom awhere 1<>1;

2. 說明:拷貝表(拷貝數(shù)據,源表名:a,目標表名:b) SQL:insert into b(a, b, c)select d, e, ffrom     b;

3. 說明:顯示文章、提交人和最后回復時間 SQL:select a.title, a.username, b.adddatefrom table a,(select max(adddate) adddatefrom table where table.title=a.title) b  

4. 說明:外連接查詢(表名1:a,表名2:b) SQL:select a.a, a.b, a.c, b.c, b.d, b.ffrom aLEFT OUTJOIN bON a.a= b.c;  

5. 說明:日程安排提前五分鐘提醒 SQL:select * from 日程安排where datediff(’’minute’’, f開     始時間,getdate())>5     

6. 說明:兩張關聯(lián)表,刪除主表中已經在副表中沒有的信息 SQL:delete from infowhere not exists(select * from infobzwhere info.infid=infobz.infid );

【SQL SERVER 數(shù)據庫實用SQL語句】

1.按姓氏筆畫排序:Select * From TableNameOrder By CustomerName Collate Chinese_PRC_Stroke_ci_as

2.分頁SQL語句select * from(select (row_number()OVER (ORDER BY tab.IDDesc))as rownum,tab.* from 表名As tab)As twhere rownumbetween 起始位置And 結束位置

3.獲取當前數(shù)據庫中的所有用戶表select * from sysobjectswhere xtype='U' and category=0

4.獲取某一個表的所有字段select namefrom syscolumnswhere id=object_id('表名')

5.查看與某一個表相關的視圖、存儲過程、函數(shù)select a.* from sysobjects a, syscomments bwhere a.id= b.idand b.text like '%表名%'

6.查看當前數(shù)據庫中所有存儲過程select nameas 存儲過程名稱from sysobjectswhere xtype='P'

7.查詢用戶創(chuàng)建的所有數(shù)據庫select * from master..sysdatabases Dwhere sidnot in(select sidfrom master..sysloginswhere name='sa') 或者select dbid, nameAS DB_NAME from master..sysdatabaseswhere sid<> 0x01

8.查詢某一個表的字段和數(shù)據類型select column_name,data_typefrom information_schema.columnswhere table_name= '表名'

9.使用事務在使用一些對數(shù)據庫表的臨時的SQL語句操作時,可以采用SQL SERVER事務處理,防止對數(shù)據操作后發(fā)現(xiàn)誤操作問題開始事務Begin tran Insert Into TableNameValues(…) SQL語句操作不正常,則回滾事務。 回滾事務Rollback tran SQL語句操作正常,則提交事務,數(shù)據提交至數(shù)據庫。 提交事務Commit tran

10. 按全文匹配方式查詢字段名LIKE N'%[^a-zA-Z0-9]China[^a-zA-Z0-9]%' OR 字段名LIKE N'%[^a-zA-Z0-9]China' OR 字段名LIKE N'China[^a-zA-Z0-9]%' OR 字段名LIKE N'China

11.計算執(zhí)行SQL語句查詢時間 declare @d datetime set @d=getdate() select * from SYS_ColumnProperties select [語句執(zhí)行花費時間(毫秒)]=datediff(ms,@d,getdate())

12、說明:幾個高級查詢運算詞

A: UNION 運算符 UNION 運算符通過組合其他兩個結果表(例如 TABLE1 和 TABLE2)并消去表中任何重復行而派生出一個結果表。當 ALL 隨 UNION 一起使用時(即 UNION ALL),不消除重復行。兩種情況下,派生表的每一行不是來自 TABLE1 就是來自 TABLE2。

B: EXCEPT 運算符 EXCEPT 運算符通過包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重復行而派生出一個結果表。當 ALL 隨 EXCEPT 一起使用時 (EXCEPT ALL),不消除重復行。

C: INTERSECT 運算符 INTERSECT 運算符通過只包括 TABLE1 和 TABLE2 中都有的行并消除所有重復行而派生出一個結果表。當 ALL 隨 INTERSECT 一起使用時 (INTERSECT ALL),不消除重復行。



計算一個庫里各個表的記錄總數(shù):

select b.name,a.rowcntfrom sysindexes a,sysobjects bwhere a.id=b.idand a.indid<2 and b.xtype='u'

--統(tǒng)計數(shù)據庫里每個表的詳細情況 EXEC sp_MSforeachtable@command1="sp_spaceused'?'"-

-獲得每個表的記錄數(shù)和容量: EXEC sp_MSforeachtable@command1="print '?'",@command2="sp_spaceused'?'",@command3= "SELECT count(*)FROM ? "













排序問題

CREATE TABLE [t] (

[id] [int] IDENTITY (1,1)NOT NULL ,

[GUID] [uniqueidentifier] NULL

)ON [PRIMARY] GO

下面這句執(zhí)行5次

insert tvalues (newid())

查看執(zhí)行結果

select * from t

1、 第一種

select * from torder by case id

when 4 then 1 when 5 then 2 when 1 then 3 when 2 then 4 when 3 then 5 end

2、 第二種

select * from torder by (id+2)%6

3、 第三種

select * from torder by charindex(cast(idas varchar),'45123')

4、 第四種

select * from tWHERE idbetween 0 and 5 order by charindex(cast(idas varchar),'45123')

5、 第五種

select * from torder by case when id>3 then id-5 else idend

6、 第六種

select * from torder by id/ 4 desc,idasc

一條語句刪除一批記錄 首先id列是int標識類類型,然后刪除ID值為5,6,8,9,10,11的列,這里的cast函數(shù)不能用convert函數(shù)代替,而且轉換的類型必須是varchar,而不能是char,否則就會執(zhí)行出你不希望的結果,這里的"5,6,8,9,10,11"可以是你在頁面上獲取的一個chkboxlist構建成的值,然后用下面的一句就全部刪除了,比循環(huán)用多條語句高效吧應該。delete from [fujian] where charindex(','+cast([id] as varchar)+',',','+'5,6,8,9,10,11,'+',')>0 還有一種就是delete from table1where idin(1,2,3,4 )



---動態(tài)SQL基本語法:

1 :普通SQL語句可以用exec執(zhí)行

Select * from tableNameexec('select * from tableName')exec sp_executesql N'select * from tableName' -- 請注意字符串前一定要加N

2:字段名,表名,數(shù)據庫名之類作為變量時,必須用動態(tài)SQL

declare @fname varchar(20)

set @fname = 'FiledName'

Select @fname from tableName-- 錯誤,不會提示錯誤,但結果為固定值FiledName,并非所要。exec('select' + @fname + ' from tableName')-- 請注意 加號前后的 單引號的邊上加空格

當然將字符串改成變量的形式也可

declare @fname varchar(20)

set @fname = 'FiledName' --設置字段名

declare @s varchar(1000)

set @s = 'select' + @fname + ' from tableName' exec(@s)-- 成功

exec sp_executesql@s -- 此句會報錯



declare @s Nvarchar(1000)-- 注意此處改為nvarchar(1000)

set @s = 'select' + @fname + ' from tableName'

exec(@s)-- 成功

exec sp_executesql@s -- 此句正確

3. 輸出參數(shù)

declare @num int,@sqls nvarchar(4000)

set @sqls='select count(*) from tableName'

exec(@sqls)

--如何將exec執(zhí)行結果放入變量中?

declare @num int,@sqls nvarchar(4000)

set @sqls='select @a=count(*) from tableName'

exec sp_executesql@sqls,N'@a int output',

@num outputselect @num

1 :普通SQL語句可以用Exec執(zhí)行 例:

Select * from tableNameExec('select * from tableName')Exec sp_executesql N'select * from tableName' -- 請注意字符串前一定要加N







1、說明:復制表(只復制結構,源表名:a 新表名:b) (access可用)

法一:select * into bfrom awhere 1 <>1

法二:select top 0 * into bfrom a

2、說明:拷貝表(拷貝數(shù)據,源表名:a 目標表名:b) (Access可用)

insert into b(a, b, c)select d,e,ffrom a;

3、說明:跨數(shù)據庫之間表的拷貝(具體數(shù)據使用絕對路徑) (Access可用)

insert into b(a, b, c)select d,e,ffrom bin ‘具體數(shù)據庫’where 條件

例子:..from bin '"&Server.MapPath(".")&"/data.mdb" &"' where..

4、說明:子查詢(表名1:a 表名2:b)

select a,b,cfrom awhere aIN (select dfrom b ) 或者:select a,b,cfrom awhere aIN (1,2,3)

5、說明:顯示文章、提交人和最后回復時間

select a.title,a.username,b.adddatefrom table a,(select max(adddate) adddatefrom table where table.title=a.title) b

6、說明:外連接查詢(表名1:a 表名2:b)

select a.a, a.b, a.c, b.c, b.d, b.ffrom aLEFT OUTJOIN bON a.a= b.C

7、說明:在線視圖查詢(表名1:a )

select * from (SELECT a,b,cFROM a) Twhere t.a> 1;

8、說明:between的用法,between限制查詢數(shù)據范圍時包括了邊界值,not between不包括

select * from table1where timebetween time1

9、說明:in 的使用方法select * from table1where a[not] in (‘值1’,’值2’,’值4’,’值6’)

10、說明:兩張關聯(lián)表,刪除主表中已經在副表中沒有的信息

delete from table1where not exists (select * from table2where table1.field1=table2.field1 )

11、說明:四表聯(lián)查問題:

select * from aleft inner join bon a.a=b.bright inner join con a.a=c.cinner join don a.a=d.dwhere .....

12、說明:日程安排提前五分鐘提醒 SQL:

select * from 日程安排where datediff('minute',f開始時間,getdate())>5

13、說明:一條sql 語句搞定數(shù)據庫分頁

select top 10 b.* from (select top 20 主鍵字段,排序字段from 表名order by 排序字段desc) a,表名 bwhere b.主鍵字段= a.主鍵字段order by a.排序字段

14、說明:前10條記錄

select top 10 * form table1where 范圍

15、說明:選擇在每一組b值相同的數(shù)據中對應的a最大的記錄的所有信息(類似這樣的用法可以用于論壇每月排行榜,每月熱銷產品分析,按科目成績排名,等等.)

select a,b,cfrom tablename tawhere a=(select max(a)from tablename tbwhere tb.b=ta.b)

16、說明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重復行而派生出一個結果表 (select afrom tableA )except (select afrom tableB)except (select afrom tableC)

17、說明:隨機取出10條數(shù)據

select top 10 * from tablenameorder by newid()

18、說明:隨機選擇記錄

select newid()

19、說明:刪除重復記錄

Delete from tablenamewhere idnot in (select max(id)from tablenamegroup by col1,col2,...)

20、說明:列出數(shù)據庫里所有的表名

select namefrom sysobjectswhere type='U'

21、說明:列出表里的所有的列

select namefrom syscolumnswhere id=object_id('TableName')

22、說明:列示type、vender、pcs字段,以type字段排列,case可以方便地實現(xiàn)多重選擇,類似

select 中的case。select type,sum(case venderwhen 'A' then pcselse 0 end),sum(case venderwhen 'C' then pcselse 0 end),sum(case venderwhen 'B' then pcselse 0 end)FROM tablenamegroup by type 顯示結果: type vender pcs 電腦 A1 電腦 A1 光盤 B2 光盤 A2 手機 B3 手機 C3

23、說明:初始化表

table1TRUNCATE TABLE table1

24、說明:選擇從10到15的記錄

select top 5 * from (select top 15 * from table order by idasc) table_別名order by iddesc declare @a varchar(100),@b varchar(20)select @a='abcdefbcmnbcde',@b='bc' select (len(@a)-len(replace(@a,@b,'')))/len(@b)

說明:選擇在每一組b值相同的數(shù)據中對應的a最大的記錄的所有信息(類似這樣的用法可以用于論壇每月排行榜,每月熱銷產品分析,按科目成績排名,等等.)

select a,b,cfrom tablename tawhere a=(select max(a)from tablename tbwhere tb.b=ta.b)



一年中所有為星期二的日期

select dateadd(day,x,col),'星期二' from

(

select cast('2006-1-1' as datetime)as col )across join

(SELECT top 365 b8.i+b7.i+ b6.i+ b5.i+ b4.i+b3.i+b2.i+ b1.i+ b0.i x

FROM(SELECT 0 iUNION ALL SELECT 1) b0CROSS JOIN(SELECT 0 iUNION ALL SELECT 2) b1CROSS JOIN(SELECT 0 iUNION ALL SELECT 4) b2CROSS JOIN(SELECT 0 iUNION ALL SELECT 8) b3CROSS JOIN(SELECT 0 iUNION ALL SELECT 16) b4CROSS JOIN(SELECT 0 iUNION ALL SELECT 32) b5CROSS JOIN(SELECT 0 iUNION ALL SELECT 64) b6CROSS JOIN(SELECT 0 iUNION ALL SELECT 128) b7CROSS JOIN(SELECT 0 iUNION ALL SELECT 256) b8order by 1

)bwhere datepart(dw,dateadd(day,x,col))=3





1. 查看數(shù)據庫的版本

select @@version

2. 查看數(shù)據庫所在機器操作系統(tǒng)參數(shù)

exec master..xp_msver

3. 查看數(shù)據庫啟動的參數(shù)

sp_configure

4. 查看數(shù)據庫啟動時間

select convert(varchar(30),login_time,120)from master..sysprocesseswhere spid=1

查看數(shù)據庫服務器名和實例名print 'Server Name...............:' + convert(varchar(30),@@SERVERNAME)print 'Instance..................:' + convert(varchar(30),@@SERVICENAME)

5. 查看所有數(shù)據庫名稱及大小

sp_helpdb 重命名數(shù)據庫用的SQL sp_renamedb'old_dbname','new_dbname'

6. 查看所有數(shù)據庫用戶登錄信息 sp_helplogins 查看所有數(shù)據庫用戶所屬的角色信息 sp_helpsrvrolemember 修復遷移服務器時孤立用戶時,可以用的fix_orphan_user腳本或者LoneUser過程 更改某個數(shù)據對象的用戶屬主 sp_changeobjectowner[@objectname =] 'object',[@newowner =] 'owner' 注意: 更改對象名的任一部分都可能破壞腳本和存儲過程。把一臺服務器上的數(shù)據庫用戶登錄信息備份出來可以用add_login_to_aserver腳本

7. 查看鏈接服務器 sp_helplinkedsrvlogin 查看遠端數(shù)據庫用戶登錄信息 sp_helpremotelogin

8.查看某數(shù)據庫下某個數(shù)據對象的大小 sp_spaceused@objname 還可以用sp_toptables過程看最大的N(默認為50)個表 查看某數(shù)據庫下某個數(shù)據對象的索引信息 sp_helpindex@objname 還可以用SP_NChelpindex過程查看更詳細的索引情況 SP_NChelpindex@objname clustered索引是把記錄按物理順序排列的,索引占的空間比較少。對鍵值DML操作十分頻繁的表我建議用非clustered索引和約束,fillfactor參數(shù)都用默認值。 查看某數(shù)據庫下某個數(shù)據對象的的約束信息 sp_helpconstraint@objname 9.查看數(shù)據庫里所有的存儲過程和函數(shù)use @database_name sp_stored_procedures 查看存儲過程和函數(shù)的源代碼 sp_helptext'@procedure_name' 查看包含某個字符串@str的數(shù)據對象名稱 select distinct object_name(id)from syscommentswhere text like '%@str%' 創(chuàng)建加密的存儲過程或函數(shù)在AS前面加WITH ENCRYPTION參數(shù) 解密加密過的存儲過程和函數(shù)可以用sp_decrypt過程

10.查看數(shù)據庫里用戶和進程的信息 sp_who 查看SQL Server數(shù)據庫里的活動用戶和進程的信息 sp_who'active' 查看SQL Server數(shù)據庫里的鎖的情況 sp_lock 進程號1--50是SQL Server系統(tǒng)內部用的,進程號大于50的才是用戶的連接進程.spid是進程編號,dbid是數(shù)據庫編號,objid是數(shù)據對象編號 查看進程正在執(zhí)行的SQL語句dbcc inputbuffer () 推薦大家用經過改進后的sp_who3過程可以直接看到進程運行的SQL語句 sp_who3 檢查死鎖用sp_who_lock過程 sp_who_lock

11.收縮數(shù)據庫日志文件的方法收縮簡單恢復模式數(shù)據庫日志,收縮后@database_name_log的大小單位為M backup log @database_name with no_logdbcc shrinkfile (@database_name_log,5)

12.分析SQL Server SQL 語句的方法:set statistics time {on | off}set statistics io {on | off} 圖形方式顯示查詢執(zhí)行計劃在查詢分析器->查詢->顯示估計的評估計劃(D)-Ctrl-L 或者點擊工具欄里的圖形 文本方式顯示查詢執(zhí)行計劃set showplan_all {on | off}set showplan_text {on | off }set statistics profile {on | off }

13.出現(xiàn)不一致錯誤時,NT事件查看器里出3624號錯誤,修復數(shù)據庫的方法 先注釋掉應用程序里引用的出現(xiàn)不一致性錯誤的表,然后在備份或其它機器上先恢復然后做修復操作

alter database [@error_database_name] set single_user

修復出現(xiàn)不一致錯誤的表

dbcc checktable('@error_table_name',repair_allow_data_loss)

或者可惜選擇修復出現(xiàn)不一致錯誤的小型數(shù)據庫名

dbcc checkdb('@error_database_name',repair_allow_data_loss)alter database [@error_database_name] set multi_user CHECKDB 有3個參數(shù): repair_allow_data_loss 包括對行和頁進行分配和取消分配以改正分配錯誤、結構行或頁的錯誤, 以及刪除已損壞的文本對象,這些修復可能會導致一些數(shù)據丟失。修復操作可以在用戶事務下完成以允許用戶回滾所做的更改。 如果回滾修復,則數(shù)據庫仍會含有錯誤,應該從備份進行恢復。 如果由于所提供修復等級的緣故遺漏某個錯誤的修復,則將遺漏任何取決于該修復的修復。修復完成后,請備份數(shù)據庫。 repair_fast 進行小的、不耗時的修復操作,如修復非聚集索引中的附加鍵。 這些修復可以很快完成,并且不會有丟失數(shù)據的危險。 repair_rebuild 執(zhí)行由 repair_fast 完成的所有修復,包括需要較長時間的修復(如重建索引)。執(zhí)行這些修復時不會有丟失數(shù)據的危險。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 绥江县| 镇宁| 临泉县| 乡宁县| 迁西县| 定襄县| 赣州市| 玛多县| 顺义区| 花垣县| 彰化县| 固原市| 巴塘县| 同仁县| 错那县| 牡丹江市| 永济市| 册亨县| 莒南县| 临西县| 三门县| 桃园市| 开原市| 樟树市| 怀化市| 隆子县| 酒泉市| 长葛市| 新沂市| 巴马| 遵义县| 阿克| 襄汾县| 连山| 利川市| 武夷山市| 壤塘县| 清原| 迁安市| 陇川县| 土默特右旗|