一些SqlServer的东西

2010年05月18日 Windows 暂无评论

本文记录一些关于SqlServer的东西

1、删除重复数据的代码

delete f  FROM fantlam f  WHERE EXISTS
          (SELECT *
         FROM fantlam WHERE name = f.name AND id > f.id)

注:把fantlam表里name重复的id靠后的数据删除,

2、一段循环程序的sql代码

declare @i int
declare @j int
declare @bid bigint
declare @pid bigint
declare @name varchar(50)
declare mc cursor

for select brand_id,brand_name from dx_brand_brand where brand_id >10000

open mc
fetch next from mc
into @bid,@name
 while(@@fetch_status=0)
begin
print(@bid)
print(@name)
set @i=0
declare mc2 cursor
for select pro_id from dx_pro_pbDetail where brand_id = @bid
open mc2
fetch next from mc2
into @pid
while(@@fetch_status=0)
begin

set @j=(select pro_hot from dx_pro_pro where pro_id=@pid)
set @i=@i+@j

fetch next from mc2
into @pid
end
 close mc2
deallocate mc2

print('总数量是--')
print(@i)
update dx_brand_brand set brand_hot=@i from dx_brand_brand where brand_id=@bid

fetch next from mc
 into @bid,@name
end
 close mc
deallocate mc

3、一个SqlServer2005的bug

最近碰到从SqlServer2000导入到SqlServer2005的时候会出一个错,网上找了一些解决方法如下:

向SQL Server2005里导入数据是出现错误“SQL 错误描述为: 链接服务器 '(null)' 的 OLE DB 访问接口'STREAM' 返回了对列 '[!BulkInsert].field' 无效的数据”,已打了SQL Server2005的最新补丁,在SQL Server2008里有同样的问题。
BaiDu了下,有人说好像是SQL 2005的一个未公开的Bug,在SQL Server2000里没有这样的问题。
解决:
在SQL Server启动过程中增加参数4808。
1. 在 SQL Server 配置管理器中,单击“SQL Server 服务”。
2. 在右窗格中,右键单击 SQL Server (<实例名>),再单击“属性”。
3. 在“高级”选项卡的“启动参数”框中,键入“;-T4808”(加分号和-T4808)。
4.设置完成后,重启SQL Server服务,在查询分析器中执行“DBCC tracestatus”,返回结果:
 一些SqlServer的东西
表明参数设置成功。

给我留言