0
トリガを初めて使用しました。私は私が今までのレコードを挿入は、トリガがパスを更新するトリガーを記述しようとしているこのSQL Serverの単純な挿入トリガーの問題
CREATE TABLE [dbo].[Positions](
[Id] [int] IDENTITY(1,1) NOT NULL,
[ParentId] [int] NOT NULL,
[Path] [varchar](100) NULL,
[Title] [nvarchar](200) NOT NULL,
[Description] [nvarchar](1000) NULL
)
のようなテーブルを持っています。
The Path = the path of parent +/+ Id of new inserted record
。
私はこのようなトリガーを持っていますが、それはいつも1
をPath
の列に設定してください。詳細は
ALTER trigger [dbo].[ti_updatepath]
on [dbo].[Positions]
after insert
as
begin
declare @NewId int = (select Id from Inserted)
declare @NewParentId int = (select parentId from Inserted)
declare @ParentPath varchar ;
set @ParentPath = (select path from positions where Id = @NewParentId)
declare @Path varchar;
set @path = @ParentPath + '/'+ convert(varchar ,@NewId)
update Positions set Path = @path where Id= @NewId
end
、私のテーブルは次のように人口:あなたはその長さを指定する必要が文字データ型を宣言する場合
Id ParentId Path
1 0 1/
2 1 1/2
3 2 1/2/3
5 2 1/2/5
6 4 1/2/6
7 2 1/2/7
8 2 1/2/8
9 2 1/2/9
10 2 12/10
13 2 1/2/13
14 2 1/2/14
15 2 1/2/15
16 2 1/2/16
17 8 1/2/8/17
18 8 1/2/8/18
19 8 1/2/8/19
20 17 1/2/8/17/20
パス変数の長さは260文字です。 (長いあなたは。\\?\接頭辞を持つ長いパスフォームを許可している場合)http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx#maxpath –
@FilipDeVosを参照してください、Iあなたのコメントをアップアップしたのは、特定のケース(Windowsプラットフォーム、ファイルパス)に対して素晴らしい提案だったからです。しかし、OPはパスが何に関連するか(カテゴリ、ウィンドウのパスなど)を指定していませんでした。 –
私はMS SQL ServerをWindowsと仮定しました。途中で型も 'NVARCHAR'でなければなりません。一度噛んだら、二度恥ずかしがり屋。 –