码迷,mamicode.com
首页 > 数据库 > 详细

Sqlserver表值函数来获取逗号分隔的ID

时间:2019-11-06 00:48:34      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:sources   returns   object   ===   date   server   cti   crm   lse   

其功能为: 将字符串如‘1,2,3,4,5,6‘ 拼接成SQL里面的id
1:使用:

 select * from Student where id  IN(
         SELECT * FROM dbo.F_SPLIT(1,2,3,4,,)
    )
2:定义表值函数:
USE [CRM_CN2]
GO
/****** Object:  UserDefinedFunction [dbo].[F_SPLIT]    Script Date: 2019/6/28 17:22:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 =============================================
 Author:        <Author,,Name>
 Create date: <Create Date,,>
 Description:    <Description,,>
 =============================================
ALTER  FUNCTION [dbo].[F_SPLIT]
(
    @SourceSql nvarchar(4000),
    @StrSeprate varchar(10)
)
RETURNS @temp TABLE(Temp_Field nvarchar(2000))

AS
BEGIN
        declare @i int
        set @SourceSql = rtrim(ltrim(@SourceSql))
        
        set @i = charindex(@StrSeprate, @SourceSql)
        while @i >= 1
        begin
            insert @temp values(left(@SourceSql, @i - 1))
            set @SourceSql = substring(@SourceSql, @i + 1, len(@SourceSql) - @i)
            set @i = charindex(@StrSeprate, @SourceSql)
        end
        if @SourceSql <> \‘
            insert @temp values(@SourceSql)
        RETURN
    END

 

 

Sqlserver表值函数来获取逗号分隔的ID

标签:sources   returns   object   ===   date   server   cti   crm   lse   

原文地址:https://www.cnblogs.com/Fengge518/p/11802659.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!