SQL語句時間批量修改
用一條語句搞不出來,我給你想了個辦法,不過我的數據字段不是日期型,是字符串型的。
declare @maxdate varchar(10),@dt varchar(10)
select @maxdate=max(thetime) from t2
set @dt = convert(varchar(10),dateadd(day,1,convert(datetime,@maxdate)),102)
update t2 set nexttime=isnull(
(select min(thetime) from t2 tmp where *e > *e )
,@dt)
from t2 t
update t2 set thetime = nexttime
////////////////////////////////////
if exists (select * from *ects where id = object_id(N'[dbo].[t2]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[t2]
GO
CREATE TABLE [dbo].[t2] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[event] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[thetime] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[nexttime] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
此外,我在表里加了一個字段,nexttime,先把下一條數據的日期查出來,放到這個字段,然后再更新。
/////////////////////////////////
我又試了一下把time變為日期類型,代碼更簡單,不用轉換了。
declare @maxdate datetime
select @maxdate=max(thetime) from t2
set @maxdate=dateadd(day,1,@maxdate)
update t2 set nexttime=isnull(
(select min(thetime) from t2 tmp where *e > *e )
,@maxdate)
from t2 t
update t2 set thetime=nexttime
你的問題,實際就是要把當前的時間變為下一條的時間,那么只要取比當前時間大的時間里,最小的那條的記錄就行了。不過這樣取最后一條有點問題,因為沒有比他大的時間了,因此需要特別處理一下。
我試驗了,可行。不過以上代碼,你可能要分別執行,或者放入一個存儲過程,或者函數里,總之,一條語句不容易搞定。
求批量修改數據庫信息的SQL語句
declare @Name2 varchar(8000)--存儲字段name1
declare @Name1 varchar(8000)--存儲字段name2
declare @id varchar(800)--存儲id
declare @result int --存儲數字之間的差
declare change_col cursor for select id,name1,name2 from table
open change_col
fecth next from change_col into @id,@Name1,@Name2
while (@@fetch_status = 0)
begin
set @result = (substring(name1,charindex(name1,'/')+1,5))
if (ABS(@result) >5)
begin
if(right(@name2,2)='\二')
begin
end
else if (right(@name2,2)='\三')
begin
update table set name2 = substring(@name2,0,len(@name2)-2)+'/二' where id = @ID
end
else
begin
update table set name2 = @name2 +'/二' where id = @ID
end
end
else
begin
if(right(@name2,2)='\二')
begin
update table set name2 = substring(@name2,0,len(@name2)-2)+'/三' where id = @id
end
else if (right(@name2,2)='\三')
begin
end
else
begin
update table set name2 = @name2 +'/三' where id = @id
end
end
fecth next from change_col into @id,@Name1,@Name2
end
close change_col
deallocate change_col