zoukankan      html  css  js  c++  java
  • SQL字串截取函数编写及应用

    SQL里面一种用来截取字符串的方法,用的是表函数实现字符串截取并应用的SQL操作语句中。
    1、截取字符串表函数
    
    ALTER FUNCTION [dbo].[SplitToTable]
    (
        -- Add the parameters for the function here
        @SplitString nvarchar(max),
        @Separator nvarchar(10)=''
    )
    RETURNS
    @SplitStringsTable TABLE
    (
        -- Add the column definitions for the TABLE variable here
        [id] int identity(1,1),
        [value] nvarchar(max)
    )
    AS
    BEGIN
        -- Fill the table variable with the rows for your result set
        DECLARE @CurrentIndex int;
        DECLARE @NextIndex int;
        DECLARE @ReturnText nvarchar(max);
        SELECT @CurrentIndex=1;
        WHILE(@CurrentIndex<=len(@SplitString))
            BEGIN
                SELECT @NextIndex=charindex(@Separator,@SplitString,@CurrentIndex);
                IF(@NextIndex=0 OR @NextIndex IS NULL)
                    SELECT @NextIndex=len(@SplitString)+1;
                    SELECT @ReturnText=substring(@SplitString,@CurrentIndex,@NextIndex-@CurrentIndex);
                    INSERT INTO @SplitStringsTable([value]) VALUES(@ReturnText);
                    SELECT @CurrentIndex=@NextIndex+1;
                END
        RETURN
    END
    
    2、应用表函数实现一次多语句添加数据
    
    ALTER PROCEDURE [dbo].[A_AccountPhotoInsert]
        -- Add the parameters for the stored procedure here
        @accid int,
        @accphoto nvarchar(max),
        @albumname nvarchar(50)
    AS
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
    
        -- Insert statements for procedure here
        DECLARE @Currentid int; --切割字符串表值函数的ID
        DECLARE @Excisionurl nvarchar(50); --字符串切割后的值,即要删除的ID
        SET @Currentid=1;--初始切割字符串表值函数的ID
    WHILE (@Currentid <= (SELECT COUNT(*) FROM SplitToTable(@accphoto,'|')))
            BEGIN
                SELECT @Excisionurl = (select value from SplitToTable(@accphoto,'|') WHERE id = @Currentid)
                insert into A_AccountPhone ([Accountid],[AccountPhoto],[AlbumName],[photoName],[BlockNone],[Headphoto])
                values(@accid,@Excisionurl,@albumname,'',0,0)
                SELECT @Currentid = @Currentid + 1;
            END
        SELECT @Currentid
    END
    
    3、应用表函数实现一次多表数据的删除
    
    ALTER PROCEDURE [dbo].[A_AccountDelete]
        -- Add the parameters for the stored procedure here
        @StrId nvarchar(max)
    AS
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
    
        -- Insert statements for procedure here
        DECLARE @Currentid int; --切割字符串表值函数的ID
        DECLARE @Excisionid int; --字符串切割后的值,即要删除的ID
        SET @Currentid=1;--初始切割字符串表值函数的ID
        
        WHILE (@Currentid <= (SELECT COUNT(*) FROM SplitToTable(@StrId,',')))
            BEGIN
    SET XACT_ABORT on
    BEGIN TRAN
                SELECT @Excisionid = (select value from SplitToTable(@StrId,',') WHERE id = @Currentid)
                DELETE FROM U_AccountInfo WHERE AccountID = @Excisionid
                DELETE FROM UserAccount WHERE id = @Excisionid
                SELECT @Currentid = @Currentid + 1;
    COMMIT TRAN
            END
        SELECT @Currentid
    END
  • 相关阅读:
    css实现截取文本
    ob_clean()解决php验证码图片无法显示
    JS获取url参数,修改url参数
    mysql模糊查询特殊字符(\,%和_)处理
    apache反向代理和监听多个端口设置
    页面底部自适应浏览器窗口高度
    变量相关考虑
    php非法输入数据类型
    php socket模拟http中post或get提交数据
    华为专利的 hybrid 端口
  • 原文地址:https://www.cnblogs.com/caoyinglai/p/3624509.html
Copyright © 2011-2022 走看看