日期:2014-05-18  浏览次数:20432 次

帮忙看下此存储过程
create Proc P_Check1 
  @DelTable varchar(50)  
  as
  declare @count int,
  @count1 int,  
  @DelID varchar(50),  
  @DelIDValue varchar(50),  
  @CheckTable varchar(50),  
  @CheckField varchar(50),  
  @Mess varchar(50),  
  @sql nvarchar(4000), 
  @sql1 varchar(4000) output
   
  begin --判断表是否存在  
  select @count=COUNT(1) from MasterCheck where DelTable=@DelTable
  if @count>0
  begin --检测
  select @DelID=DelID,@DelIDValue=DelIDValue,@CheckTable=CheckTable,@CheckField=CheckField,@Mess=Mess
  from RecordCheck where DelTable = @DelTable
  print(@CheckField)
  set @sql='select @count1=COUNT(1) from '+@CheckTable+' where '+@CheckField+'='''+@DelIDValue+''''
  print(@sql)
  exec sp_executesql @sql,N'@count1 int',@count1 
  print(@count1)
  if @count1>0
  print('有内容')
  else
  print('直接删除')
  end
  else
  print('此表不存在')
  end
  go


  print(@sql)和print(@count1)都没法输出,我也不知道count1是否得到了正确结果,但是最终输输出的是'直接删除',这个结果是不对的,应该输出'有内容',请帮我看一下错误出在哪里!谢谢!



------解决方案--------------------
SQL code
create Proc P_Check1  
  @DelTable varchar(50)   
  as
  declare @count int,
  @count1 int,   
  @DelID varchar(50),   
  @DelIDValue varchar(50),   
  @CheckTable varchar(50),   
  @CheckField varchar(50),   
  @Mess varchar(50),   
  @sql nvarchar(4000),  
  @sql1 varchar(4000) output
    
  begin --判断表是否存在   
  select @count=COUNT(1) from MasterCheck where DelTable=@DelTable
  if @count>0
  begin --检测
  select @DelID=DelID,@DelIDValue=DelIDValue,@CheckTable=CheckTable,@CheckField=CheckField,@Mess=Mess
  from RecordCheck where DelTable = @DelTable
  print(@CheckField)
  set @sql='select @count1=COUNT(1) from '+@CheckTable+' where '+@CheckField+'='''+@DelIDValue+''''
  print(@sql)
  exec sp_executesql @sql,N'@count1 int output',@count1  output
  print(@count1)
  if @count1>0
  print('有内容')
  else
  print('直接删除')
  end
  else
  print('此表不存在')
  end
  go

------解决方案--------------------
SQL code
create Proc P_Check1  
  @DelTable varchar(50)   
  as
  declare @count int,
  @count1 int,   
  @DelID varchar(50),   
  @DelIDValue varchar(50),   
  @CheckTable varchar(50),   
  @CheckField varchar(50),   
  @Mess varchar(50),   
  @sql nvarchar(4000),  
  @sql1 varchar(4000) output
    
  begin --判断表是否存在   
  select @count=COUNT(1) from MasterCheck where DelTable=@DelTable
  if @count>0
  begin --检测
  select @DelID=DelID,@DelIDValue=DelIDValue,@CheckTable=CheckTable,@CheckField=CheckField,@Mess=Mess
  from RecordCheck where DelTable = @DelTable
  print(@CheckField)
  set @sql='select @count1=COUNT(1) from '+@CheckTable+' where '+@CheckField+'='''+@DelIDValue+''''
  print(@sql)
  exec sp_executesql @sql,N'@count1 int output',@count1  output
  print(@count1)
  if @count1>0
  print('有内容')
  else
  print('直接删除')
  end
  else
  print('此表不存在')
  end
  go