4
内のテーブル変数を宣言し、これは私が得た唯一のoutcomes
テーブルです:私はサンプルクエリに取り組んでいます、ユーザー定義関数
**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
私は最初と最後の空白の間の文字を置き換えるに取り組んでいます文字列には*
が含まれています。そして、私は正しいことを次のコードを、得た:コードが動作しているが、私は、私は多くの困難もなく、それを再利用できるように、ユーザー定義関数内のテーブル変数をしたいと思い
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;
。 Iは、テーブル変数を宣言するために使用されるコードは、以下の通りであると正しい:
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
問題は、私はそれのユーザ定義関数にするために、次のコードを使用した場合、である。
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
;
システムはそうだIncorrect syntax near the keyword 'declare'.
私はこれがどうやって間違っているのか分かりません。助けてください。