• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
一个具有上进心的码农
因为一篇文章中有很多是从很多篇文章中摘取的,请恕我没有一一说明摘取出处,如果没有说明,则该文章默认是摘取,如有侵犯您的权益,请与我联系,将会马上删除。
博客园    首页    新随笔    联系   管理    订阅  订阅

查询相邻两行的数据

Code
表概况: 
  ..交易时间..当前余额.. 
..(
1)xxxx-xx-xx ..40000.0.. 
..(
2)xxxx-xx-xx30000.0.. 
..(
3)xxxx-xx-xx20000.0.. 
..(
4)xxxx-xx-xx10000.0.. 

大概是这种情况,我想求相邻两行的 交易时间 的差,注意是相邻两行,然后用40000.
0×时间(2)与时间(1)的差×日利率=利息。 

请高手帮帮忙吧,小弟很急的,最好能有代码


create table tbf(id 
int,tim varchar(10),acc decimal(22,4))
insert into tbf values(
1,'2009/01/01',40000)
insert into tbf values(
2,'2009/02/01',30000)
insert into tbf values(
3,'2009/03/01',20000)
insert into tbf values(
4,'2009/04/01',10000)
insert into tbf values(
5,'2009/04/07',60000)


select datediff(dd,a.tim,b.tim),a.
*
from tbf a 
left join tbf b on a.id 
= b.id+1

drop table tbf
/*
            id          tim        acc
----------- ----------- ---------- ---------------------------------------
NULL        1           2009/01/01 40000.0000
-31         2           2009/02/01 30000.0000
-28         3           2009/03/01 20000.0000
-31         4           2009/04/01 10000.0000
-6          5           2009/04/07 60000.0000

(5 行受影响)

*/

 

 

 

Code
SQL实现split函数,自定义分割字符,自定义取出第几个分割字符前的字符串 
自定义取出第几个分割字符前的字符串,默认位置(
0)
格式:dbo.split(字段名,
'分隔字符',取出的第几个字符串)
如果没有分隔的字符,则返回整个字符串。
如果取出的位置字符串的位置超出Index则返回空。

CREATE FUNCTION [dbo].[split]
(@str nvarchar(
4000),@code varchar(10),@no int ) 
RETURNS varchar(
200)
AS 
BEGIN 

declare @intLen 
int
declare @count 
int
declare @indexb 
int
declare @indexe 
int
set @intLen=len(@code)
set @count=0
set @indexb=1

if @no=0
if charindex(@code,@str,@indexb)<>0
return left(@str,charindex(@code,@str,@indexb)-1) 
else
return @str

while charindex(@code,@str,@indexb)<>0
begin
set @count=@count+1
if @count=@no
break
set @indexb=@intLen+charindex(@code,@str,@indexb)
end 

if @count=@no
begin

set @indexe=@intLen+charindex(@code,@str,@indexb)
if charindex(@code,@str,@indexe)<>0
return substring(@str,charindex(@code,@str,@indexb)+len(@code),charindex(@code,@str,@indexe)-charindex(@code,@str,@indexb)-len(@code))
else 
return right(@str,len(@str)-charindex(@code,@str,@indexb)-len(@code)+1)

end

return ''

END 


游标的使用

  给出具体的例子: 
declare @id nvarchar(
20)  --定义变量来保存ID号
declare @A 
float                  --定义变量来保存值
declare mycursor cursor 
for select * from tb_c   --为所获得的数据集指定游标
open mycursor                   
--打开游标
fetch next from mycursor  into @id,@A   
--开始抓第一条数据
while(@@fetch_status=0)     --如果数据集里一直有数据
begin
 select tb_b.name,(tb_b.gz 
+ @A) from tb_b where tb_b.id = @id   --开始做想做的事(什么更新呀,删除呀)
        fetch next from mycursor into @id,@A     
--跳到下一条数据
end
close mycursor        
--关闭游标
deallocate mycursor  
--删除游标

注:

//判断游标的状态
//0 fetch语句成功 
//-1 fetch语句失败或此行不在结果集中 
//-2被提取的行不存在


大数据的迁移用此个方法比较好。。
sqlbulkcopy

 

 

 

  用来测试一个方法需要的时间 

Stopwatch myWatch = new Stopwatch();

posted @ 2009-05-08 17:15  不若相忘于江湖  阅读(259)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3