日期:2014-05-17  浏览次数:20651 次

求一SQL算法
实现说明:
  前台传进几个数据,从一个表中搜索出同这几个数据完全一样的一条记录! 
  要求:
  表中存在一个标记字段is_audit ,当通过传入的数据搜索时,结果集中存在记录有可能is_audit=0  
  or is_audit=1.
  每次返回的结果只能是一条而且is_audit=0 ,当结果集中只有一条记录且is_audit=1 时,返回error=0
  当@@rowcount=0 时返回error=-1
  当系统中存在二条完全一样且is_audit=0 时,先返回第一条且要updata is_audit=1 
  select @prod_no=m.co_no  
  from t_PaperCO m  
  where  
  m.qty=@qty  
  and m.art_id=@art_id  
  and m.csize_l=@csize_l  
  and m.csize_w=@csize_w  
  and m.cust_id=@cust_id  
  and m.size_yx1=@yx1 
  and m.size_yx2=@yx2  
  and m.size_yx3=@yx3
  and m.size_yx4=@yx4
  and m.size_yx5=@yx5
  and m.yx_id=@press
  and convert(char(10),m.co_date,111) >convert(char(10),dateadd(dd,-5,getDate()),111)  
if @@rowcount<1 --如果没有订单就提示并退出
 begin
  set @errno='-1'  
  goto _error
 end


select top 1 @prod_no=co_no from t_PaperCO
  where @prod_no=co_no and convert(char(10),co_date,111) >convert(char(10),dateadd(dd,-5,getDate()),111)  
  and is_audit='0'


 if @@rowcount<1 --此单审核了
  begin
  set @errno='0'  
  goto _error
 end

  update t_PaperCO set is_audit='1',auditor=@user_name,audit_date=getdate() where co_no=@prod_no and is_audit='0' 

_exit: 
  set nocount off  
  return 0  
  
_error: 
  set nocount off  
  return 1  
  
  请重新写出一高效算法.

------解决方案--------------------
没看懂,大概是写存储过程做几个分支判断吧。
------解决方案--------------------
每次返回的结果只能是一条而且is_audit=0 ,当结果集中只有一条记录且is_audit=1 时,返回error=0
当@@rowcount=0 时返回error=-1
当系统中存在二条完全一样且is_audit=0 时,先返回第一条且要updata is_audit=1

看到这有些绕,lz怎么确定 返回的结果只能是一条,如果多条,怎么处理,都没有交代清楚
------解决方案--------------------
直接给出表结构、数据和想要的结果。光看你的需求,很糊涂。。。