当前位置:数据库 > SQLServer >>

使用SQL Server 1、修创建一个表T_Person,并插入一些数据值。写一个存储过程使用游标将每行记录的年加一

答案:create table T_Person (id int identity(1,1) primary key,p_year int,p_date datetime); GO insert into T_Person (p_year,p_date) values(year(getdate()),getdate()); insert into T_Person (p_year,p_date) values(year(getdate()),getdate()); insert into T_Person (p_year,p_date) values(year(getdate()),getdate()); GO create procedure myProc as begin declare @id int declare myCursor cursor for select id from T_Person open myCursor fetch next from myCursor into @id WHILE @@FETCH_STATUS = 0 BEGIN update T_Person set p_year=p_year+1,p_date=dateadd(yy,1,p_date) where id=@id fetch next from myCursor into @id END close myCursor deallocate myCursor end GO exec myProc GO select * from T_Person GO 记得给分啊亲~~
其他:Create table T_Person (Tid int primary key,Tyear datetime);
GO
insert into T_Person(Tid,Tyear)
select '1',GETDATE()+0
union select '2',GETDATE()+1
union select '3',GETDATE()+2
union select '4',GETDATE()+3
Select * from T_Person
GO
Create procedure Proc_Tyear as
begin
declare @id int
declare cur cursor for select tid from T_Person
open cur
fetch next from cur into @id
WHILE @@FETCH_STATUS = 0
BEGIN
update T_Person set Tyear=DATEADD(YEAR,1,Tyear) where Tid=@id
fetch next from cur into @id
END
close cur
deallocate cur
end
GO
exec Proc_Tyear
GO
select * from T_Person
GO
drop table T_Person

上一个:sql server 2008 中 instance 是干嘛的 如何创建一个instance ??
下一个:在用VS2010连接SQL Server 2012时,在SQL中先创建好数据库,然后再在VS中链接,但怎么也链接不上,怎么办

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,