手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表Tag:sqlserver

常用SQL整理

  说明:复制表(只复制结构,源表名:a 新表名:b)
      SQL: select * into b from a where 1<>1  (mysql无法执行

  说明:拷贝表(拷贝数据,源表名:a 目标表名:b)

  SQL: insert into b(a, b, c) select d,e,f from b;

  说明:显示文章、提交人和最后回复时间[这个还是用innerJOIN较好]

  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)[MYSQL已经支持using函数]

  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

  说明:日程安排提前五分钟提醒[MYSQL无getdate函数]

  SQL: select * from 日程安排 where datediff('minute',开始时间,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)

原文来自:精妙SQL语句精华

Tags: sqlserver

Microsoft Web Platform Installer (Release Candidate)

The Web Platform Installer (Web PI) is a simple tool that installs Microsoft's entire Web Platform, including IIS, Visual Web Developer 2008 Express Edition, SQL Server 2008 Express Edition and the .NET Framework. Using the Web Platform Installer’s user interface, you can choose to install either specific products or the entire Microsoft Web Platform onto your computer. The Web PI also helps keep your products up to date by always offering the latest additions to the Web Platform.

New Updates! Now supporting Windows XP and Windows Server 2003, Web PI makes it easy to install and stay up-to-date with the Microsoft Web Platform. This updated release lets you install ASP.NET MVC, Visual Studio Tools for Silverlight, and much more!

System requirements

  • Supported Operating Systems are: Windows Vista RTM, Windows Vista SP1, Windows XP, Windows Server 2003, Windows Server 2008
  • You must have administrator privileges on your computer to run Web Platform Installer
  • .NET 2.0 Framework
  • Supported Architectures: x86 and 64-bit

 

Installation Steps

Install Web PI in 3 easy steps:

  1. Click on the "install now" button on this page to install and launch the Web Platform Installer.
  2. Choose the profile that meets your needs or choose "Your Choice" for full control on you what you install.
  3. Confirm and watch while Web PI downloads and installs what you need.

Once you have completed your platform installation, you can immediately start developing your Web solution. Support for Web Platform Installer can be received by visiting the Web Platform Installer Forum to discuss issues and suggestions.

Tags: microsoft, iis7, virtualstudio, sqlserver

惊心动魄的SQL BUG

刚才打开google reader的时候,突然看到一篇文章,让俺大吃一惊呀。详细内容如下:

原文网址:http://www.cnblogs.com/xinerzhui/archive/2008/07/30/1256648.html
  1.  请谨慎注意这一微软SQLBug  
  2. 刚来博客园,有什么不对,需要改进的地方,欢迎各位同道指出来,谢谢。  
  3. 今天在使用数据库时出现意外操作,将一张表的数据删除了。仔细查看SQL语句,发现问题。将问题类推到Northwind中。我们使用两个表,Employees和Products  
  4. SQL语句如下:  
  5. select * from Products where CategoryID in (select CategoryID from Employees)  
  6. 不看表结构是看不出什么问题的,不清楚的看一下表结构。执行上面的SQL语句,你将能看到所有的产品记录。  
  7. 当你执行上面语句的"select CategoryID from Employees",将会提示一个错误提示:  
  8. 消息 207,级别 16,状态 3,第 1 行  
  9. 列名 'CategoryID' 无效。  
  10. 表Employees中根本就没有CategoryID列,而微软忽略了括号中的错误,执行前面的语句。如果是应用在删除,更新,那后果将是不可现象的。我曾为此付出了代价。  
  11. 这个应该就是微软SQL的BUG吧,我们需要特别注意。  
  12. 在SQL2000企业管理器,SQL2000的查询分析器,SQL2005的Management Studio Express中均有此Bug。   

 

然后有人回复为:

#2楼 2008-07-30 17:21 | 熊呜呜
我在Sql2000上测试,确实有这个问题。  

#3楼 2008-07-30 17:27 | ocean  
  1. 有意思的问题,在我的management studio里面查询,也会有这个问题,要好好地研究一下。  

#7楼 117.22.68.*2008-07-30 17:45 | johnnyshieh [未注册用户]
  1. Microsoft SQL Server Management Studio 9.00.1399.00  
  2. 有楼主说的问题!  
  3. 确实应该注意一下。  
#9楼 2008-07-30 17:55 | 金色海洋(jyk)
  1. 看了表结构才知道,Employees 表里面没有 CategoryID字段。为什么不报错呢。错误被吃掉了。  
  2.   
  3. select * from Products where CategoryID in (select CategoryID from Employees)  
  4.   
  5. select * from Products where CategoryID in (select EmployeeID from Employees)  
  6.   
  7. 我比较了一下这两个语句的执行计划,不完全一样。建议lz分析一下执行计划。  
  8. 因为我还不能完全看懂执行计划。只能看出来两个执行计划是不一样的。呵呵。  
在MYSQL里没有试会不会遇到这种情况,我用navicat试是没有这种情况的。不知道其他的会不会有。

联想起以前写CRUD之类的东西时候,在过滤条件时,自动将空值直接unset掉,然后再拼成SQL,后来被BOBBY严厉批评了一下,确实,查询的时候感觉不出有什么问题,可如果是在UPDATE或者是DELETE的时候呢?本来是有条件的,可突然被unset掉成为没有条件了,那岂不是很恐怖?

借这个机会再次提醒自己,细心、周全、小心

 

Tags: mssql, sqlserver, microsoft, sql, bug

Records:812