1.多条数据使用逗号隔开
select invoiceApplyNo,stuff((select distinct ','+incomePlanNo from T1 where a.invoiceApplyNo=invoiceApplyNo for xml path('')),1,1,'') AS items from T1 a group by invoiceApplyNo
2.批量更新多条语句
方法一
update a set HIGH=b.NEW from SPEC1 a,tmpDOT b where a.high=b.old
方法二
UPDATE A SET HIGH=B.NEW FROM A LEFT JOIN B ON (A.HIGH=B.OLD)
方法三
update a set high = (select new from tmpdot where old=a.high ) from spec1 a
3.存储IN查询处理:
-- ============================================= -- 名称:字符串分隔成表 -- 维护:ifu25 -- 日期:2018/08/14 -- 说明:用于将类似'1,2,3,4,5,6'的字符串分解成多行,以便于在存储过程中使用 where id in(select * from StrSplitToTable(@Strs,',') ) -- ============================================= CREATE function func_StrSplitToTable ( @Strs varchar(max), --需要被分隔的字符串 @Split varchar(50) --分隔符 ) returns @Temp table (id varchar(50) null) as begin while (charindex(@Split, @Strs)<>0) begin insert @Temp(id) values (substring(@Strs, 1, charindex(@Split, @Strs)-1)) set @Strs=stuff(@Strs, 1, charindex(@Split, @Strs), '') end insert @Temp(id) values (@Strs) return end
调用示例:
SELECT * FROM table_temp id in (SELECT * FROM func_StrSplitToTable(@idStr,','));
方法2:
-- a.feeguid:是要查询的表字段。feeguidstr传过来的值 select * from a where charindex(','+ltrim(a.feeguid)+',',','+@feeguidstr+',')>0
3.使用IN参数化
public JsonResult Delete(string oidStr) { string[] oidArr=null; if (!string.IsNullOrEmpty (oidStr)) { oidArr = oidStr.Split(','); } try { string sqlStr = "DELETE FROM TABLE WHERE CAST(id as varchar(36)) IN @id"; //执行 param=new{id=oidArr} } catch (Exception) { return new JsonResult(new { Result ="Error"}); } return new JsonResult(new { Result ="OK"}); }
4.