SQL 中传数组参数的变通方法:通过字符串分拆实现
来源:互联网 发布:linux close_wait 编辑:程序博客网 时间:2024/05/17 05:58
字符串分拆函数:
/**//****** 对象: UserDefinedFunction [dbo].[Split] 脚本日期: 04/23/2007 16:02:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Librading
-- Create date:
-- Description: 字符串分拆函数
-- =============================================
CREATE FUNCTION [dbo].[Split]
(
@Input varchar(4000), -- 输入字符串
@Separator varchar(4000) = ',' -- 分隔符
)
RETURNS
@Result TABLE
(
Value varchar(4000)
)
AS
BEGIN
DECLARE @InputLength int;
SET @InputLength = LEN(@Input);
DECLARE @SeparatorLength int;
SET @SeparatorLength = LEN(@Separator);
DECLARE @LastIndex int;
SET @LastIndex = 0;
DECLARE @CurrentIndex int;
SET @CurrentIndex = -1;
WHILE @CurrentIndex != 0
BEGIN
SET @CurrentIndex = CHARINDEX(@Separator, @Input, @LastIndex );
IF @CurrentIndex != 0
INSERT @Result
(
Value
)
VALUES
(
SUBSTRING(@Input, @LastIndex, @CurrentIndex - @LastIndex)
)
ELSE
INSERT @Result
(
Value
)
VALUES
(
SUBSTRING(@Input, @LastIndex, @InputLength + 1 - @LastIndex)
)
SET @LastIndex = @CurrentIndex + @SeparatorLength;
END
RETURN
END
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Librading
-- Create date:
-- Description: 字符串分拆函数
-- =============================================
CREATE FUNCTION [dbo].[Split]
(
@Input varchar(4000), -- 输入字符串
@Separator varchar(4000) = ',' -- 分隔符
)
RETURNS
@Result TABLE
(
Value varchar(4000)
)
AS
BEGIN
DECLARE @InputLength int;
SET @InputLength = LEN(@Input);
DECLARE @SeparatorLength int;
SET @SeparatorLength = LEN(@Separator);
DECLARE @LastIndex int;
SET @LastIndex = 0;
DECLARE @CurrentIndex int;
SET @CurrentIndex = -1;
WHILE @CurrentIndex != 0
BEGIN
SET @CurrentIndex = CHARINDEX(@Separator, @Input, @LastIndex );
IF @CurrentIndex != 0
INSERT @Result
(
Value
)
VALUES
(
SUBSTRING(@Input, @LastIndex, @CurrentIndex - @LastIndex)
)
ELSE
INSERT @Result
(
Value
)
VALUES
(
SUBSTRING(@Input, @LastIndex, @InputLength + 1 - @LastIndex)
)
SET @LastIndex = @CurrentIndex + @SeparatorLength;
END
RETURN
END
使用例子:
1、简单的例子:
select * from dbo.Split('a,b,c', ',');
2、拆分后的表作为 int 或者其他数据类型而不是字符串:
DECLARE @Table TABLE
(
ID int
);
INSERT @Table SELECT * FROM [dbo].[Split]('1---2---3---4---5', '---');
SELECT * FROM @Table;
(
ID int
);
INSERT @Table SELECT * FROM [dbo].[Split]('1---2---3---4---5', '---');
SELECT * FROM @Table;
- SQL 中传数组参数的变通方法:通过字符串分拆实现
- SQL 中传数组参数的变通方法:通过字符串分拆实现
- Sql Server存储过程数组类型参数的变通方法
- Sql数组类型的变通方法
- 给SQL Server传送数组参数的变通办法
- 给SQL Server传送数组参数的变通办法
- 给SQL Server传送数组参数的变通办法
- 给SQL Server传送数组参数的变通办法
- 给SQL Server传送数组参数的变通办法
- 给SQL Server存储过程,传送数组参数(多个参数)的变通办法
- 查看Hibernate参数值的变通方法
- 通过hashmap实现统计字符串数组中某个字符串出现的次数的方法
- SQL Server 2005 存储过程不支持数组类型参数的变通办法
- 给SQL Server存储过程,传送数组参数的变通办法
- 可变参数的实现是通过数组的方法?
- UpdatePanel中使用FileUpload的变通方法
- 给SQLServer传送数组参数的变通办法
- php通过变通方法检测系统的文件夹路径编码
- .Net中的设计模式——Decorator模式
- 没事写写
- Synchronized使用心得
- ASP.NET里常用的JS
- 春
- SQL 中传数组参数的变通方法:通过字符串分拆实现
- Java的中文处理学习笔记:Hello Unicode
- ASP.NET 程序中常用代码
- 学习Linux的七点忠告
- Test
- String.getBytes()的问题
- 精彩的近乎完美的分页存储过程
- delphi杂记
- SQL Server通用分页存储过程:利用SQL Server未公开的存储过程实现