Гость
Целевая тема:
Создать новую тему:
Автор:
Форумы / Microsoft SQL Server [игнор отключен] [закрыт для гостей] / Insert Into - Identity / 3 сообщений из 3, страница 1 из 1
13.05.2002, 15:16
    #32030003
Dima
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Insert Into - Identity
Помогите с решением проблемы!

В таблице у ключевого поля указано свойство Idenity - Yes,
поэтому новые номера присваиваются при добавлении записи автоматически,
но вот в этом то и проблема.
Была сделана связная таблица содержащая два вторичных ключа,
один из них должен быть из выше описанной таблицы.
Добавление записей в обе эти таблицы должно быть в одной транзакции.
Можно ли после добавления записи в таблицу с Idenity - Yes
получить(определить) значение ключа?

Спасибо!
...
Рейтинг: 0 / 0
13.05.2002, 15:31
    #32030005
ВладимирМ
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Insert Into - Identity
Есть системная переменная @@Identity, которая содержит последнее созданное в данном соединении значение поля со свойством Identity. Т.е логика примерно следующая:

DECLARE @LastIdentity Int
BEGIN TRANSACTION
INSERT INTO Tab1 (...) VALUES (...)
SET @LastIdentity=@@Identity
INSERT INTO Tab2 (Tab1_ID, ...) VALUES (@LastIdentity, ...)
COMMIT TRANSACTION
...
Рейтинг: 0 / 0
13.05.2002, 15:40
    #32030006
MX_Air
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Insert Into - Identity
Цитирую отсюда : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_globals_50u1.asp

@@IDENTITY

Returns the last-inserted identity value.

Syntax
@@IDENTITY

Return Types
numeric

Remarks
After an INSERT, SELECT INTO, or bulk copy statement completes, @@IDENTITY contains the last identity value generated by the statement. If the statement did not affect any tables with identity columns, @@IDENTITY returns NULL. If multiple rows are inserted, generating multiple identity values, @@IDENTITY returns the last identity value generated. If the statement fires one or more triggers that perform inserts that generate identity values, calling @@IDENTITY immediately after the statement returns the last identity value generated by the triggers. If a trigger is fired after an insert action on a table that has an identity column, and the trigger inserts into another table that does not have an identity column, @@IDENTITY will return the identity value of the first insert. The @@IDENTITY value does not revert to a previous setting if the INSERT or SELECT INTO statement or bulk copy fails, or if the transaction is rolled back.
...
Рейтинг: 0 / 0
Форумы / Microsoft SQL Server [игнор отключен] [закрыт для гостей] / Insert Into - Identity / 3 сообщений из 3, страница 1 из 1
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]