08 20, 2008

[数据库] ::[ZZ]表值函数增强功能

以前我们使用表值函数,只能用select * from 表值函数 的形式,如果我们想和别的表连接就做不到了.现在2005新增的{CROSS | OUTER} APPLY运算符可以让我们轻松的做到这一点.下边我就以一个例子来说明.

表1(t):sail cmd
9258 LBQR|LB
92587 A|19|1


想变成如下的形式:

sail cmd
9258 LBQR
9258 LB
92587 A
92587 19
92587 1
-- 步骤1:表值函数

create function dbo.fn_t(@sail int,@cmd varchar(1000))
returns @tb_t table(sail int,cmd varchar(1000))
as
begin
while(charindex('|',@cmd)>0) -- 循环取得列表值,并插入@tb_t表里边.
begin
insert @tb_t select @sail,left(@cmd,charindex('|',@cmd)-1)
set @cmd=stuff(@cmd,1,charindex('|',@cmd),'')
end
insert @tb_t select @sail,@cmd -- 插入最后一个值
return
end
Go

-- 步骤2:查询

select t.sail,b.cmd
from t
cross apply dbo.fn_t(t.sail,t.cmd) b
order by t.sail

07 17, 2008

[数据库] ::通过SQL杀系统用户

系统登陆不上或死锁时,可用SQL杀掉用户,相应进程也将终止。

1. SQL连接
2. 列用户
exec master.dbo.xp_cmdshell 'query user'
3. 杀用户
exec master.dbo.xp_cmdshell 'logoff 用户号'

如果xp_cmdshell功能未打开,则先使用以下语句将xp_cmdshell功能打开:
EXEC sp_configure 'show advanced options', 1
RECONFIGURE;
EXEC sp_configure 'xp_cmdshell', 1;
RECONFIGURE;

07 15, 2008

[数据库] ::建立链接服务器及报错的处理

一、建立链接服务器

--简历链接服务器
EXEC sp_addlinkedserver @server='link_server',
@srvproduct='',
@provider='SQLOLEDB',
@datasrc='10.1.1.1'
Go

--建立登陆凭证
EXEC sp_addlinkedsrvlogin 'link_server', 'false', NULL, 'myname', 'mypassword'
Go

--查询
select * from link_server.comm.dbo.d_user
Go

二、64位SQL上建立32位SQL Server为链接服务器时报错7311
[FROM]http://blog.csdn.net/hb_gx/archive/2007/10/15/1826436.aspx

error 7311:
链接服务器 OLE DB 访问接口 "SQLNCLI" 返回了消息 在该服务器上找不到完成该操作所需的存储过程。 请与系统管理员联系。

原因:
因为32位的 Sql2000 升级到sp3或者sp4以后还需要手工执行补丁包内的 Instcat.sql 脚本才能连接上64位的 Sql2005 ,执行 cmd 进入命令行模式,输入如下语句,完成安装,这下 LinkServer 能够正常连接。

在32位机上cmd命令行下输入 : osql -U myname -P mypassword -S 127.0.0.1 -i C:\sql2ksp4\install\instcat.sql

-U:Sql2000的登陆名,要是具备管理权限的,最好是使用sa用户
-P:密码
-S:服务器名或IP地址
-i:脚本所在的路径及名称,注意大小写。

参考MSDN : http://support.microsoft.com/kb/906954/zh-tw

06 23, 2008

[数据库] ::分区表Switch

设表myPartitionTable是一已分区的表,分区函数为myPartitionTableFN

1. 建一个和myPartitionTable一样的表myPartitionTableArchive,包括结构和索引
2. 使用以下SQL查询分区序号:
select $partition.myPartitionTableFN(day_key) as [partition_number],
min(day_key) as min_day_key,
max(day_key) as max_day_key,
count(1) as ct
from myPartitionTable
group by $partition.myPartitionTableFN(day_key)
order by [partition_number]
3. 使用以下SQL将数据switch到出去
ALTER TABLE myPartitionTable switch PARTITION 3 TO myPartitionTableArchive PARTITION 3
此步操作非常快。

06 6, 2008

[系统] ::64位英文Windows 2003改成中文

步骤:
1. 安装多国语言包
2. 安装过程中需要Windows的SP2,需要插入CD1
3. 按照提示重启
4. Windows 控制面板->语言和区域设置->区域和语言选项,各种标签下都设为中国或中文
5. 按照提示重启

06 5, 2008

[SQL] ::安装SQL SERVER 2005时提示找不到sql.cab问题的解决

Subject: Re: Potential solution for SQL.CAB problem reported in several threads
Posted by: "forgetfuljones" (forgetfuljones.2ew8..@mail.mcse.ms)
Date: Fri, 29 Sep 2006 02:09:09 -0500

Previous Message View Thread This message has no replies.
===SOLUTION====

(I had this issue on x64 Std 2003 and x64 SQL Enterprise)

Dear all,

I have been around in circles for about 5 hours and finally got rid of
the SQL.CAB error.

I removed all of the SQL installaations (DB and Studio) and then .Net
framework.
Rebooted.
Ran setup (Which installed .Net etc)

Installed only the Client Management Studio

Then installed the DB.

NO ERRORS

Hope this helps someone

05 19, 2008

[SQL] ::添加AS2005链接服务器

EXEC sp_addlinkedserver
@server='LINKED_OLAP', -- local SQL name given to the linked server
@srvproduct='', -- not used
@provider='MSOLAP.3', -- OLE Database provider (the .2 means the SQL2K version)
@datasrc='machine name', -- analysis server name (machine name)
@catalog='logdw' -- default catalog/database
GO