sql server2005恢复只有mdf文件的数据库
[ 2008-10-28 23:06:29 | 作者: progame ]
sp_attach_single_file will only work if the file was unattached cleanly.
Steps in the case when this hasn't happened are
1) Move your mdf to safe location.
2) Create new databse of same name, same files, same file location and same file size.
3) Stop SQL server.
4) Swap mdf file of just created DB to your save one.
5) Start SQL. DB will go suspect.
6) ALTER DATABASE <your db> SET EMERGENCY
ALTER DATABASE <your db> SET SINGLE_USER
7) DBCC CHECKDB (<your db>, REPAIR_ALLOW_DATA_LOSS)
8) ALTER DATABASE <your db> SET MULTI_USER
ALTER DATABSE <your db> SET ONLINE
2000下的设置紧急模式的方法不行了, sql 2005是无论如何不允许直接修改系统表, 但是它提供了相关的命令
Steps in the case when this hasn't happened are
1) Move your mdf to safe location.
2) Create new databse of same name, same files, same file location and same file size.
3) Stop SQL server.
4) Swap mdf file of just created DB to your save one.
5) Start SQL. DB will go suspect.
6) ALTER DATABASE <your db> SET EMERGENCY
ALTER DATABASE <your db> SET SINGLE_USER
7) DBCC CHECKDB (<your db>, REPAIR_ALLOW_DATA_LOSS)
8) ALTER DATABASE <your db> SET MULTI_USER
ALTER DATABSE <your db> SET ONLINE
2000下的设置紧急模式的方法不行了, sql 2005是无论如何不允许直接修改系统表, 但是它提供了相关的命令
转: Comparison of different SQL implementations (2) [私人]
[ 2008-07-12 12:39:14 | 作者: progame ]
转: Comparison of different SQL implementations (1) [私人]
[ 2008-07-12 12:38:07 | 作者: progame ]
max_allowed_packet=16M
UTF8支持即可正确显示中文
Incorrectly used table in subquery:
Error 1093 (ER_UPDATE_TABLE_USED)
SQLSTATE = HY000
Message = "You can't specify target table 'x'
for update in FROM clause"
This error occurs in cases such as the following:
UPDATE t1 SET column2 = (SELECT MAX(column1) FROM t1);
You can use a subquery for assignment within an UPDATE statement because subqueries are legal in UPDATE and DELETE statements as well as in SELECT statements. However, you cannot use the same table (in this case, table t1) for both the subquery's FROM clause and the update target.
For transactional storage engines, the failure of a subquery causes the entire statement to fail. For non-
UTF8支持即可正确显示中文
Incorrectly used table in subquery:
Error 1093 (ER_UPDATE_TABLE_USED)
SQLSTATE = HY000
Message = "You can't specify target table 'x'
for update in FROM clause"
This error occurs in cases such as the following:
UPDATE t1 SET column2 = (SELECT MAX(column1) FROM t1);
You can use a subquery for assignment within an UPDATE statement because subqueries are legal in UPDATE and DELETE statements as well as in SELECT statements. However, you cannot use the same table (in this case, table t1) for both the subquery's FROM clause and the update target.
For transactional storage engines, the failure of a subquery causes the entire statement to fail. For non-
Only text pointers are allowed in work tables
[ 2008-06-24 13:30:55 | 作者: progame ]
Server: Msg 8626, Level 16, State 1, Line 1
Only text pointers are allowed in work tables, never text, ntext, or image
columns. The query processor produced a query plan that required a text,
ntext, or image column in a work table.
服务器: 消息 8626,级别 16,状态 1,行 1
在工作表中只允许有 text 指针,而决不允许有 text、ntext 或 image 列。查询处理器生成的查询计划要求在工作表中有 text、ntext 或 image 列。
将此关字段cast可以解决此问题
原因是union的查询内出现了text, ntext, image列, 然后再在外部对任意列order by, 即出现此问题
Only text pointers are allowed in work tables, never text, ntext, or image
columns. The query processor produced a query plan that required a text,
ntext, or image column in a work table.
服务器: 消息 8626,级别 16,状态 1,行 1
在工作表中只允许有 text 指针,而决不允许有 text、ntext 或 image 列。查询处理器生成的查询计划要求在工作表中有 text、ntext 或 image 列。
将此关字段cast可以解决此问题
原因是union的查询内出现了text, ntext, image列, 然后再在外部对任意列order by, 即出现此问题
MySql 中文编码的解决方法以及其它的一些问题
[ 2008-04-23 01:05:59 | 作者: progame ]
如果只是设置数据库和表的字符编码, 用第三方工具是可以正常写入中文的, 但是用MySql .Net Connector写入之后成为乱码
查了一下 应该设置my.ini文件中的默认编码
分别在[MySQL(和PHP搭配之最佳组合)]和[MySQL(和PHP搭配之最佳组合)d]配置段中增加或修改default_charater_set=gb2312
也许在连接开始时用set names可行, 但因为我的连接是不保持的, 所以这个太浪费了, 多了次往返
对于一个连接查询, Mysql可以正确的得到字段类型, 但是子查询的话, 就无法正确了, 返回了string类型, 见鬼
经查询, 得知:
Posted by: Reggie Burnett ()
Date: April 07, 2008 10:39PM
These are known issues. There are whole classes of issues surrounding losing data type. MySQL has issues with temp tables and losing of data type fidelity.
再一搜, 原来这个问题还是在最新的版本中解决了, 于是下载最新版本, 安装, 测试OK, 这样我的关联字段的写法看来不用从子查询变成连接查询了, 主要是连接查询的话需要加大量的表前缀以防止两个表的同名字段冲突
查了一下 应该设置my.ini文件中的默认编码
分别在[MySQL(和PHP搭配之最佳组合)]和[MySQL(和PHP搭配之最佳组合)d]配置段中增加或修改default_charater_set=gb2312
也许在连接开始时用set names可行, 但因为我的连接是不保持的, 所以这个太浪费了, 多了次往返
对于一个连接查询, Mysql可以正确的得到字段类型, 但是子查询的话, 就无法正确了, 返回了string类型, 见鬼
经查询, 得知:
Posted by: Reggie Burnett ()
Date: April 07, 2008 10:39PM
These are known issues. There are whole classes of issues surrounding losing data type. MySQL has issues with temp tables and losing of data type fidelity.
再一搜, 原来这个问题还是在最新的版本中解决了, 于是下载最新版本, 安装, 测试OK, 这样我的关联字段的写法看来不用从子查询变成连接查询了, 主要是连接查询的话需要加大量的表前缀以防止两个表的同名字段冲突
参数化SQL, 性能的调优抑或是杀手?
[ 2007-12-13 00:26:29 | 作者: progame ]
参数化SQL一直以来是提高性能的重要手段, 其优势是显而易见的, 对于大量重复执行的SQL, 如果只是常量(插入值, 更新值, 比对值)不同, 可以重复使用执行计划, 减少了SQL编译时间, 因此简单SQL语句, 我个人认为参数化SQL的性能是不会比存储过程差的
还有很重要的一点, 起码是在SQL SERVER2000里, 非参数化的SQL很容易就导致内存上升, 而缓存的执行计划命中率又低, 因此对数据库的整体性能颇受影响
但是, 见到常量就参数化就一定好么? 看下面例子:
select * from table where 1 = 2
exec sp_executesql N'SELECT * FROM table WHERE @P1 = @P2 ', N'@P1 int, @p2 int', 1,2
看看执行计划就知道了, 后者使用了全表扫描, 原因何在?
因为对于@p1 = @p2, 在进行执行计划分析时, 是根本无法判断是否为假的(其实再智能点还是可以的, 不是真就是假), 所以常量运算的话, 在不使用参数化SQL时是能够被查询优化器先期运行出来再决定执行计划的, 但参数化SQL之后, 通用的执行计划只能干瞪眼了
其实这也是一种通病, 东西做得通用了, 那么对具体的实际应用就要打折扣了
还有很重要的一点, 起码是在SQL SERVER2000里, 非参数化的SQL很容易就导致内存上升, 而缓存的执行计划命中率又低, 因此对数据库的整体性能颇受影响
但是, 见到常量就参数化就一定好么? 看下面例子:
select * from table where 1 = 2
exec sp_executesql N'SELECT * FROM table WHERE @P1 = @P2 ', N'@P1 int, @p2 int', 1,2
看看执行计划就知道了, 后者使用了全表扫描, 原因何在?
因为对于@p1 = @p2, 在进行执行计划分析时, 是根本无法判断是否为假的(其实再智能点还是可以的, 不是真就是假), 所以常量运算的话, 在不使用参数化SQL时是能够被查询优化器先期运行出来再决定执行计划的, 但参数化SQL之后, 通用的执行计划只能干瞪眼了
其实这也是一种通病, 东西做得通用了, 那么对具体的实际应用就要打折扣了
sqlparse.dll 总得让你干点活
[ 2007-08-21 12:28:13 | 作者: progame ]
SqlServer2000是自带了一个sqlparse.dll的 不过这个东西是用来高亮语法的 (晕) 所以它生成的东西类似这样:
\cf1 select\cf \cf5 [\cf \cf1 key\cf \cf5 ]\cf \cf5 ,\cf \cf5 [\cf \cf1 value\cf \cf5 ]\cf \cf1 from\cf \cf10 stConfig\cf
但是我们完全可以用它来做简单的sql analysis, 对应表如下
(先顺便说一下, \cf1...\cf是RTF格式, 表示...用colortable的定义的颜色显示
cf1 keyword
cf2 datatype
cf3 function
cf4 fmtonly 之类
cf5 符号 如 * . [ ] ( ) null
cf6 字符串 有可能是日期
cf7 数字(注意包括top count) 函数内的参数
cf8 comment
cf10 object (select n'abc' 的n会当做object)
par 语句结束
bug: 有多行字符串时分析结果是错误的
\cf1 select\cf \cf5 [\cf \cf1 key\cf \cf5 ]\cf \cf5 ,\cf \cf5 [\cf \cf1 value\cf \cf5 ]\cf \cf1 from\cf \cf10 stConfig\cf
但是我们完全可以用它来做简单的sql analysis, 对应表如下
(先顺便说一下, \cf1...\cf是RTF格式, 表示...用colortable的定义的颜色显示
cf1 keyword
cf2 datatype
cf3 function
cf4 fmtonly 之类
cf5 符号 如 * . [ ] ( ) null
cf6 字符串 有可能是日期
cf7 数字(注意包括top count) 函数内的参数
cf8 comment
cf10 object (select n'abc' 的n会当做object)
par 语句结束
bug: 有多行字符串时分析结果是错误的
小心getdate()
[ 2007-08-16 16:58:49 | 作者: progame ]
在SqlServer里GetDate()被称之为不确定函数, 所以在针对这样的查询时:
select * from table where date > dateadd(month,-1,getdate())
SQLSERVER会认为后面的比较值不确定, 所以不会使用index seek
sql server2005如何未知, 要测试一下才知道
select * from table where date > dateadd(month,-1,getdate())
SQLSERVER会认为后面的比较值不确定, 所以不会使用index seek
sql server2005如何未知, 要测试一下才知道
如何从DataProvider中得到相关信息来生成数据库无关的SQL Command
[ 2007-07-07 23:43:07 | 作者: progame ]
.net 2.0中machine.config中有dataprovider的注册信息, 比如说这样:
<connectionStrings>
<add name="TBPConnectionString" connectionString="Database=tbp;Data Source=localhost;User Id=root;Password=" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
machine.config中对应的provider configiration:
<system.data>
<DbProviderFactories>
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=5.0.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
因为同样的功能, 在sql server中如果是这样的:
select [a], [c] from [t] where [id] = @id
那么在MySql中是这样的:
select `a`,`c` from `t` where `id` = ?id
那么如果我们只是根据web.config中的providerName来生成connection, 又该如何去生成native sql?
因为这个功能是我在AddIN中使用的, 所以我的sqlbroker只能把两个手拢袖筒里干瞪眼了:( 还好, 只是简单的CRUD, 不过就是有过滤, 否则我直接用commandtype = tabledirect再加上dataset就可以搞定了
先得到DbProviderFactory 吧:
DbProviderFactory ...
阅读全文...
<connectionStrings>
<add name="TBPConnectionString" connectionString="Database=tbp;Data Source=localhost;User Id=root;Password=" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
machine.config中对应的provider configiration:
<system.data>
<DbProviderFactories>
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=5.0.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
因为同样的功能, 在sql server中如果是这样的:
select [a], [c] from [t] where [id] = @id
那么在MySql中是这样的:
select `a`,`c` from `t` where `id` = ?id
那么如果我们只是根据web.config中的providerName来生成connection, 又该如何去生成native sql?
因为这个功能是我在AddIN中使用的, 所以我的sqlbroker只能把两个手拢袖筒里干瞪眼了:( 还好, 只是简单的CRUD, 不过就是有过滤, 否则我直接用commandtype = tabledirect再加上dataset就可以搞定了
先得到DbProviderFactory 吧:
DbProviderFactory ...
阅读全文...

