标签:sqlserver

浏览模式: 标准 | 列表 | 图文
工作笔记

工作笔记

一个句子的迁移,一个粗心:
原mysql代码是:select date_format(from_unixtime(xxx),'%Y-%M') as xxxxx from table group by xxxxx

sql server转换后:
select (select dateadd(ss,xxx,'1970-01-01 08:00:00')) as xxxxx from table group by xxxxx
因为sql server不支持将as 后的内容做聚集,只能:
select dateadd(ss,xxx,'1970-01-01 08:00:00') as xxxxx from table group by (select dateadd(ss,xxx,'1970-01-01 08:00:00'))
但又报错,说是group by 不能使用子查询。

仔细想了想,(select dateadd(ss,xxx,'1970-01-01 08:00:00')) as xxxxx,这个select用来干嘛 ?如果是在查询分析器里,当然是要用select,因为要输出嘛,但是在sql语句中。。。
不需要啊。

记录一下。

php freetds 处理sqlserver大字段的问题

php freetds 处理sqlserver大字段的问题

freetds连接sqlserver是没问题,在连接的时候dsn用的是dblib,而不是mssql。
连接和普通的查询都没有问题,但一旦涉及到大字段,你就会发现问题多多了。比如你的字段类型是nvarchar(max),你在处理的时候你会发现,返回结果都是空。
我晶啊。看来php连接sqlserver还是用windows自己的方案吧:sqlsrv这个组件,但它只能在windows下面使用。

太纠结了

Persist Security InFso=true 是什么意思

Persist Security InFso=true 是什么意思

Conn2.Open "Provider=SQLOLEDB.1;Persist Security InFso=true;Data Source=192.168.1.2;Initial Catalog=xxxxx;User ID=sa;Password=123456;"

一些参考信息:
Integrated Security默认值是False,此时需要提供Uid和Pwd,即将以Sql Server 用户身份登陆数据库;如果设置为True,Yes 或 SSPI,这不能出现Uid和Pwd,将以Windows用户省份登陆数据库。强烈推荐用后一种形式,安全性更高。

参见
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataSqlClientSqlConnectionClassConnectionStringTopic.asp
--------
http://expert.csdn.net/Expert/topic/2930/2930660.xml?temp=.423382

Persist Security Info 布尔类型,为True时,表明采用集成安全机制;若为False,则表明不采用集成安全机制。

更多资料还是看:http://connectionstrings.com/,还是这个网站好玩。

Microsoft SQL Server Functions (PDO_SQLSRV)

Introduction

PDO_SQLSRV is a driver that implements the PHP Data Objects (PDO) interface to enable access from PHP to MS SQL Server (starting with SQL Server 2005) and SQL Azure databases.

Installation

The PDO_SQLSRV extension is enabled by adding appropriate DLL file to your PHP extension directory and the corresponding entry to the php.ini file. The PDO_SQLSRV download comes with several driver files. Which driver file you use will depend on 3 factors: the PHP version you are using, whether you are using thread-safe or non-thread-safe PHP, and whether your PHP installation was compiled with the VC6 or VC9 compiler. For example, if you are running PHP 5.3, you are using non-thread-safe PHP, and your PHP installation was compiled with the VC9 compiler, you should use the php_pdo_sqlsrv_53_nts_vc9.dll file. (You should use a non-thread-safe version compiled with the VC9 compiler if you are using IIS as your web server). If you are running PHP 5.2, you are using thread-safe PHP, and your PHP installation was compiled with the VC6 compiler, you should use the php_pdo_sqlsrv_52_ts_vc6.dll file.

For more information about system requirements, see » SQLSRV System Requirements.

The PDO_SQLSRV extension is only compatible with PHP running on Windows. For Linux, see ODBC and » Microsoft's SQL Server ODBC Driver for Linux.

System Requirements (Microsoft Drivers for PHP for SQL Server)

SQL Server 2008 R2

To access data in a SQL Server 2005 or later database using the Microsoft Drivers for PHP for SQL Server, you must have the following components installed on your computer:

  • Supported operating systems for version 3.0 of the driver include:

    • Windows Server 2008 R2 SP1

      Windows Vista SP2

      Windows Server 2008 SP2

      Windows 7 SP1

  • Supported operating systems for version 2.0 of the driver include:

    • Supported operating systems for version 2.0 of the driver include:

    • Windows Server 2003 Service Pack 1

    • Windows XP Service Pack 3

    • Windows Vista Service Pack 1 or later

    • Windows Server 2008

    • Windows Server 2008 R2

    • Windows 7

  • PHP 5. For information about how to download and install the latest stable binaries, visit http://php.net.

    Microsoft Drivers for PHP for SQL Server, version 3.0 requires PHP 5.3.0 or later. If possible, use PHP 5.3.6, or later. The version 2.0 driver works with PHP 5.2.4 or later. If possible, use PHP 5.2.13, or later.

  • A version of the driver file must be in your PHP extension directory. See Driver Versions later in this topic for information about the different driver files. See Loading the Microsoft Drivers for PHP for SQL Server for information on configuring the driver for the PHP runtime.

    (See The Data Center for the Microsoft Drivers for PHP for SQL Server for information on where to download the Microsoft Drivers for PHP for SQL Server.)

  • A Web server. Your Web server must be configured to run PHP. For information about hosting PHP applications with Internet Information Services (IIS) 6.0, see Using FastCGI to Host PHP Applications on IIS 6.0. For information about hosting PHP applications with IIS 7.0, see Using FastCGI to Host PHP Applications on IIS 7.0.

    The Microsoft Drivers for PHP for SQL Server has been tested using IIS 6 and IIS 7 with FastCGI.

  • The x86 version of SQL Server Native Client is required on the computer where PHP is running. If you are using a 64-bit operating system, the x86 version of SQL Server Native Client will be installed with the x64 version of SQL Server Native Client (do not install the x86 version of SQL Server Native Client on an x64 operating system).

    If you are using the SQLSRV driver, sqlsrv_client_info will return information about which version of SQL Server Native Client is being used by the Microsoft Drivers for PHP for SQL Server. If you are using the PDO_SQLSRV driver, you can use PDO::getAttribute to discover the version.

This section lists the drivers that are installed for version 2.0 and version 3.0 of the Microsoft Drivers for PHP for SQL Server. PHP version 5.2 is not supported in version 3.0 of the Microsoft Drivers for PHP for SQL Server.

Follow the installation instructions in Loading the Driver to configure the driver for use with the PHP runtime.

Microsoft Drivers 2.0 for PHP for SQL Server installs several versions of the driver:

Driver file

PHP version

Thread safe?

Use with PHP .dll

php_sqlsrv_53_nts_vc6.dll

php_pdo_sqlsrv_53_nts_vc6.dll

5.3

no

php5.dll

php_sqlsrv_53_nts_vc9.dll

php_pdo_sqlsrv_53_nts_vc9.dll

5.3

no

php5.dll

php_sqlsrv_53_ts_vc6.dll

php_pdo_sqlsrv_53_ts_vc6.dll

5.3

yes

php5ts.dll

php_sqlsrv_53_ts_vc9.dll

php_pdo_sqlsrv_53_ts_vc9.dll

5.3

yes

php5ts.dll

php_sqlsrv_52_nts_vc6.dll

php_pdo_sqlsrv_52_nts_vc6.dll

5.2

no

php5.dll

php_sqlsrv_52_ts_vc6.dll

php_pdo_sqlsrv_52_ts_vc6.dll

5.2

yes

php5ts.dll

If the name of the driver file contains "vc9", it should be used with a PHP version compiled with Visual C++ 9.0.

Microsoft Drivers 3.0 for PHP for SQL Server installs several versions of the driver:

Driver file

PHP version

Thread safe?

Use with PHP .dll

php_sqlsrv_53_nts.dll

php_pdo_sqlsrv_53_nts.dll

5.3

no

php5.dll

php_sqlsrv_53_ts.dll

php_pdo_sqlsrv_53_ts.dll

5.3

yes

php5ts.dll

备份:sqlserver to mysql

备份:sqlserver to mysql

从sqlserver转到mysql上相对还是比较方便的,外面的工具层出不穷,比如dbconverter之类的,可惜它是商业版,而且,15M左右,还TNND没有破解版。
问了一下google和百度,最终度娘没有帮我解决,而且谷哥告诉我答案了:http://www.cnblogs.com/andrew-blog/archive/2011/12/03/SQL_mss2sql.html
然后下载到本地。做个备份,以后可以直接用
下载:70257e20f62afe55d853909b8c9eee4a
只有1.3M哦,非常好用,昨天帮一个朋友转数据的时候,只花了大约半小时左右转换了将近1G左右的数据。

由于用法简单就不多做介绍了,sqlserver的登录可以直接选系统登录,而不需要输入密码,你懂的。甚至可以支持远程导入,黑黑。。。当然这受限于网络带宽的传输了。
如果还不会,就看我上面的URL吧,里面有图文教学,不过我相信,即使没有教学,也是下一步下一步就会搞的。

常用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语句精华

Microsoft Web Platform Installer (Release Candidate)

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.

惊心动魄的SQL BUG

惊心动魄的SQL BUG

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

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

然后有人回复为:

我在Sql2000上测试,确实有这个问题。
有意思的问题,在我的management studio里面查询,也会有这个问题,要好好地研究一下。
Microsoft SQL Server Management Studio 9.00.1399.00
有楼主说的问题!
确实应该注意一下。
看了表结构才知道,Employees 表里面没有 CategoryID字段。为什么不报错呢。错误被吃掉了。

select * from Products where CategoryID in (select CategoryID from Employees)

select * from Products where CategoryID in (select EmployeeID from Employees)

我比较了一下这两个语句的执行计划,不完全一样。建议lz分析一下执行计划。
因为我还不能完全看懂执行计划。只能看出来两个执行计划是不一样的。呵呵。
在MYSQL里没有试会不会遇到这种情况,我用navicat试是没有这种情况的。不知道其他的会不会有。

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

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