码迷,mamicode.com
首页 > 数据库 > 详细

sqlserver中存储过程和游标的使用

时间:2015-05-14 00:58:12      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

DECLARE
@phone nvarchar(20)
BEGIN
declare @count int=0
declare @dateNow nvarchar(100) =convert(nvarchar, getdate(),120)
declare @dateNowFormat nvarchar(20) =convert(nvarchar(100), getdate(), 23) print @dateNowFormat
declare @datepre nvarchar(100)=convert(nvarchar(100), CONVERT(varchar(100), dateadd(DAY,-1,@dateNowFormat), 23), 23)
declare @tableBack nvarchar(100)=‘sms_userDeadPhone_‘+@dateNowFormat

print @dateNow
print @datepre

-- 定义游标.
DECLARE DeadPhone CURSOR FAST_FORWARD FOR

select UserPhone from SMS_UserDB.dbo.SMS_YDReciveMessage where (UserMessage like ‘%TD%‘ or UserMessage like ‘%td%‘) and MessageDate between @datepre and @dateNow
-- 打开游标.
OPEN DeadPhone;
--填充数据.
FETCH NEXT FROM DeadPhone INTO @phone;
--假如检索到了数据,才处理.
WHILE @@fetch_status = 0
BEGIN
print @phone;
--查询号码是否已经存在
select @count=COUNT(1) from SMS_UserDeadPhone where UserPhone=@phone
if(@count<=0)
begin
--select * into UserDeadPhone from SMS_UserDeadPhone
insert into DeadPhonetable(UserPhone,UserReson,CreateDate)values(@phone,‘屏蔽‘,GETDATE())
end

--填充下一条数据.
FETCH NEXT FROM DeadPhone INTO @phone;

END
-- 关闭游标
CLOSE DeadPhone;
--释放游标.
DEALLOCATE DeadPhone;
END;

sqlserver中存储过程和游标的使用

标签:

原文地址:http://www.cnblogs.com/hui1107464497/p/4502115.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!