--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 小时2 分零3 秒后才执行SELECT 语句 waitfor delay '01:02:03' select * from employee --例 等到晚上11 点零8 分后才执行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关键字的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) --------- 子查询 --------- 除非能确保内层select只返回一个行的值, --------- 否则应在外层where子句中用一个in限定符 select distinct column_name form table_name --------- distinct指定检索独有的列值,不重复 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合并查询结果集,all-保留重复行 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语句
----系统函数---- APP_NAME() --函数返回当前执行的应用程序的名称 COALESCE() --函数返回众多表达式中第一个非NULL 表达式的值 COL_LENGTH('table_name'>, 'column_name'>) --函数返回表中指定字段的长度值 COL_NAME(table_id>, column_id>) --函数返回表中指定字段的名称即列名 DATALENGTH() --函数返回数据表达式的数据的实际长度 DB_ID(['database_name']) --函数返回数据库的编号 DB_NAME(database_id) --函数返回数据库的名称 HOST_ID() --函数返回服务器端计算机的名称 HOST_NAME() --函数返回服务器端计算机的名称 IDENTITY(data_type>[, seed increment]) [AS column_name]) --IDENTITY() 函数只在SELECT INTO 语句中使用用于插入一个identity column列到新表中 /*select identity(int, 1, 1) as column_name into newtable from oldtable*/ ISDATE() --函数判断所给定的表达式是否为合理日期 ISNULL(check_expression>, replacement_value>) --函数将表达式中的NULL 值用指定值替换 ISNUMERIC() --函数判断所给定的表达式是否为合理的数值 NEWID() --函数返回一个UNIQUEIDENTIFIER 类型的数值 NULLIF(expression1>, expression2>) --NULLIF 函数在expression1 与expression2 相等时返回NULL 值若不相等时则返回expression1 的值 精妙SQL语句 说明:复制表(只复制结构,源表名:a 新表名:b) SQL: select * into b from a where 1>1 说明:拷贝表(拷贝数据,源表名:a 目标表名:b) SQL: insert into b(a, b, c) select d,e,f from b; 说明:显示文章、提交人和最后回复时间 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开始时间,getdate())>5
说明:两张关联表,删除主表中已经在副表中没有的信息 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 专业名称='"strprofessionname"' order by 性别,生源地,高考总成绩 说明: 从数据库中去一年的各单位电话费统计(电话费定额贺电化肥清单两个表来源) 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') 说明:四表联查问题: 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)