strSql.Append("select dossier_id,archives_parcel_id,owner_name,land_card_id from Archive_Details_view ");
下面是where作为条件,
if (model.dossier_id != null)
{
strSql.Append("dossier_id='" + model.dossier_id + "',");
}
if (model.title != null)
{
strSql.Append("title='" + model.title + "',");
}
if (model.map_number != null)
{
strSql.Append("map_number='" + model.map_number + "',");
}
if (model.usufruct_name != null)
{
strSql.Append("usufruct_name='" + model.usufruct_name + "',");
}
if (model.use_term != null )
{
strSql.Append("use_term=" + model.use_term + ",");
}
if (model.end_time != null)
{
strSql.Append("end_time='" + model.end_time + "',");
}
if (model.land_type_name != null)
{
strSql.Append("land_type_name='" + model.land_type_name + "',");
}
if (model.location != null)
{
strSql.Append("location='" + model.location + "',");
}
if (model.owner_kind_name != null)
{
strSql.Append("owner_kind_name='" + model.owner_kind_name + "',");
}
if (model.former_owner_id != null)
{
strSql.Append("former_owner_id=" + model.former_owner_id + ",");
}
if (model.land_card_id != null)
{
strSql.Append("land_card_id='" + model.land_card_id + "',");
}
if (model.former_land_card_id != null)
{
strSql.Append("former_land_card_id='" + model.former_land_card_id + "',");
}
if (model.owner_name != null)
{
strSql.Append("owner_name='" + model.owner_name + "',");
}
if (model.credentials_type_name != null)
{
strSql.Append("credentials_type_name='" + model.credentials_type_name + "',");
}
if (model.credentials_no != null)
{
strSql.Append("credentials_no='" + model.credentials_no + "',");
}
if (model.issue_time != null)
{
strSql.Append("issue_time='" + model.issue_time + "',");
}
if (model.box_id != null)
{
strSql.Append("box_id='" + model.box_id + "',");
}
if (model.room_id != null)
{
strSql.Append("room_id=" + model.room_id + ",");
}
if (model.cabinet_id != null)
{
strSql.Append("cabinet_id=" + model.cabinet_id + ",");
}
if (model.node_id != null)
{
strSql.Append("node_id=" + model.node_id + ",");
}
if (model.nodein_id != null)
{
strSql.Append("nodein_id=" + model.nodein_id + ",");
}
if (model.is_borrowed != false )
{
strSql.Append("is_borrowed=" + (model.is_borrowed ? 1 : 0) + ",");
}
if (model.archives_parcel_id != null)
{
strSql.Append("archives_parcel_id='" + model.archives_parcel_id + "',");
}
下面是where作为条件,
if (model.dossier_id != null)
{
strSql.Append("dossier_id='" + model.dossier_id + "',");
}
if (model.title != null)
{
strSql.Append("title='" + model.title + "',");
}
if (model.map_number != null)
{
strSql.Append("map_number='" + model.map_number + "',");
}
if (model.usufruct_name != null)
{
strSql.Append("usufruct_name='" + model.usufruct_name + "',");
}
if (model.use_term != null )
{
strSql.Append("use_term=" + model.use_term + ",");
}
if (model.end_time != null)
{
strSql.Append("end_time='" + model.end_time + "',");
}
if (model.land_type_name != null)
{
strSql.Append("land_type_name='" + model.land_type_name + "',");
}
if (model.location != null)
{
strSql.Append("location='" + model.location + "',");
}
if (model.owner_kind_name != null)
{
strSql.Append("owner_kind_name='" + model.owner_kind_name + "',");
}
if (model.former_owner_id != null)
{
strSql.Append("former_owner_id=" + model.former_owner_id + ",");
}
if (model.land_card_id != null)
{
strSql.Append("land_card_id='" + model.land_card_id + "',");
}
if (model.former_land_card_id != null)
{
strSql.Append("former_land_card_id='" + model.former_land_card_id + "',");
}
if (model.owner_name != null)
{
strSql.Append("owner_name='" + model.owner_name + "',");
}
if (model.credentials_type_name != null)
{
strSql.Append("credentials_type_name='" + model.credentials_type_name + "',");
}
if (model.credentials_no != null)
{
strSql.Append("credentials_no='" + model.credentials_no + "',");
}
if (model.issue_time != null)
{
strSql.Append("issue_time='" + model.issue_time + "',");
}
if (model.box_id != null)
{
strSql.Append("box_id='" + model.box_id + "',");
}
if (model.room_id != null)
{
strSql.Append("room_id=" + model.room_id + ",");
}
if (model.cabinet_id != null)
{
strSql.Append("cabinet_id=" + model.cabinet_id + ",");
}
if (model.node_id != null)
{
strSql.Append("node_id=" + model.node_id + ",");
}
if (model.nodein_id != null)
{
strSql.Append("nodein_id=" + model.nodein_id + ",");
}
if (model.is_borrowed != false )
{
strSql.Append("is_borrowed=" + (model.is_borrowed ? 1 : 0) + ",");
}
if (model.archives_parcel_id != null)
{
strSql.Append("archives_parcel_id='" + model.archives_parcel_id + "',");
}
CREATE PROC p_test
@para1 varchar(10)=null,
@para2 varchar(10)=null,
@para3 varchar(10)=null,
@para4 varchar(10)=null
AS
SET NOCOUNT ON
DECLARE @sql varchar(8000)
SET @sql='SELECT * FROM tbname WHERE 1=1'
IF @para1 IS NOT NULL
SET @sql=@sql+' AND col1='''+@para1+''''
IF @para2 IS NOT NULL
SET @sql=@sql+' AND col2='''+@para2+''''
IF @para3 IS NOT NULL
SET @sql=@sql+' AND col3='''+@para3+''''
IF @para4 IS NOT NULL
SET @sql=@sql+' AND col4='''+@para4+''''
EXEC(@sql)
GO
/*======================================================*/--2. 使用 sp_executesql 实现的动态参数存储过程
CREATE PROC p_test
@para1 varchar(10)=null,
@para2 datetime=null,
@para3 varchar(10)=null,
@para4 int=null
AS
SET NOCOUNT ON
DECLARE @sql nvarchar(4000)
SET @sql='SELECT * FROM tbname WHERE 1=1'
+CASE WHEN @para1 IS NULL THEN '' ELSE ' AND col1=@para1' END
+CASE WHEN @para2 IS NULL THEN '' ELSE ' AND col2=@para2' END
+CASE WHEN @para3 IS NULL THEN '' ELSE ' AND col3=@para3' END
+CASE WHEN @para4 IS NULL THEN '' ELSE ' AND col4=@para4' END
EXEC sp_executesql @sql,N'
@para1 varchar(10)=null,
@para2 datetime=null,
@para3 varchar(10)=null,
@para4 int=null
',@para1,@para2,@para3,@para4
GO
/*======================================================*/--3. 不使用动态 Transact-SQL 语句实现的动态参数存储过程
CREATE PROC p_test
@para1 varchar(10)=null,
@para2 datetime=null,
@para3 varchar(10)=null,
@para4 int=null
AS
SET NOCOUNT ON
SELECT * FROM tbname
WHERE (@para1 IS NULL OR col1=@para1)
AND (@para2 IS NULL OR col2=@para2)
AND (@para3 IS NULL OR col3=@para3)
AND (@para4 IS NULL OR col4=@para4)
1 :普通SQL语句可以用Exec执行 eg: Select * from tableName
Exec('select * from tableName')
Exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N 2:字段名,表名,数据库名之类作为变量时,必须用动态SQL eg:
declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname from tableName -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。
Exec('select ' + @fname + ' from tableName') -- 请注意 加号前后的 单引号的边上加空格 当然将字符串改成变量的形式也可
declare @fname varchar(20)
set @fname = 'FiledName' --设置字段名 declare @s varchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句会报错 declare @s Nvarchar(1000) -- 注意此处改为nvarchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句正确 3. 输出参数
declare @num int,
@sqls nvarchar(4000)
set @sqls='select count(*) from tableName'
exec(@sqls)
--如何将exec执行结果放入变量中? declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from tableName '
exec sp_executesql @sqls,N'@a int output',@num output
select @num
select *
from TB
where isnull(Col1,ColValue1)=ColValue1 and
isnull(Col12,ColValue12)=ColValue2 and....
这个同意