首页 > 数据库 >sqlserver 存储过程 where Id in 传参

sqlserver 存储过程 where Id in 传参

时间:2022-10-31 17:25:58浏览次数:54  
标签:传参 set varchar StuID F201803271145 sqlserver Str where

 sqlserver 存储过程 where Id in 传参

----更新多条数据,不传参
update Students set StuName='12348888' where StuID in ('1','2')

 

----更新多条数据,传参
DECLARE   @StuID   Varchar(50)   
set   @StuID='1,2'--是从参数中得到字符  '3908,3934'   
update Students set StuName='123666888'
WHERE charindex(',' + cast(StuID as varchar) + ',',',' + @StuID + ',') > 0

 

commandTextSB.Append(" DECLARE @StuID Varchar(50) set @StuID=@StuIDAAA update Students set StuName=@StuName where charindex(',' + cast(StuID as varchar) + ',',',' + @StuID + ',') > 0");
//创建命令对象
                SqlCommand cmd = new SqlCommand(commandTextSB.ToString(), myConn);
                //执行命令
                string stuIDArray = "1,2";
                cmd.Parameters.Add(new SqlParameter("@StuIDAAA", stuIDArray));           
                cmd.Parameters.Add(new SqlParameter("@StuName", "81111118"));
                int exeInt = cmd.ExecuteNonQuery();
                Console.WriteLine(exeInt);

=========================================================

这是整形Int的

结果:

sqlRtGoodsID=  1000002,1002808,1002822,1002832,1002845

string sqlRtGoodsID = @" 
                    declare 
                    @Str varchar(max)
                     set @Str=( select (','+ convert(varchar(20), goodsID)) from PercenTageRt for xml path(''))
                    select substring( @Str,2,len(@Str)) ";
                var resultGoodsIDStr = await _dbBase.QueryFirstOrDefaultAsync<string>(sqlRtGoodsID);
                if (!string.IsNullOrEmpty(resultGoodsIDStr))
                {
                    sqlPage2And += " and goodsid not in (" + resultGoodsIDStr + ") ";
                }

 

========================================================

这是字符串varchar()的

declare 
                    @Str varchar(max)
                     set @Str=( select top 10 (','''+ convert(varchar(20), FlowID)+'''') from Trade where 1=1 and UserID='4023' for xml path(''))
                    select substring( @Str,2,len(@Str))

----------------------------------------------------------------------------------

//筛选Trade中的FlowID
                string sqlTradeLike = @" declare 
                                        @Str varchar(max)
                                        set @Str=( select (','''+ convert(varchar(50), t.FlowID)+'''') from Trade t where 1=1 "
                                        + sqlPage2Where
                                        + "for xml path('')) "
                                        + "select substring( @Str,2,len(@Str))";

                var resultTradeLike = _IDbConnection.QueryFirstOrDefault<string>(sqlTradeLike, request);
                string whereIn = resultTradeLike == null ? "'0'" : resultTradeLike;

-------------------------------------------------------------------------------

 

结果是这样的:

不为null的情况:

 

'F201803271145','F201803271145','F201803271145','F201803271145','F201803271145','F201803271145','F201803271145','F201803271145','F201803271145','F201803271145'

 

为null的情况:

 

'0'

 

里面的top 10 和FlowID字段名,Trade表等,可以自由调整。

 

最后C#里执行以下语句:

 

string sqlPage2 = @"                    
                                 select Row_number() over(order by tf.confirmDate desc) as                               rownum,tf.* 
                                 from 
                                 TradeFlow tf where tf.FlowID in ( " + whereIn+")";

 

搜索

复制

标签:传参,set,varchar,StuID,F201803271145,sqlserver,Str,where
From: https://www.cnblogs.com/jankie1122/p/11109071.html

相关文章

  • sqlserver 根据多个字段查询重复数据
     sqlserver根据多个字段查询重复数据如:表a abcdef 121231121231121232121232表中每个字段只要有一个不一样就算不重复 selecta,b,......
  • sqlserver 关于 case when is null 的查询
    sqlserver关于casewhenisnull的查询select*fromApInterSkuInfowhereBeginValue=convert(varchar(100),convert(datetime,'2017-12-0923:59:59.8',101),23)......
  • sqlserver convert datetime 和字符串之间的转换
     sqlserverconvertdatetime和字符串之间的转换--语句及查询结果:SelectCONVERT(varchar(100),GETDATE(),0)--0516200610:57AMSelectCONVERT(varchar(......
  • sqlServer执行计划分析
    SQLServerManagementStudio是一个交互式图形工具,使数据库管理员或开发人员可以编写查询、同时执行多个查询、查看结果、分析查询计划以及获得提高查询性能的帮助。如......
  • SQLServer备份信息查看
    查看数据文件和日志文件的备份记录SELECT[filegroup_name],[backed_up_page_count],[file_type],[file_size],[logical_name],[phy......
  • SqlServer 查询数据重复
    SQLServer查询数据重复1、查询单列重复select*fromtestwherenamein(selectnamefromtestgroupbynamehavingcount(name)>1)2、查询多列重复SELECT......
  • C#的LINQ select查询、where过滤、group分组、join关联
    语言集成查询(LanguageIntegratedQuery)是一系列直接将查询功能集成到C#语言的技术统称,用于在所有基于.NET平台的变成语言中更加直接地声明跨越、过滤和投射操作的一......
  • Docker 安装SqlServer、Mysql、MariaDB
    Docker安装SqlServer说明1.拉取镜像dockerpullmcr.microsoft.com/mssql/server:2019-latest2.运行dockerrun--namemssqlserver2019-d--restartunless-st......
  • sqlserver and or 的区别
    sqlserverandor的区别Or不加括号:查询结果36条数据,BID=21,BID=25匹配的Status的数据都会查询出来 selectBID,TradeID,[status],PaymentToBIdfromTradewhereBID......
  • sqlserver between
     sqlserverbetween----如果UpdateTime为''或者为null,就用InsertTime条件查询select*fromWxUserswhereBID='957'andParentID='5537821'and(COALESCE(UpdateT......