版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、說明:復(fù)制表(只復(fù)制結(jié)構(gòu),源表名:a新表名:b select * into b from a where 11說明:拷貝表(拷貝數(shù)據(jù),源表名:a目標(biāo)表名:b insert into b(a, b, c select d,e,f from b;說明:顯示文章、提交人和最后回復(fù)時(shí)間select a.title,a.username,b.adddatefrom table a,(select max(adddate adddate from table where table.title=a.title 說明:夕卜 連接查詢(表名 1: a 表名 2: b select a.a, a.b, a.c,
2、 b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c說明:日程安排提前五分鐘提醒 select * from日程安排where datediff(minute,f開始時(shí)間,getdate(5說明:兩張關(guān)聯(lián)表,刪除主表中已經(jīng)在副表 中沒有的信息 delete from info where not exists ( select * from infobz where info.infid=infobz.infid 說明:-SQL: SELECT A.NUM, A.NAME, B.UPD_DATE, B.PREV_UPD_DATE FROM TAB
3、LE1, (SELECT X.NUM, X.UPD_DATE, Y.UPD_DATE PREV_UPD_DATE FROM (SELECT NUM, UPD_DATE, INBOUND_QTY, STOCK_ONHAND FROM TABLE2 WHERE TO_CHAR(UPD_DATE,YYYY/MM = TO_CHAR(SYSDATE, YYYY/MM X, (SELECT NUM, UPD_DATE, STOCK_ONHAND FROM TABLE2 WHERE TO_CHAR(UPD_DATE,YYYY/MM = TO_CHAR(TO_DATE(TO_CHAR(SYSDATE, YY
4、YY/MM | /01,YYYY/MM/DD - 1, YYYY/MM Y, WHERE X.NUM = Y.NUM(+) AND X.IN BOUND_QTY + NVL(Y.STOCK_ONHAND,O X.STOCK_ONHAND B WHERE A.NUM = B.NUM 說明:-select * from studentinfo where not exists(select * from stude nt where stude ntin fo.id=stude nt.id an系名稱 =&strdepartme ntn ame& and 專業(yè)名稱=&strprofessio nn
5、 ame& order by 性別,生源 地,高考總成績 從數(shù)據(jù)庫中去一年的各單位電話費(fèi)統(tǒng)計(jì)(電話費(fèi)定額賀電化肥清單兩 個(gè)表來源)SELECT a.userper, a.tel, a.sta ndfee, TO_CHAR(a.telfeedate, yyyy AS telyear, SUM(decode(TO_CHAR(a.telfeedate, mm, 01, a.factratio n AS JAN, SUM(decode(TO_CHAR(a.telfeedate, mm, 02, a.factratio n AS FRI, SUM(decode(TO_CHAR(a.telfeedate,
6、 mm, 03, a.factratio n AS MAR, SUM(decode(TO_CHAR(a.telfeedate, mm, 04, a.factratio n AS APR, SUM(decode(TO_CHAR(a.telfeedate, mm, 05, a.factratio n AS MAY, SUM(decode(TO_CHAR(a.telfeedate, mm, 06, a.factratio n AS JUE,SUM(decode(TO_CHAR(a.telfeedate, mm, 07, a.factratio n AS JUL, SUM(decode(TO_CHAR
7、(a.telfeedate, mm, 08, a.factratio n AS AGU, SUM(decode(TO_CHAR(a.telfeedate, mm, 09, a.factratio n AS SEP, SUM(decode(TO_CHAR(a.telfeedate, mm, 10, a.factratio n AS OCT, SUM(decode(TO_CHAR(a.telfeedate, mm, 11, a.factratio n AS NOV, SUM(decode(TO_CHAR(a.telfeedate, mm, 12, a.factratio n AS DEC FROM
8、 (SELECT a.userper, a.tel, a.sta ndfee, b.telfeedate, b.factratio n FROM TELFEESTAND a, TELFEE b WHERE a.tel = b.telfax a GROUP BY a.userper, a.tel, a.sta ndfee, TO_CHAR(a.telfeedate, yyyy說明:四表聯(lián)查問題 select * from a leftinner join b on a.a=b.b right inner join c on a.a=c.c inn er jo in d on a.a=d.d wh
9、ere . 說明:得至U表中最小的未使用的 ID號(hào)SELECT (CASE WHEN EXISTS(SELECT * FROM Han dle b WHERE b.Ha ndlelD = 1 THEN MIN(Ha ndlelD + 1 ELSE 1 END as Ha ndlelD FROM Han dle WHERE NOT Ha ndlelD IN (SELECT a.Ha ndlelD - 1 FROM Handle a一個(gè)SQL語句的問題:行列轉(zhuǎn)換select * from v_temp上面的視圖結(jié)果如下: user_name role_name系統(tǒng)管理員 管理員 feng 管理員
10、feng 般用戶test 一般用戶 想把結(jié)果變成這樣:user_name role_name系統(tǒng)管理員管理員feng管理員,一般用戶test 一般用戶= create table a_test (n ame varchar(20,role2 varchar(20 in sert into a_test value李(,管理員 insert into a_test values張,管理員insert into a_test values張,一般用戶insert into a_test values(常,一般用戶create function join_str(content varchar(10
11、0 returns varchar(2000 as begi n declare str varchar(2000 set str= select str=str+,+rtrim(role2 from a_test where n ame=c ontent select str=right(str,le n(str-1 return str end go -調(diào)用: selectn ame,dbo.join_str( name role2 from a_test group by n ame -select disti net name,dbo.uf_test( name from a_tes快
12、速比較結(jié)構(gòu)相同的兩表 結(jié)構(gòu)相同的兩表,一表 有記錄3萬條左右,一表有記錄2萬條左右,我怎樣快速查找兩表的不同記錄? = 給你一個(gè)測試方法,從 northwind 中的 orders表取數(shù)據(jù)。 select * into n1 from orders select * into n2 from orders select * from n1select * from n2 -添加主鍵,然后修改 n1中若干字段的若干條 alter table n1 add con stra int pk_n1d primary key (OrderlD alter table n2 add con stra in
13、t pk_n 2_id primary key (OrderID select OrderID from (select * from n1 union select * from n2 a group by OrderlD having count(* 1應(yīng)該可以,而且將不同的記錄的ID顯示出來。下面的 適用于雙方記錄一樣的情況,select * from n1 where orderid in (select OrderlD from(select * from n1 union select * from n2 a group by OrderlD hav ing coun t(* 1
14、至于雙 方互不存在的記錄是比較好處理的-刪除n1,n2中若干條記錄delete from n1 whereorderlD in (10728,10730 delete from n2 where orderlD in (11000,11001-*雙方都有該記錄去卩不完全相同 select * from n1 where orderid in (select OrderlD from (select * from n1 union select * from n2 a group by OrderlD having count(* 1 union -n2 中存 在但在 n1 中不存的在 1072
15、8,10730 select * from n1 where OrderlD not in (select OrderlD from n2 union -n1 中存在但在 n2 中不存的在 11000,11001 select * from n2 where OrderlD not in (select OrderlD from n1 四種方法取表里 n 至U m 條紀(jì)錄:1. select top m * into 臨時(shí)表(或表變量 from table name orderby columnname -將 top m 筆插入 set rowcount n select * from 表變量
16、 order by colu mnn ame desc 2. select top n * from (select top m * from table name order by colu mnn ame a order by colu mnn ame desc 3 口 果 table name 里沒有其他 ide ntity 列,那 么: select ide ntity(i nt id0,* into #temp from table name 取 n 至U m 條的語句為: select * from #temp where id0 =n and id0 1 exec(sql ope
17、 n cur_rows fetch cur_rows into id,max while fetch_status=0 beg in select max = max -1 set rowco unt max select type = xtype from syscolu mns where id=object_id(t_ name and n ame=f_key if type=56 select sql = delete from +t_name+ where + f_key+ = + id if type=167 select sql = delete from +t_name+ wh
18、ere + f_key+ = + id + exec(sql fetch cur_rows into id,max end close cur_rows deallocate cur_rows set rowco unt 0 end select * from systypes select * from syscolu mns where id = object_id(a_dis查詢數(shù) 據(jù)的最大排序問題(只能用一條語句寫)CREATE TABLE hard (qu char (11 ,cochar (11 ,je numeric(3, 0 in sert into hard values (
19、A,1,3 in sert into hard values (A,2,4 insert into hard values (A,4,2 in sert into hard values (A,6,9 in sert into hard values (B,1,4 insert into hard values (B,2,5 insert into hard values (B,3,6 insert into hard values (C,3,4 in sert into hard values (C,6,7 in sert into hard values (C,2,要求查詢 出來的結(jié)果如下
20、:qu co jeA 6 9 A 2 4 B 3 6 B 2 5 C 6 7 C 3 4就是要按qu分組,每組中取je最大的前2位!而且只能用一句sql語句! ! select * from hard a where je in (select top 2 je from hard b where a.qu=b.qu order by je 求刪除重復(fù)記錄的sql語句?怎樣把具有相同字段的紀(jì)錄刪除,只留下一條。例如,表test里有id,name字段如果有name相同的記錄只留下 一條,其余的刪除。name的內(nèi)容不定,相同的記錄數(shù)不定。有沒有這樣的sql語句? = A: 一個(gè)完整的解決方案:將重
21、復(fù)的記錄記入 temp1 表:select 標(biāo)志字段 id,count(* into temp1 from 表名group by 標(biāo)志 字段 id having count(*1 2、將不重復(fù)的記錄記入 temp1 表:insert temp1 select 標(biāo) 志字段 id,count(* from 表名group by 標(biāo)志字段 id having count(*=1 3、作一個(gè)包 含所有不重復(fù)記錄的表:select * into temp2 from 表名where標(biāo)志字段id in (select標(biāo)志字段id from temp1 4、刪除重復(fù)表:delete 表名5、恢復(fù)表:inse
22、rt 表名 select * from temp2 6、刪除臨時(shí)表:drop table temp1 drop table temp2B: create table a_dist(id in t, name varchar(20 in sert into a_dist values(1,abc insert into a_dist values(1,abc insert into a_dist values(1,abc in sert into a_dist values(1,abc exec up_disti net a_dist,id select * from a_dist create
23、 procedure up_disti nct(t_n ame varchar(30,f_key varchar(30 - f_key 表示是分組字段,即主鍵字段 as begin declare max integer,id varchar(30 ,sql varchar(7999 ,type in teger select sql = declare cur_rows cursor for select +f_key+ ,co un t(* from +t_ name + group by +f_key + hav ing coun t(* 1 exec(sql ope n cur_row
24、s fetch cur_rows into id,max while fetch_status=0 beg in select max = max -1 set rowco unt max select type = xtype from syscolu mns where id=object_id(t_ name and n ame=f_key if type=56 select sql = delete from +t_name+ where + f_key+ = + id if type=167 select sql = delete from +t_name+ where + f_ke
25、y+ = + id + exec(sql fetch cur_rows into id,max end close cur_rows deallocate cur_rows set rowco unt 0 end select * from systypes select * from syscolumns where id = object_id(a_dis行歹U轉(zhuǎn)換-普通 彳假設(shè)有 張學(xué)生成績表(CJ如下Name Subject Result張三語文80張三數(shù)學(xué)90張三物理85 李四語文85李四數(shù)學(xué)92李四物理82想變成姓名語文數(shù)學(xué)物理張三80 90 85 李四 85 92 82 decl
26、are sql varchar(4000 set sql = select Name select sql = sql + ,sum(case Subject when ”+Subject+” then Result end +Subject+ from (select distinet Subject from CJ as a select sql = sql+ from test group by n ame exec(s行歹U轉(zhuǎn) 換-合并有表 A, id pid 1 1 1 2 1 3 2 1 2 2 3 1 如何化成表 B: id pid 1 1,2,3 2 1,2 3 1 倉U 建一
27、個(gè)合并的函數(shù) create function fmerg(id int returns varchar(8000 as begin declare str varchar(8000 set str= select str=st葉,+cast(pid as varchar fro表 A where id=id set str=right(str,le n( str-1 return(str End go -調(diào)用自定義函數(shù)得到結(jié)果 select dist inct id,dbo.fmerg(idfrom表A如何取得一個(gè)數(shù)據(jù)表的所有列名 方法如下:先從SYSTEMOBJECT系統(tǒng) 表中取得數(shù)據(jù)表的S
28、YSTEMID,然后再SYSCOLUMN表中取得該數(shù)據(jù)表的所有列 名。SQL 語句如下: declare objid in t, objn ame char(40 set objn ame =table name select objid = id from sysobjects where id = object_id( objn ame select Colu mn_n ame = n ame from syscolu mns where id = objid order by colic或 SELECT* FROM INFORMATION_SCHEMA.COLUMNS WHERE TAB
29、LE_NAME =users 通 過SQL語句來更改用戶的密碼 修改別人的,需要sysadmin role EXEC sp_password NULL, newpassword, User如果帳號(hào)為 SA 執(zhí)行 EXEC sp_password NULL,n ewpassword, sa怎么判斷出一個(gè)表的哪些字段不允許為空?selectCOLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where IS_NULLABLE=NO and TABLE_NAME=table name 如何在數(shù)據(jù)庫里找到含有相同 字段的表? a.查已知列名的情況 SELECT b.
30、name as TableName, as columnname From syscolumns a INNER JOIN sysobjects b ON a.id=b.id AND b.type=U AND =你的字段名字未知列名查所有在不同表出現(xiàn)過的列名 Select o. name As table name,s1. name As colu mnn ame From syscolu mns s1, sysobjects o Where s1.id = o.id And o.type = U And Exists ( Select 1 From syscolumn
31、s s2 Where = And s1.id s2.i(查詢第 xxx 行數(shù)據(jù) 假設(shè) id 是主鍵:select * from (select top xxx * from yourtable aa where not exists(select 1 from (select top xxx-1* from yourtable bb where aa.id=bb.id 如果使用游標(biāo)也是可以的 fetch absolutenumber from cursor_name行數(shù)為絕對(duì)行數(shù) SQL Server日期計(jì)算a. 一個(gè)月的第一 天 SELECT DATEADD(
32、mm, DATEDIFF(mm,O,getdate(, 0 b.本周的星期一 SELECT DATEADD(wk, DATEDIFF(wk,O,getdate(, 0 c. 一年的第一天 SELECT DATEADD(yy, DATEDIFF(yy,O,getdate(, 0 d.季度的第一天 SELECT DATEADD(qq, DATEDIFF(qq,O,getdate(, 0 e.上個(gè)月的最后一天 SELECT dateadd(ms,- 3,DATEADD(mm, DATEDIFF(mm,O,getdate(, 0 f.去年的最后一天 SELECT dateadd(ms,-3,DATEA
33、DD(yy, DATEDIFF(yy,O,getdate(, 0 g.本月的最后一天 SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate(+1,0 h.本月的第一 個(gè)星期一 select DATEADD(wk, DATEDIFF(wk,O, dateadd(dd,6- datepart(day,getdate(,getdate( , 0 本年的最后一天 SELECT dateadd(ms,- 3,DATEADD(yy, DATEDIFF(yy,0,getdate(+1, 0。獲取表結(jié)構(gòu)把sysobjects替換 成 tablename即可
34、SELECT CASE IsNull(I.name, When Then Else * End as IsPK, Object_Name(A.id as t_name, A.name as c_name, IsNull(SubString(M.text, 1,254, aspbc_i nit, T.n ame as F_DataType, CASE lsNull(TYPEPROPERTY(T. name, Scale, ”WHEN The n Cast(A.precas varchar ELSE Cast(A.prec as varchar + , + Cast(A.scale as varchar END as F_Scale, A.is nullable as F_isNullAble FROM Syscolum ns as A JOIN Systypes as T ON (A.
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 生產(chǎn)供水合同范例
- 買房定金協(xié)議合同范例
- 玻璃切割合同范例
- 小區(qū)收購合同范例
- 工程合同變更服務(wù)合同范例
- 用工合同范例保密協(xié)議
- 網(wǎng)絡(luò)車輛抵押合同范例
- 結(jié)婚后財(cái)產(chǎn)分配合同范例
- 養(yǎng)豬用地合同范例
- 經(jīng)銷代理協(xié)議合同范例
- GB 5959.3-2008電熱裝置的安全第3部分:對(duì)感應(yīng)和導(dǎo)電加熱裝置以及感應(yīng)熔煉裝置的特殊要求
- DB32/ 4437-2022 施工場地?fù)P塵排放標(biāo)準(zhǔn)
- 課件:第一章 導(dǎo)論(《現(xiàn)代社會(huì)福利思想》課程)
- 大件運(yùn)輸管理制度范本(3篇)
- 構(gòu)型構(gòu)象分析課件
- 第七章-期權(quán)的組合策略-《金融工程》課件
- 管理學(xué)原理(南大馬工程)
- 《【電力工程中級(jí)職稱評(píng)審工作總結(jié)】電力工程中級(jí)職稱》
- 供熱、供燃?xì)?、通風(fēng)及空調(diào)工程研究生職業(yè)生涯規(guī)劃
- 鐵路專用線管理模式比較
- 隔物灸技術(shù)操作流程圖
評(píng)論
0/150
提交評(píng)論