2016-11-15 65 views
1
前五個數字形式的字符串

我的工作SQL Server(2005,2008 & 2012)返回內UDF

我想通過使用UDF

輸入提取VARCHAR列前五位的數字:

rrr123ddd4567ddd19828www2 
123hhhsss124ss18762s 
qq12349wsss12376ss 

輸出:

19828 
18762 
12349 

我的蹤跡是如下:

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前面的查詢,下面的錯誤認識:

Cannot access temporary tables from within a function.

編輯

如果我用表變量,我得到了一個錯誤:

Only functions and some extended stored procedures can be executed from within a function.

任何幫助將不勝感激。

+0

更改代碼以使用表變量,而不是一個臨時表。 –

+0

@ R.Richards將其更改爲表變量後,我得到此錯誤找不到列「dbo」或用戶定義函數或聚合「dbo.GetFirstFiveNumbers」,或名稱不明確。 –

回答

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 

結果: -

enter image description here

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 
+0

如何在內聯函數中使用表變量? ..返回表後出現下一個錯誤>>>無法找到列「dbo」或用戶定義的函數或聚合「dbo.GetFirstFiveNumbers」,或名稱不明確。 –

+0

請參閱http://sqlhints.com/tag/table-variables-in-user-defined-function/關於如何在用戶定義的函數內使用表變量 –

+0

它不工作,主要問題我想返回值來自@tempTable,內聯函數怎麼來? –