CREATE PROCEDURE [dbo].[sp_stat_info] 
(
@city nvarchar(20),
@town nvarchar(20),
@village nvarchar(20)
)
as
declare @num numeric(10)
declare @yd_num numeric(10)
declare @lt_num numeric(10)
declare @gh_num numeric(10)
declare @xlt_num numeric(10)
select @num=count(jmzh)  from jfxd.t_gongan_end_2
if @city='aaa' 
begin
select @yd_num=count(jmzh)  from jfxd.t_gongan_end_2 where SERIAL_NUMBER is not null
select @lt_num=count(jmzh) from jfxd.t_gongan_end_2 where  unicom is not null
select @gh_num=count(jmzh) from jfxd.t_gongan_end_2 where  tel is not null
select @xlt_num=count(jmzh) from jfxd.t_gongan_end_2 where  little_tel is not null
end
else if @town='bbb' 
begin
//sql语句
end
else
begin
//sql语句
end
update t_stat_info set……
GO

解决方案 »

  1.   

    语法没问题,关键是需要检查你的处理逻辑。if @city='aaa' else if @town='bbb' else这三者好像不是条件的全集吧
      

  2.   

    谢谢,好像没有执行到后面条件
    下面语句的like部分有问题吗?
    select @yd_num=count(jmzh)  from jfxd.t_gongan_end_2 where SERIAL_NUMBER is not null and city like '%+@city+%' and town like '%+@town+%' and dic_text like '%+@village+%'
      

  3.   

    --tryselect @yd_num=count(jmzh)  
    from jfxd.t_gongan_end_2 
    where SERIAL_NUMBER is not null 
    and city like '%' + @city + '%' 
    and town like '%' + @town + '%' 
    and dic_text like '%' + @village + '%'
      

  4.   

    把city like '%+@city+%' and town like '%+@town+%' and dic_text like '%+@village+%'
    换成:
    city like '%'+@city+'%' and town like '%'+@town+'%' and dic_text like '%'+@village+'%'
    即:把匹配符号%用单引号括起来.假设@city = '北京'的话,括起来的效果就是:
    city like '%北京%'
    否则就是:
    city like '%+@city+%'
      

  5.   

    select @yd_num=count(jmzh)  from jfxd.t_gongan_end_2 where SERIAL_NUMBER is not null and city like '%'+@city+'%' and town like '%'+@town+'%' and dic_text like '%'+@village+'%'
      

  6.   

    非常非常感谢大家的帮助,只是在C#调用存储过程的过程中,根本没有执行存储过程,而只是将t_stat_info 现成的数据提出来。(存储过程是用来更新t_stat_info的,应该每次不一样的)
    string constr=System.Configuration.ConfigurationSettings.AppSettings["sql144"]; 
    string sqlstr="select * from t_stat_info ";
    SqlConnection myconn=new SqlConnection(constr);
    myconn.Open(); SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = new SqlCommand();
    da.SelectCommand.Connection = myconn;
             da.SelectCommand.CommandText = "sp_stat_info_3p  '"+city+"','"+town+"','"+village+"'";
    da.SelectCommand.CommandType = CommandType.StoredProcedure; 

    SqlDataAdapter myAdapter=new SqlDataAdapter(sqlstr,myconn);
    DataSet myDataSet=new DataSet();
    myAdapter.Fill(myDataSet,sqlstr);
    this.DataGrid1.DataSource=myDataSet;
    this.DataGrid1.DataBind();
    myAdapter.Dispose();
    myconn.Close();