--IF ELSE declare @x int @y int @z int select @x = 1 @y = 2 @z=3 if @x > @y print 'x > y' --打印字符串'x > y' else if @y > @z print 'y > z' else print 'z > y'
--CASE use pangu update employee set e_wage = case when job_level = '1' then e_wage*1.08 when job_level = '2' then e_wage*1.07 when job_level = '3' then e_wage*1.06 else e_wage*1.05 end
--WHILE CONTINUE BREAK declare @x int @y int @c int select @x = 1 @y=1 while @x 3 begin print @x --打印變量x 的值 while @y 3 begin select @c = 100*@x + @y print @c --打印變量c 的值 select @y = @y + 1 end select @x = @x + 1 select @y = 1 end
--WAITFOR --例 等待1 小時(shí)2 分零3 秒后才執(zhí)行SELECT 語句 waitfor delay '01:02:03' select * from employee --例 等到晚上11 點(diǎn)零8 分后才執(zhí)行SELECT 語句 waitfor time '23:08:00' select * from employee
***SELECT***
select *(列名) from table_name(表名) where column_name operator value ex:(宿主) select * from stock_information where stockid = str(nid) stockname = 'str_name' stockname like '% find this %' stockname like '[a-zA-Z]%' --------- ([]指定值的范圍) stockname like '[^F-M]%' --------- (^排除指定范圍) --------- 只能在使用like關(guān)鍵字的where子句中使用通配符) or stockpath = 'stock_path' or stocknumber 1000 and stockindex = 24 not stock*** = 'man' stocknumber between 20 and 100 stocknumber in(10,20,30) order by stockid desc(asc) --------- 排序,desc-降序,asc-升序 order by 1,2 --------- by列號 stockname = (select stockname from stock_information where stockid = 4) --------- 子查詢 --------- 除非能確保內(nèi)層select只返回一個(gè)行的值, --------- 否則應(yīng)在外層where子句中用一個(gè)in限定符 select distinct column_name form table_name --------- distinct指定檢索獨(dú)有的列值,不重復(fù) select stocknumber ,"stocknumber + 10" = stocknumber + 10 from table_name select stockname , "stocknumber" = count(*) from table_name group by stockname --------- group by 將表按行分組,指定列中有相同的值 having count(*) = 2 --------- having選定指定的組
select * from table1, table2 where table1.id *= table2.id -------- 左外部連接,table1中有的而table2中沒有得以null表示 table1.id =* table2.id -------- 右外部連接
select stockname from table1 union [all] ----- union合并查詢結(jié)果集,all-保留重復(fù)行 select stockname from table2
***insert***
insert into table_name (Stock_name,Stock_number) value ("xxx","xxxx") value (select Stockname , Stocknumber from Stock_table2)---value為select語句
----系統(tǒng)函數(shù)---- APP_NAME() --函數(shù)返回當(dāng)前執(zhí)行的應(yīng)用程序的名稱 COALESCE() --函數(shù)返回眾多表達(dá)式中第一個(gè)非NULL 表達(dá)式的值 COL_LENGTH('table_name'>, 'column_name'>) --函數(shù)返回表中指定字段的長度值 COL_NAME(table_id>, column_id>) --函數(shù)返回表中指定字段的名稱即列名 DATALENGTH() --函數(shù)返回?cái)?shù)據(jù)表達(dá)式的數(shù)據(jù)的實(shí)際長度 DB_ID(['database_name']) --函數(shù)返回?cái)?shù)據(jù)庫的編號 DB_NAME(database_id) --函數(shù)返回?cái)?shù)據(jù)庫的名稱 HOST_ID() --函數(shù)返回服務(wù)器端計(jì)算機(jī)的名稱 HOST_NAME() --函數(shù)返回服務(wù)器端計(jì)算機(jī)的名稱 IDENTITY(data_type>[, seed increment]) [AS column_name]) --IDENTITY() 函數(shù)只在SELECT INTO 語句中使用用于插入一個(gè)identity column列到新表中 /*select identity(int, 1, 1) as column_name into newtable from oldtable*/ ISDATE() --函數(shù)判斷所給定的表達(dá)式是否為合理日期 ISNULL(check_expression>, replacement_value>) --函數(shù)將表達(dá)式中的NULL 值用指定值替換 ISNUMERIC() --函數(shù)判斷所給定的表達(dá)式是否為合理的數(shù)值 NEWID() --函數(shù)返回一個(gè)UNIQUEIDENTIFIER 類型的數(shù)值 NULLIF(expression1>, expression2>) --NULLIF 函數(shù)在expression1 與expression2 相等時(shí)返回NULL 值若不相等時(shí)則返回expression1 的值 精妙SQL語句 說明:復(fù)制表(只復(fù)制結(jié)構(gòu),源表名:a 新表名:b) SQL: select * into b from a where 1>1 說明:拷貝表(拷貝數(shù)據(jù),源表名:a 目標(biāo)表名:b) SQL: insert into b(a, b, c) select d,e,f from b; 說明:顯示文章、提交人和最后回復(fù)時(shí)間 SQL: select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b 說明:外連接查詢(表名1:a 表名2:b) SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c 說明:日程安排提前五分鐘提醒 SQL: select * from 日程安排 where datediff('minute',f開始時(shí)間,getdate())>5
說明:兩張關(guān)聯(lián)表,刪除主表中已經(jīng)在副表中沒有的信息 SQL: 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 TABLE1, (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, 'YYYY/MM') || '/01','YYYY/MM/DD') - 1, 'YYYY/MM') ) Y, WHERE X.NUM = Y.NUM (+) AND X.INBOUND_QTY + NVL(Y.STOCK_ONHAND,0) > X.STOCK_ONHAND ) B WHERE A.NUM = B.NUM 說明:-- SQL: select * from studentinfo where not exists(select * from student where studentinfo.id=student.id) and 系名稱='"strdepartmentname"' and 專業(yè)名稱='"strprofessionname"' order by 性別,生源地,高考總成績 說明: 從數(shù)據(jù)庫中去一年的各單位電話費(fèi)統(tǒng)計(jì)(電話費(fèi)定額賀電化肥清單兩個(gè)表來源) SQL: SELECT a.userper, a.tel, a.standfee, TO_CHAR(a.telfeedate, 'yyyy') AS telyear, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '01', a.factration)) AS JAN, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '02', a.factration)) AS FRI, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '03', a.factration)) AS MAR, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '04', a.factration)) AS APR, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '05', a.factration)) AS MAY, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '06', a.factration)) AS JUE, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '07', a.factration)) AS JUL, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '08', a.factration)) AS AGU, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '09', a.factration)) AS SEP, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '10', a.factration)) AS OCT, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '11', a.factration)) AS NOV, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '12', a.factration)) AS DEC FROM (SELECT a.userper, a.tel, a.standfee, b.telfeedate, b.factration FROM TELFEESTAND a, TELFEE b WHERE a.tel = b.telfax) a GROUP BY a.userper, a.tel, a.standfee, TO_CHAR(a.telfeedate, 'yyyy') 說明:四表聯(lián)查問題: SQL: select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where ..... 說明:得到表中最小的未使用的ID號 SQL: SELECT (CASE WHEN EXISTS(SELECT * FROM Handle b WHERE b.HandleID = 1) THEN MIN(HandleID) + 1 ELSE 1 END) as HandleID FROM Handle WHERE NOT HandleID IN (SELECT a.HandleID - 1 FROM Handle a)