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
  • 相关阅读:
    javascript:showModelDialog注意点
    VS.NET 查找未使用过的方法
    JAVASCRIPT:style 中visibility和display之间的区别
    基于MapServer的WebGIS开发http://www.gisforum.net/show.aspx?id=1491&cid=27
    POJ 1845 Sumdiv
    xmu 1254.异或求和
    hdu 4282 A very hard mathematic problem
    POJ Longge's problem 2480
    hdu 1199 Color the Ball
    HDU 1492 The number of divisors(约数) about Humble Numbers
  • 原文地址:https://www.cnblogs.com/caoyinglai/p/3624509.html
Copyright © 2011-2022 走看看