存档

2010年 的存档,文章数:148

应一个朋友的要求,贴上收藏的SQL常用分页的办法~~

表中主键必须为标识列,[ID] int IDENTITY (1,1)

1.分页方案一:(利用Not In和SELECT TOP分页)

语句形式: 
SELECT TOP 页记录数量 *
FROM 表名
WHERE (ID NOT IN
  (SELECT TOP (每页行数*(页数-1)) ID
  FROM 表名
  ORDER BY ID))
  ORDER BY ID
//自己还可以加上一些查询条件

例:
select top 2 *
from Sys_Material_Type
where (MT_ID not in
    (select top (2*(3-1)) MT_ID from Sys_Material_Type  order by MT_ID))
order by MT_ID

2.分页方案二:(利用ID大于多少和SELECT TOP分页)

语句形式:
SELECT TOP 每页记录数量 *
FROM 表名
WHERE (ID >
          (SELECT MAX(id)
    FROM (SELECT TOP 每页行数*页数 id  FROM 表
          ORDER BY id) AS T)
      )
ORDER BY ID

例:
SELECT TOP 2 *
FROM Sys_Material_Type
WHERE (MT_ID >
          (SELECT MAX(MT_ID)
          FROM (SELECT TOP (2*(3-1)) MT_ID
                FROM Sys_Material_Type
                ORDER BY MT_ID) AS T))
ORDER BY MT_ID

3.分页方案三:(利用SQL的游标存储过程分页)
create  procedure SqlPager
@sqlstr nvarchar(4000), --查询字符串
@currentpage int, --第N页
@pagesize int --每页行数
as
set nocount on
declare @P1 int, --P1是游标的id
@rowcount int
exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1, @rowcount=@rowcount output
select ceiling(1.0*@rowcount/@pagesize) as 总页数--,@rowcount as 总行数,@currentpage as 当前页
set @currentpage=(@currentpage-1)*@pagesize+1
exec sp_cursorfetch @P1,16,@currentpage,@pagesize
exec sp_cursorclose @P1
set nocount off

4.总结:
其它的方案:如果没有主键,可以用临时表,也可以用方案三做,但是效率会低。
建议优化的时候,加上主键和索引,查询效率会提高。

通过SQL 查询分析器,显示比较:我的结论是:
分页方案二:(利用ID大于多少和SELECT TOP分页)效率最高,需要拼接SQL语句
分页方案一:(利用Not In和SELECT TOP分页)  效率次之,需要拼接SQL语句
分页方案三:(利用SQL的游标存储过程分页)    效率最差,但是最为通用

今天在测试sybase服务器上配置encryption加密功能,测试一下数据加密的新特性。

sp_configure "enable encrypted columns",1

go

之后errorlog中显示:

======================================================================

00:00000:00026:2010/01/18 11:09:51.56 kernel  SySAM: Checked out license for 1 ASE_ENCRYPTION (2009.1231/permanent/19B3 47BC 0B7E 8DC3).
00:00000:00026:2010/01/18 11:09:51.56 kernel  Checked out license ASE_ENCRYPTION
00:00000:00026:2010/01/18 11:09:51.67 kernel  SySAM: Failed to obtain 2 license(s) for ASE_ENCRYPTION feature from license file(s) or server(s).
00:00000:00026:2010/01/18 11:09:51.67 kernel  SySAM: ASE requires 2 'per-CPU' licenses for use on this machine but only 1 could be obtained. Check whether additional licenses are available at the Sybase Product Download Center; or check whether the licensing terms allow you to partition the machine so that ASE can only use 1 CPU(s).
00:00000:00026:2010/01/18 11:09:51.67 kernel  SySAM: License feature name:  ASE_ENCRYPTION
00:00000:00026:2010/01/18 11:09:51.67 kernel  SySAM: License search path:   D:\sybase\\SYSAM-2_0\licenses\SYBASE_ASE_DE.lic
00:00000:00026:2010/01/18 11:09:51.67 kernel  SySAM: FLEXnet Licensing error:-73,125
00:00000:00026:2010/01/18 11:09:51.67 kernel  SySAM: For further information, refer to the Sybase Software Asset Management website at http://www.sybase.com/sysam
00:00000:00026:2010/01/18 11:09:51.73 server  Configuration file 'D:\sybase\TEST.cfg' has been written and the previous version has been renamed to 'D:\sybase/TEST.057'.
00:00000:00026:2010/01/18 11:09:51.73 server  The configuration option 'enable encrypted columns' has been changed by 'sa' from '0' to '1'.

======================================================================

重启了一下sybase服务器,就启动不起来了。

errorlog如下:

0:00000:00000:2010/01/18 15:11:26.37 kernel  SySAM: Using licenses from: D:\sybase\\SYSAM-2_0\licenses
00:00000:00000:2010/01/18 15:11:27.31 kernel  SySAM: Checked out license for 1 ASE_CORE (2009.1231/permanent/19B3 47BC 0B7E 8DC3).
00:00000:00000:2010/01/18 15:11:27.31 kernel  This product is licensed to: ASE Developer Edition - For Development and Test use only
00:00000:00000:2010/01/18 15:11:27.31 kernel  Checked out license ASE_CORE
00:00000:00000:2010/01/18 15:11:27.31 kernel  Adaptive Server Enterprise (Developer Edition)
00:00000:00000:2010/01/18 15:11:27.48 kernel  Using config area from primary master device.
00:00000:00000:2010/01/18 15:11:27.53 kernel  Warning: Using default file 'D:\sybase\TEST.cfg' since a configuration file was not specified. Specify a configuration file name in the RUNSERVER file to avoid this message.
00:00000:00000:2010/01/18 15:11:27.53 server  Illegal value '1' specified for configuration option 'max online engines'. The legal values are between '2' and '2'.
00:00000:00000:2010/01/18 15:11:27.53 server  The value specified for configuration option 'max online engines' '1' cannot be less than the sum of the configuration options 'number of engines at startup' and 'max online Q engines'. The current sum is '2'.
00:00000:00000:2010/01/18 15:11:27.53 server  'max online engines' has been modified from '1' to '2' by the verification routine.
00:00000:00000:2010/01/18 15:11:27.56 kernel  SySAM: Checked out license for 1 ASE_PARTITIONS (2009.1231/permanent/19B3 47BC 0B7E 8DC3).
00:00000:00000:2010/01/18 15:11:27.56 kernel  Checked out license ASE_PARTITIONS
00:00000:00000:2010/01/18 15:11:27.59 kernel  SySAM: Checked out license for 1 ASE_ENCRYPTION (2009.1231/permanent/19B3 47BC 0B7E 8DC3).
00:00000:00000:2010/01/18 15:11:27.59 kernel  Checked out license ASE_ENCRYPTION
00:00000:00000:2010/01/18 15:11:27.59 server  The value of the 'max memory' parameter (39936) defined in the configuration file is not high enough to set the other parameter values specified in the configuration file. 'max memory' should be greater than the 'total logical memory' '41568'.

======================================================================

暂时忽略number of engines at startup 和 max online engines 这两个参数不一致的情况。因为我的本是双核cpu。

关于内存配置,我没有调整max memory,在.cfg文件中一致都是DEFAULT。现在启动的时候报内存39936不够用,需要41568bytes内存。说明sybase中的 encryption选项是需要消耗一定的内存的, 另外用加密这个选项不知道对性能的影响有多大。想想每次写入数据库设备的数据都要在内存中进行加密处理了之后才写入磁盘。这给处理器有增加了负担。对性能的影响待以后有时间体会一下。

————————————————————————————————————
——— 本文为andkylee个人原创,请在尊重作者劳动成果的前提下进行转载;
——— 转载务必注明原始出处 : http://www.dbainfo.net
——— 关键字: ASE 15.0.3 加密 内存 encryption memory
————————————————————————————————————

很郁闷,对360很失望。上午在用360优化电脑的时候,平时我是不用什么优化大师或者什么兔子、360来倒腾电脑的。因为:我觉得也不知道软件具体删除了哪些文件,误删了文件就麻烦了。果不其然,360把我的服务信息给搞没了。

在用360软件管家中的开机加速后,我的一些服务不能自动启动了。

从其他sybase服务器上导出项目HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SYBSQL_TEST 的信息,用记事本打开,如下的信息:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SYBSQL_TEST ]
"Type"=dword:00000010
"Start"=dword:00000003
"ErrorControl"=dword:00000001
"ImagePath"=hex(2):64,00,3a,00,5c,00,73,00,79,00,62,00,61,00,73,00,65,00,5c,00,\
  41,00,53,00,45,00,2d,00,31,00,32,00,5f,00,35,00,5c,00,62,00,69,00,6e,00,5c,\
  00,73,00,71,00,6c,00,73,00,72,00,76,00,72,00,2e,00,65,00,78,00,65,00,20,00,\
  2d,00,73,00,4c,00,49,00,55,00,5a,00,48,00,45,00,4e,00,46,00,55,00,20,00,2d,\
  00,43,00,00,00

"DisplayName"="Sybase SQLServer _ TEST"
"ObjectName"="LocalSystem"

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SYBSQL_TEST \Enum]
"0"="Root\\LEGACY_SYBSQL_TEST \\0000"
"Count"=dword:00000001
"NextInstance"=dword:00000001

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SYBSQL_TEST \Performance]
"Library"="d:\\sybase\\ASE-15_0\\dll\\sybperf15.dll"
"Open"="sybperf_open"
"Collect"="sybperf_collect"
"Close"="sybperf_close"
"Last Counter"=dword:00000ea2
"Last Help"=dword:00000ea3
"First Counter"=dword:00000a4a
"First Help"=dword:00000a4b
"inifile_version"="15"
"WbemAdapFileSignature"=hex:eb,32,0b,37,22,b5,36,f0,55,9b,2f,92,7a,2f,eb,3d
"WbemAdapFileTime"=hex:1e,bd,1d,f4,f4,99,c8,01
"WbemAdapFileSize"=dword:00009600
"WbemAdapStatus"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SYBSQL_TEST \Security]
"Security"=hex:01,00,14,80,90,00,00,00,9c,00,00,00,14,00,00,00,30,00,00,00,02,\
  00,1c,00,01,00,00,00,02,80,14,00,ff,01,0f,00,01,01,00,00,00,00,00,01,00,00,\
  00,00,02,00,60,00,04,00,00,00,00,00,14,00,fd,01,02,00,01,01,00,00,00,00,00,\
  05,12,00,00,00,00,00,18,00,ff,01,0f,00,01,02,00,00,00,00,00,05,20,00,00,00,\
  20,02,00,00,00,00,14,00,8d,01,02,00,01,01,00,00,00,00,00,05,0b,00,00,00,00,\
  00,18,00,fd,01,02,00,01,02,00,00,00,00,00,05,20,00,00,00,23,02,00,00,01,01,\
  00,00,00,00,00,05,12,00,00,00,01,01,00,00,00,00,00,05,12,00,00,00

请将红色的TEST替换为你的sybase服务器名称即servername。如果你是导出的12.5版本的注册表信息,请修改对应的Library值(红色标记)。 蓝色标记的ImagePath是十六进制的,请在注册表内修改,就是系统启动的程序的路径。

修改完之后,就可以启动sybase服务了。在服务中启动或者sc start sybsql_servername   都是可以的!

其它类似backup,monitor,xpserver,和主服务类似。不再赘述。

但是很郁闷,我的sql server database analysis agent服务不知道该如何添加注册表信息了。估计只能通过重新安装程序来解决了。

到注册表了查看,关于SYBBCK_TEST_BS ,SYBMON_TEST_MS ,    SYBSQL_TEST ,SYBXPS_TEST_XP,以及 sql server database analysis agent 服务的信息全不见了。 360太可恨了,幸亏本人懂sybase的注册表配置信息,不然真的重装一遍sybase系统了。

————————————————————————————————————
——— 本文为andkylee个人原创,请在尊重作者劳动成果的前提下进行转载;
——— 转载务必注明原始出处 : http://www.dbainfo.net
——— 关键字: sybase 注册表 360 服务 恢复
————————————————————————————————————

将Sybase的字符集调整为支持简体中文的cp936字符集。

 步骤如下:

(这里假设SYBASE的安装路径为c:\sybase) 
1.c:\>cd \sybase\charsets\cp936
2.c:\sybase\charsets\cp936> charset -U用户名 -P密码 -S数据库服务器名称 binary.srt cp936

更改默认字符集为cp936
3.在SQL环境中
1>select name,id from syscharsets(会列出字符集对应的id号)
2>go
找到name为cp936对应的id(假设为171)
.1>sp_configure "default character set id",171
2>go
5.重启server两次
(注:第一次启动后,server会自动宕掉,需要第二次重启后才能使用)

启动之后,要清空日志。应该是sybase日志恢复时不会转换字符集吧,所以先前的日志没有作用了。要及时清除掉!
清空提交日志dump tran dbname with  truncate_only

=====================================================================

SYBASE使用utf8字符集出现乱码问题的解决

于国际化的原因,现在使用sybase15.0.3字符集utf8作为默认字符集,现在客户端与服务端一致都配置成为 locale=default,us_english,utf8 ,发现使用客户端写入数据后,汉字全为乱码。在isql下用iso_1,cp850字符集连接上,查询已有数据汉字全为乱码。

解决的办法:

将客户端改为cp936,或者在isql连接的时候指定字符集为简体中文字符集cp936即-Jcp936。

问题解决!

ps:在isql查看客户端字符集为:select @@client_csname
查看服务器的默认排序顺序:sp_configure "default sortorder"

————————————————————————————————————
——— 本文为andkylee个人原创,请在尊重作者劳动成果的前提下进行转载;
——— 转载务必注明原始出处 : http://www.dbainfo.net
——— 关键字:  ASE 字符集 简体中文  CP936 UTF8 乱码
————————————————————————————————————