2016-03-26 43 views
4

içinde bir tablo değişkeni bildirmek ve bu benim tek outcomes tablodur:Ben örnek bir sorgu üzerinde çalışıyorum bir kullanıcı tanımlı işlevin

**ship** 
Bismarck 
California 
California 
Duke of York 
Fuso 
Hood 
King George V 
Kirishima 
Prince of Wales 
Rodney 
Schamhorst 
South Dakota 
Tennessee 
Washington 
West Virginia 
Yamashiro 

ben ilk ve son boşluklar arasındaki karakterleri değiştirmek üzerinde çalışıyorum * ile dizelerde. Ve doğrudur aşağıdaki kodu, var: kodunun çalıştığını ama çok zorlanmadan yeniden kullanabilirsiniz böylece kullanıcı tanımlı işlevi içinde bir tablo değişkeni yapmak istiyorum

select 
     left(ship, charindex(' ', ship) - 1) + ' ' + 
     replicate('*', charindex(' ', substring(ship, charindex(' ', ship) + 1, len(ship))) + 1 -2) + ' ' + 
     reverse(left(reverse(ship), charindex(' ', reverse(ship)) - 1)) 
from outcomes 
where charindex(' ', substring(ship, charindex(' ', ship) + 1, len(ship))) > 1; 

. Kod Aşağıdaki tablo olarak değişkendir bildirmek için kullanılır ve doğru:

declare @ship_outcome table 
( final_work nvarchar(30) 
) 

insert into @ship_outcome (final_work) 
select 
     left(ship, charindex(' ', ship) - 1) + ' ' + 
     replicate('*', charindex(' ', substring(ship, charindex(' ', ship) + 1, len(ship))) + 1 -2) + ' ' + 
     reverse(left(reverse(ship), charindex(' ', reverse(ship)) - 1)) 
from outcomes 
where charindex(' ', substring(ship, charindex(' ', ship) + 1, len(ship))) > 1; 

select * from @ship_outcome 

sorun, bunu bir kullanıcı tanımlı işlevi olmak için aşağıdaki kod kullanıldığında:

CREATE FUNCTION dbo.shippad (@tbl nvarchar(30)) 
RETURNS TABLE 
AS 
RETURN 

    declare @ship_outcome table 
    (
     final_work nvarchar(30) 
    ) 

    insert into @ship_outcome 

    select 
     left(ship, charindex(' ', ship) - 1) + ' ' + 
     replicate('*', charindex(' ', substring(ship, charindex(' ', ship) + 1, len(ship))) + 1 -2) + ' ' + 
     reverse(left(reverse(ship), charindex(' ', reverse(ship)) - 1)) 
    from outcomes 
    where charindex(' ', substring(ship, charindex(' ', ship) + 1, len(ship))) > 1 
    select * from @ship_outcome 
; 

sistem, bu yanlış anladığımı anlayamıyorum. Lütfen yardım et.

cevap

3

sözdizimi Sen multi statement table valued function

CREATE FUNCTION dbo.Shippad (@tbl NVARCHAR(30)) 
RETURNS @ship_outcome TABLE (
    final_work NVARCHAR(30)) 
AS 
    BEGIN 
     INSERT INTO @ship_outcome 
     SELECT LEFT(ship, Charindex(' ', ship) - 1) + ' ' 
      + Replicate('*', Charindex(' ', Substring(ship, Charindex(' ', ship) + 1, Len(ship))) + 1 -2) 
      + ' ' 
      + Reverse(LEFT(Reverse(ship), Charindex(' ', Reverse(ship)) - 1)) 
     FROM outcomes 
     WHERE Charindex(' ', Substring(ship, Charindex(' ', ship) + 1, Len(ship))) > 1 

     RETURN 
    END; 

için BEGIN..END kullanmak gerekir Inline table valued function

den multi statement table valued function için biraz farklı Ama bu

CREATE FUNCTION dbo.Shippad (@tbl NVARCHAR(30)) 
RETURNS TABLE 
AS 
    RETURN 
     SELECT LEFT(ship, Charindex(' ', ship) - 1) + ' ' 
      + Replicate('*', Charindex(' ', Substring(ship, Charindex(' ', ship) + 1, Len(ship))) + 1 -2) 
      + ' ' 
      + Reverse(LEFT(Reverse(ship), Charindex(' ', Reverse(ship)) - 1)) as final_work 
     FROM outcomes 
     WHERE Charindex(' ', Substring(ship, Charindex(' ', ship) + 1, Len(ship))) > 1 
yapmak Inline table valued function tercih edecektir
İlgili konular