SqlServer存储过程用到一些总结 小白总结嘻嘻

声明:

 declare @i 

赋值

set i = 0

将多行的字段ID用逗号分开

select @ApplyIDs=stuff((select ','+ CONVERT(nvarchar(50),[ID])  from ApplyMain t where BillState<>1 and NextApproverNo = @BeforeUserNo for xml path('')), 1, 1, '');//'111,222,333'

执行动态SQl

set @Sql = 'update ApplyMain set NextApproverNo = '''+@AfterUserNo +''' where ID in ('+ @ApplyIDs+')'
EXEC sp_executesql @Sql

执行动态SQL求Count

 set @SqlDY =  'with cr as (select HigherDepID,ID from DepartmentInfo where ID =(select ID from DepartmentInfo
                 where  DepartmentName = '''+@DepartmentName+''' )
                union all select d.HigherDepID,d.ID from cr c inner join DepartmentInfo d on c.ID  = d.HigherDepID) 
                select @DYCount=count(*) from EmployeeInfo where DepartmentID in (select ID from cr ) and delflag = 0'
  EXEC sp_executesql @SqlDY,N'@DYCount int output',@DYCount OUTPUT

将逗号分隔的字符串拆开 eg:‘制造,工厂,科,系,班,A’

   set @Sql = 'with cr as (select HigherDepID,ID from DepartmentInfo_mb where ID= '+convert(varchar,@DepartmentID)+'  union all select d.HigherDepID,d.ID from cr c inner join DepartmentInfo_mb d on c.HigherDepID = d.ID) select @DepartmentName=stuff((select '',''+ DepartmentName from DepartmentInfo_mb t where  ID in  (select ID from cr)   and delFlag=0  order by ID  for xml path('''')), 1, 1, '''')';
   EXEC sp_executesql @Sql,N'@DepartmentName nvarchar(500) output',@DepartmentName OUTPUT
   set @idx =1;
   set @num =1;
   set @Delimiter = ',';
   set @dep_henkomae =null
   set @fac_henkomae =null;
   set @section_henkomae =null;
   set @subsection_henkomae =null;
   set @class_henkomae =null;
   set @shift_henkomae =null;
   while @idx != -1
   begin 
    SET @idx = CHARINDEX(@Delimiter,@DepartmentName);
    IF @idx != 0
       begin 
         SET @slice = LEFT(@DepartmentName,@idx - 1)
       end
    ELSE
       begin 
           SET @slice = @DepartmentName
        set @idx= -1;
       end
    SET @DepartmentName = RIGHT (@DepartmentName, LEN(@DepartmentName) - @idx)、
     if @num = 1 
     set @dep_henkomae = @slice;
    if @num = 2
     set @fac_henkomae = @slice;
    if @num = 3
     set @section_henkomae =@slice;
    if @num = 4
     set @subsection_henkomae = @slice;
    if @num = 5
     set @class_henkomae = @slice;
    if @num = 6
     set @shift_henkomae = @slice;
  set @num = @num+1
  end  

日期转为YYYY-MM-DD日期格式

convert(date, [CreateTime],111)

日期转为YYYY_MM_DD字符串格式、YYYY/MM/DD

convert(varchar(20), [CreateTime],23)、convert(varchar(20), [CreateTime],111)

数字转为字符串格式

convert(varchar(20), [Id])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值