0

クライアントに入るために、MS SQL SERVER 2008 でストアド プロシージャを作成しています。これが私のコードの重要な部分です:

INSERT INTO Entity VALUES (@nameEnt, @iniEnt, @LastNEnt1, @suffixEnt);
INSERT INTO Patient VALUES (@housing, @dob, IDENT_CURRENT('Entity'));
INSERT INTO Direction VALUES (@postAdd, @city, @state, @zip, IDENT_CURRENT('Entity'));
INSERT INTO PatientSecure VALUES (IDENT_CURRENT('Patient'), @idSecure, @contractNo, @groupNo, @coverage)

おそらく、SELECTでIDENT_CURRENTが使用されているのを見ただけなので、機能しません。ただし、最初の行が挿入された後にEntityで生成されたidEntityが、 Entityで生成された同じ idである必要がある行idEntityのPatientテーブルにあるようなことをする必要があります。Direction と PatientSecure についても同様です。

これを行うためのより良い方法がある場合は、それを提案してください。

助けてください!そして素敵に:)ありがとう

4

2 に答える 2

0

Note that IDENT_CURRENT returns a value that is independent of session and scope. A different user might cause the value the change between your INSERT statements.

You can use Scope_Identity():

declare @EntityId as Int;
INSERT INTO Entity VALUES (@nameEnt, @iniEnt, @LastNEnt1, @suffixEnt);
set @EntityId = Scope_Identity();
declare @PatientId as Int;
INSERT INTO Patient VALUES (@housing, @dob, @EntityId);
set @PatientId = Scope_Identity();
INSERT INTO Direction VALUES (@postAdd, @city, @state, @zip, @EntityId);
INSERT INTO PatientSecure VALUES (@PatientId, @idSecure, @contractNo, @groupNo, @coverage)

Or you could use an OUTPUT clause on the INSERTs to save the appropriate values. While it may be overkill in this case, it is a valuable tool to know. It works with DELETE, INSERT and UPDATE statements, handles multiple rows and (where applicable) provides access to before and after values.

于 2013-08-31T01:26:13.083 に答える
0

I would replace all of your calls to ident_current('some_table') with calls to scope_identity().

于 2013-08-31T01:26:59.960 に答える