以下事例,使用游標循環表#temptable中數據,然後讓當前行和上一行中的argument1 相加 存放到當前行的 argument2 中,比較簡單。
--drop table #temptable
create table #temptable
(
argument1 int,
argument2 int,
argument3 datetime
)
declare @rowcount int,@argument1 int,@argument2 nvarchar(50),@argument3 datetime
set @rowcount=1
set @argument1=1
set @argument2=0
set @argument3=GETDATE()
while(@rowcount<100)
begin
insert into #temptable(argument1,argument2,argument3)
values(@argument1,@argument2,@argument3)
set @argument1=@argument1 + datepart(day,@argument3)
set @argument3=@argument3-1
set @rowcount = @rowcount + 1
end
--select * from #temptable
declare @lastargument2 int
set @lastargument2=0
set @argument2=0
declare _cursor cursor for(select argument1 from #temptable)
open _cursor;
fetch next from _cursor into @argument2
while @@fetch_status = 0
begin
update #temptable
set argument2=@argument2+@lastargument2
where current of _cursor
set @lastargument2=@argument2
fetch next from _cursor into @argument2
end
close _cursor
deallocate _cursor
--select * from #temptable
問一個問題:
第一句fetch next from _cursor into @argument2 這句為什麼不能放在while循環的第一行,刪除第二行呢?我記得自己當時在這裡出錯了,呵呵。
update table_name set PAS00000000055956 = PAS00000000055956 + '9000000' where...
前提是你的字段類型要對
那麼你就把數據讀出來,然後操作
如用PHP開發, 那麼就可以很容易根據數據的特性來選擇數據段運算.
然後再寫入,我也是初學,通常我會這樣處理.
不過就你的問題我有另外一個想法,就是在數據庫裡只存儲數字部分,然後對其運算,然後在讀取時候再進行判斷,處理輸入.
這才是程序設計的正確思路: 物理層+邏輯層+輸出層.
再大型的程序分了這三層來定義,思路都能清晰起來.
create table biao22 ( a int)
insert biao22 select '10'
insert biao22 select '9'
insert biao22 select '20'
insert biao22 select '25'
insert biao22 select '18'
insert biao22 select '7'
select * from biao22
create table #A ( id int identity(1,1), a int)
insert into #a(a)
select * from biao22
declare @a int
declare @b int
select @a=min(id) from #a
select @b=max(id) from #a
while (@a<@b)
begin
select t1.a-t2.a from #a t1,#a t2 where t1.id=t2.id+1 and t1.id=@a
set @a=@a+1
end