Sql 中一种很巧妙的获取更新数据

在sql中有些数据是实时更新的,而且要获取最新的数据并更改它的状态,这个时候理论是需要先更新你获取的这些实时新数据的status,然后再把你查询处理的数据返回处理,因为数据是实时更新的状态,如果你先获取新的数据,然后在更新的时候,更新的数据往往跟你获取的新数据会有点误差!那么咱们要如果先更改数据再进行查询呢?

View Code
USE [800Fc#Com#System]
GO
/****** Object:  StoredProcedure [dbo].[sp_admin_mobile_smssend_log_select]    Script Date: 04/19/2013 09:56:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER proc [dbo].[sp_admin_mobile_smssend_log_select]
as
--f_status=-100查询的,-100代表是新的数据
begin

declare @guid varchar(50) = newid()

update t_mobile_smssend_log set f_status=0,f_threadguid = @guid 
where f_status=-100

select * from t_mobile_smssend_log where f_threadguid = @guid

end

这段代码的意思就是先定义一个变量@guid的,然后给@guid一个随机值,随机值的获取是newid()这个函数。

当f_status的值为-100的时候,代表该数据是新的数据,那么我们更改f_status的值为0.代表已经进入队列之中,然后更改f_threadguid的值为我们事先已经定义好的变量值,这个变量值是一个随机数

。这一步就更改了新数据的状态,也能获取咱们更改这些数据的集合,根据f_threadguid=@guid;

所以这个问题很巧妙的解决了实时更新数据跟要更改数据之间的漏洞!

如果大家还有更好的方法,欢迎提供一起进步哈!

 

View Code
USE [800Fc#Com#System]
GO
/****** Object:  StoredProcedure [dbo].[sp_admin_taginsertupdate]    Script Date: 05/02/2013 10:04:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[sp_admin_taginsertupdate]
@siteid int=3501,
@id int=-1,
@f_name nvarchar(50)='',
@masterid int,
@mastername nvarchar(50)='',
@f_status nvarchar(50)='',
@f_color nvarchar(50)='',
@f_key nvarchar(50)='',
@action int --1:新增,2:修改,3:修改颜色标签
as
--判断权限
BEGIN

    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
        if dbo.fn_sys_siterightcheck(@siteid,@masterid)=0
    begin
        select '403.1' as RtnCode,'您没有操作此站点权限' as RtnMessage for xml 

raw,elements,root('rows')
        return 403.1
    end
    if @action=1--新增操作
    begin
    if  @f_color is NULL or @f_color=''
        begin    

            if(select count(*) from t_tag where ISNULL(f_status,1) !=99  and f_key=@f_key)>=10
                begin
                    Select 3 as RtnCode,'添加失败!标签不能多于10个!' as RtnMessage For XML RAW ,ELEMENTS,ROOT('rows') 
                    return 3
                end
        
            declare @colors  varchar(max) ='#63901b|#b23ec7|#309496|#4863b0|#4d53a9|#b59037|#c96507|#b43419|#c94a91|#b50c13|#ea9c00|#e86c24|#a4529a|#925b58|#b2773d|#aaac47|#ee5007|#ae453f|#960699|#733a71|#3a9a50|#17b7ab|#1887fb|#569493|#7b6379|#4989eb|#354b72|#206bea|#7143c0|#2521f1|#a49f79|#859aad|#595959|#343434|#000000|'
            declare @r int = ROUND(Rand()*35,0)
                if @r=0 
                    set @r=1                                 
                    set @f_color=(select col from dbo.fn_splitSTR(@colors,'|') where idx = @r)
        end    
                        insert into t_tag(
                        f_name,
                        f_addtime,
                        f_master_id,
                        f_master_name,
                        f_updatetime,
                        f_color,
                        f_key,
                        f_siteid
                        )
                        values
                        (
                    @f_name,
                    getdate(),
                    @masterid,
                    @mastername,
                    getdate(),
                    @f_color,
                    @f_key,
                    @siteid
                        )
    if @@ERROR=0
                    begin
                        Select 1 as RtnCode,'添加成功' as RtnMessage For XML RAW ,ELEMENTS,ROOT('rows') 
                        return 1
                    end 
                else
                    begin
                        Select 0 as RtnCode,'添加失败' as RtnMessage For XML RAW ,ELEMENTS,ROOT('rows') 
                        return 0
                    end
    end
    
                if @action=2 --修改操作
                begin
                        update t_tag set f_name=@f_name,f_master_id=@masterid,f_master_name=@mastername,f_updatetime=getdate()
                        where id=@id
                
                        Select 1 as RtnCode,'修改成功' as RtnMessage For XML RAW ,ELEMENTS,ROOT('rows') 
                        return 1
                end
                
                if @action=3--修改颜色标签
                begin
                update t_tag set 
                f_master_id=@masterid,f_master_name=@mastername,f_updatetime=getdate(),f_color=@f_color
                where id=@id
                Select 1 as RtnCode,'修改成功' as RtnMessage For XML RAW ,ELEMENTS,ROOT('rows') 
                return 1
                end
        End

 

posted @ 2013-04-19 10:15  张涵哲琪  阅读(1012)  评论(0编辑  收藏  举报