私は実際にこれを行うプロジェクトに取り組んできました。私の解決策は空想ではないが、これまでのところ効果的であると証明されている。迷惑な部分はセットアッププロセスである。私は改善のための批判と提案に非常に寛容です。
- (私が行ってきた「プレースホルダ」の列を作成し、各PKEY/FKEYに必要なすべてのテーブル(私は新しい[ApplicationTableName]で行ってきた)
- の「ミラー」スキーマ/ DBを作成します。
- 既存のデータを1でインデックス付けされたプレースホルダキーにマップします(これは迷惑ですが、ランキング機能で実行できます)。
- プレースホルダキーでアプリケーションに降順で挿入します重要!)
- ランキング機能を使用して「ミラー」テーブルを更新する(例を参照)
- 必要なだけ多くのテーブルに渡って、drived/inserted値を使用して、必要に応じて繰り返します。
例:このスキーマを考える
...
CREATE TABLE Accounts (
AccountID int identity(1,1) not null,
Name varchar(500) not null
)
CREATE TABLE Users(
UserID int identity(1,1) not null,
AccountID int not null,
Name varchar(500) not null
)
CREATE TABLE NewUsers(
pUserID int not null,
UserID int not null,
AccountID int not null,
Name varchar(500)
)
そして、このデータ
INSERT INTO NewUsers VALUES
(1,0,0,'Bob'),
(2,0,0,'Sally'),
(3,0,0,'Jeff'),
(4,0,0,'Sam')
セイたびに、私たちはこれらを作成するアカウントを "作成" 4人のデフォルトユーザー...これは次のようになります
DECLARE @AccountID int --this is scalar, so we'll use scope_identity() to grab it.
INSERT INTO Account VALUES('MyNewAccountID')
SELECT @AccountID = SCOPE_IDENTITY()
--Prepare NewUsers w/ derived accountID
UPDATE NewUsers SET AccountID = @AccountID
--Do our "application" insert
INSERT INTO Users(AccountID,Name)
SELECT AccountID,Name
FROM NewUsers
ORDER BY pUserID DESC;
--Capture inserted ID's for use in other tables (where we've derived pUserID)
WITH InsertedUsers AS(
SELECT
--use dense rank, it handles fkey mappings too
DENSE_RANK() OVER(ORDER BY UserID DESC) as pUserID,
UserID
FROM Users
)
UPDATE NewUsers SET UserID = iu.UserID
FROM NewUsers nu
JOIN InsertedUsers iu
ON iu.pUserID = nu.pUserID
SELECT TOP 100 * FROM Account ORDER BY 1 DESC
SELECT TOP 100 * FROM Users ORDER BY 1 DESC
将来のテーブルにAppIDが必要な場合(派生したpUserIDがある場合)、pUserIDに参加してNewUsersから取得できます。
出典
2016-04-26 21:49:22
Ron
意味がありません –
@PouriaSharif何が意味がありませんか? –