我正在研究SQL Server
(2005,2008& 2012)
我想通过使用UDF
输入:
rrr123ddd4567ddd19828www2
123hhhsss124ss18762s
qq12349wsss12376ss
输出:
19828
18762
12349
My Trail如下:
DECLARE
@myString VARCHAR(1000),
@temp VARCHAR(100),
@position INT,
@ExecuteInsert nvarchar (500),
@FirstChar bit
SET @myString = 'rrr123ddd4567ddd19828www2'
SET @position = 1
SET @FirstChar = 1
WHILE @position <= LEN(@myString)
BEGIN
IF (ISNUMERIC(SUBSTRING(@myString,@position,1))) = 1
BEGIN
SET @temp = isnull(@temp,'') + SUBSTRING(@myString,@position,1)
SET @FirstChar = 1
END
ELSE /* The char is alphabetical */
BEGIN
if (@FirstChar= 1)
BEGIN
SET @temp = isnull(@temp,'') + ','
SET @FirstChar = 0
END
END
SET @position = @position + 1
END
IF (RIGHT(@temp,1) <> ',')
BEGIN
SET @temp = @temp + ','
END
SELECT @temp = REPLACE(','+ @temp + ',',',,','')
SELECT @temp = Replace (@temp,',','''),(''')
Select @temp = '(''' + @temp + ''')'
Create table #temp
(
col1 varchar(100)
)
SET @ExecuteInsert = 'insert into #temp values ' + @temp
Execute sp_executesql @ExecuteInsert
select top 1 col1 from #temp
where LEN(col1) = 5
drop table #temp
-- Output >> 19828
上一个查询适用于字符串输入,但我想在UDF
中使用此代码,可以将其用于列。
如果我在UDF中使用了上一个查询,则会出现以下错误:
无法从函数中访问临时表。
修改
如果我使用Table变量,我会收到下一个错误:
只能执行函数和一些扩展存储过程 来自一个函数。
任何帮助将不胜感激。
答案 0 :(得分:1)
CREATE FUNCTION udfTest
(
-- Add the parameters for the function here
)
RETURNS int
AS
BEGIN
-- Declare the return variable here
DECLARE
@Result int,
@myString VARCHAR(1000),
@temp VARCHAR(100),
@position INT,
@ExecuteInsert nvarchar (500),
@FirstChar bit
SET @myString = 'rrr123ddd4567ddd19828www2'
SET @position = 1
SET @FirstChar = 1
WHILE @position <= LEN(@myString)
BEGIN
IF (ISNUMERIC(SUBSTRING(@myString,@position,1))) = 1
BEGIN
SET @temp = isnull(@temp,'') + SUBSTRING(@myString,@position,1)
SET @FirstChar = 1
END
ELSE /* The char is alphabetical */
BEGIN
if (@FirstChar= 1)
BEGIN
SET @temp = isnull(@temp,'') + ','
SET @FirstChar = 0
END
END
SET @position = @position + 1
END
IF (RIGHT(@temp,1) <> ',')
BEGIN
SET @temp = @temp + ','
END
SELECT @temp = REPLACE(','+ @temp + ',',',,','')
SELECT @temp = Replace (@temp,',','''),(''')
Select @temp = '(''' + @temp + ''')'
Declare @tempTable TABLE
(
col1 varchar(100)
)
insert into @tempTable SELECT @temp
select top 1 @Result=col1 from @tempTable
where LEN(col1) = 5
return @Result
END
GO
答案 1 :(得分:0)
在这里,你是我对我的问题的回答,希望能帮助别人。
目标是创建UDF
函数,以便将其与列一起使用,而不仅仅是固定值。
该方法使用SplitString代替sp_executesql
用于拆分逗号分隔的字符串并在表中循环它的值。
<强>演示: - 强>
Create table DummyTable
( col1 varchar (100))
go
Insert into DummyTable values ('rrr123ddd4567ddd19828www2')
Insert into DummyTable values ('123hhhsss124ss18762s')
Insert into DummyTable values ('qq12349wsss12376ss')
go
/*
SplitString via Mudassar Khan
http://www.aspsnippets.com/Articles/Split-and-convert-Comma-Separated-Delimited-String-to-Table-in-SQL-Server.aspx
*/
Create FUNCTION SplitString
(
@Input NVARCHAR(MAX),
@Character CHAR(1)
)
RETURNS @Output TABLE (
Item NVARCHAR(1000)
)
AS
BEGIN
DECLARE @StartIndex INT, @EndIndex INT
SET @StartIndex = 1
IF SUBSTRING(@Input, LEN(@Input) - 1, LEN(@Input)) <> @Character
BEGIN
SET @Input = @Input + @Character
END
WHILE CHARINDEX(@Character, @Input) > 0
BEGIN
SET @EndIndex = CHARINDEX(@Character, @Input)
INSERT INTO @Output(Item)
SELECT SUBSTRING(@Input, @StartIndex, @EndIndex - 1)
SET @Input = SUBSTRING(@Input, @EndIndex + 1, LEN(@Input))
END
RETURN
END
GO
-------------------------------------
-------------------------------------
-------------------------------------
/*
My Own Function
*/
Create FUNCTION udfGetFirstFiveNumbers
(
@myString VARCHAR(1000)
)
RETURNS varchar(100)
AS
BEGIN
DECLARE
@temp VARCHAR(100),
@result Varchar (100),
@position INT,
@ExecuteInsert nvarchar (500),
@FirstChar bit
SET @position = 1
SET @FirstChar = 1
WHILE @position <= LEN(@myString)
BEGIN
IF (ISNUMERIC(SUBSTRING(@myString,@position,1))) = 1
BEGIN
SET @temp = isnull(@temp,'') + SUBSTRING(@myString,@position,1)
SET @FirstChar = 1
END
ELSE /* The char is alphabetical */
BEGIN
if (@FirstChar= 1)
BEGIN
SET @temp = isnull(@temp,'') + ','
SET @FirstChar = 0
END
END
SET @position = @position + 1
END
IF (RIGHT(@temp,1) <> ',')
BEGIN
SET @temp = @temp + ','
END
SELECT @temp = REPLACE(','+ @temp + ',',',,','')
SELECT @result = Item
FROM dbo.SplitString(@temp, ',')
where len(Item) = 5
return @result
END
GO
-- Test
select col1, dbo.udfGetFirstFiveNumbers(col1) as result
from DummyTable
<强>结果: - 强>