|M| 从零开始学习存储过程第一贴:把我以前的程序换存储过程写法
public DataTable Search(string Name, int TypeID)
{
String sql= "select * from tab ";
String iWhere= " where 1=1 "
if(name != " ")
{
iWhere += " And Name like '% " + Name + "% ' ";
}
if(TypeID != -1)
{
iWhere += "And TypeID = " + TypeID;
}
...
...
}
也就是将上面的那段组SQL查询的写成存储过程
谢谢
------解决方案--------------------。。。。
直接把你这些字符串组合起来不行么???
和写存储过程没什么区别了吧
------解决方案--------------------create procedure test(@name varchar,@TypeId int)
as
declare @sql varchar(512)
declare @iWhere varchar(32)
set @sql= "select * from tab "
set @iWhere= " where 1=1 "
if(name <> " ")
begin
set @iWhere =@iWhere + " And Name like '% " + Name + "% ' ";
end
if(TypeID <> -1)
begin
set @iWhere =@iWhere + "And TypeID = " + TypeID;
end
------解决方案--------------------up
------解决方案--------------------create procedure test(@name varchar,@TypeId int)
as
declare @sql varchar(512)
declare @iWhere varchar(32)
set @sql= "select * from tab "
set @iWhere= " where 1=1 "
if(name <> " ")
begin
set @iWhere =@iWhere + " And Name like '% " + Name + "% ' ";
end
if(TypeID <> -1)
begin
set @iWhere =@iWhere + "And TypeID = " + TypeID;
end
EXECUTE sp_executesql @sql
------解决方案--------------------create procedure ProSearch
@Name char(400),
@ID int
as
Declare @sql
set @sql= 'select * from tab where 1=1 and name like % '+ Name + '% and TypeID= '+ID
exec sp_executesql @sql
GO