ALTER proc [dbo].[procvote]
 @voteTime varchar(20),
 @voteMac varchar(20),
 @pk_article varchar(20)
AS
BEGIN
declare @countArt int, @count int,@countVote int
--先检查是否有这个参赛记录
 select @count=(count(*)) from tb1 where substring(pk_article,1,18)=@pk_article
--再检查是否投票记录已经开启!
 select @countVote=(count(*)) from tb1 where substring(pk_article,1,18)=@pk_article and islock=1
--再检测今天是否有投票记录
 select @countArt=(count(*)) from tb2 where pk_article=@pk_article and voteMac=@voteMac and substring(convert(varchar(20),voteTime,120),1,10)=substring(@voteTime,1,10)
if(@count <> 0)
 begin
  if(@countVote=0)
   begin
    if(@countArt = 0)
     begin
      insert into blogVote(voteTime,voteMac,pk_article) values(@voteTime,@voteMac,@pk_article)
      update bloggame set count_blog=count_blog+1 where substring(pk_article,1,18)=@pk_article
      return 0
     end
    else
     begin
      return 3
     end
   end
  else
   begin
    return 4
   end
 end
else
 begin
  return 2
 end
 SET NOCOUNT ON;    -- Insert statements for procedure here
END问题是这样的,我获得返回值的时候,只能获得2和0,就是没有这个记录,或者是投票成功,至于投票被锁定和已经投过票的返回记录都是投票成功...是不是我的存储过程条件嵌套有问题,还是declare定义的时候出了问题,请教高手 

解决方案 »

  1.   

    create proc [dbo].[procvote] 
    @votetime varchar(20), 
    @votemac varchar(20), 
    @pk_article varchar(20) 
    as 
    begin 
    set nocount on; --调下顺序 declare @countArt int, @count int,@countVote int 
    --先检查是否有这个参赛记录 
    select @count=count(1) 
    from tb1 
    where left(pk_article,18)=@pk_article  --再检查是否投票记录已经开启! 
    select @countVote=count(1)
    from tb1 
    where left(pk_article,18)=@pk_article and islock=1  --再检测今天是否有投票记录 
    select @countArt=count(*) 
    from tb2 
    where pk_article=@pk_article and voteMac=@voteMac 
      and convert(varchar(10),voteTime,120)=left(@voteTime,10)  if(@count <> 0) 
      if(@countVote=0) 
    if(@countArt = 0) 
    begin    insert into blogVote(voteTime,voteMac,pk_article) 
    values(@voteTime,@voteMac,@pk_article)    update bloggame 
    set count_blog=count_blog+1 
    where left(pk_article,18)=@pk_article    return 0  end 
    else --@countArt <> 0
    return 3 
      else --@countVote<>0
    return 4
    else --@count = 0
      return 2 
    end 没发现什么问题..
      

  2.   

    ALTER proc [dbo].[procvote] 
        @voteTime varchar(20), 
        @voteMac varchar(20), 
        @pk_article varchar(20) 
    AS 
    BEGIN --先检查是否有这个参赛记录 
    if not exists (select 1 from tb1 where substring(pk_article,1,18)=@pk_article)
        return 2--再检查是否投票记录已经开启! 
    if exists (select 1 from tb1 where substring(pk_article,1,18)=@pk_article and islock=1)
        return 4--再检测今天是否有投票记录 
    if exists (select 1 from tb2 where pk_article=@pk_article and voteMac=@voteMac and substring(convert(varchar(20),voteTime,120),1,10)=substring(@voteTime,1,10))
        return 3
    insert into blogVote(voteTime,voteMac,pk_article) values(@voteTime,@voteMac,@pk_article) 
    update bloggame set count_blog=count_blog+1 where substring(pk_article,1,18)=@pk_article 
    return 0 SET NOCOUNT ON;     -- Insert statements for procedure here 
    END 
      

  3.   

    试试看:
    declare 
        @voteTime varchar(20), 
        @voteMac varchar(20), 
        @pk_article varchar(20) set @voteTime = ..
    set @voteMac = ..
    set @pk_article = ..if exists (select 1 from tb2 where pk_article=@pk_article and voteMac=@voteMac and substring(convert(varchar(20),voteTime,120),1,10)=substring(@voteTime,1,10))
        print 'A'
    这段代码是否能够打印出"A"