62,041
社区成员
发帖
与我相关
我的任务
分享
string strName,strNo,strAddr;
string strSqlWhere;
if (!string.IsNullOrEmpty(strName)) strSqlWhere = strSqlWhere + " NAME LIKE " + strName.ToString().Trim();
if (!string.IsNullOrEmpty(strNo))
{
if (string.IsNullOrEmpty(strSqlWhere)) strSqlWhere = strSqlWhere + " NO LIKE " + strNo.ToString().Trim();
else strSqlWhere = strSqlWhere + " OR NO LIKE " + strNo.ToString().Trim();
}
if (!string.IsNullOrEmpty(strAddr))
{
if (string.IsNullOrEmpty(strSqlWhere)) strSqlWhere = strSqlWhere + " ADDR LIKE " + strAddr.ToString().Trim();
else strSqlWhere = strSqlWhere + " OR ADDR LIKE " + strAddr.ToString().Trim();
}
if (!string.IsNullOrEmpty(strSqlWhere)) strSqlWhere = " WHERE " + strSqlWhere;
begin
set nocount on
declare @s nvarchar(4000)
select @s='Select * from table where 1=1'
+case when @Name>'' then ' and Name like @Name' else '' end
+case when @id>'' then ' and id like @id' else '' end
+case when @address>'' then ' and address like @address' else '' end
exec sp_executesql @s,
N'@Name varchar(50)=null,
@id varchar(50)=null,
@address varchar(50)
'
,@Name,@id,@address
end