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
(
@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
下面语句的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+%'
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 + '%'
换成:
city like '%'+@city+'%' and town like '%'+@town+'%' and dic_text like '%'+@village+'%'
即:把匹配符号%用单引号括起来.假设@city = '北京'的话,括起来的效果就是:
city like '%北京%'
否则就是:
city like '%+@city+%'
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();