-- Declare Variables
DECLARE @ErrorMessage NVARCHAR(4000),
@ErrorSeverity INT,
@ErrorState INT,
@Last_Inserted_ID INT;
-- Try Catch Block
BEGIN TRY
-- Start Transaction
BEGIN TRANSACTION
INSERT INTO [dbo].[user_info]
([user_name]
,[pass])
VALUES
('U11'
,'U12')
-- Returns the last identity value inserted
SET @Last_Inserted_ID = @@IDENTITY;
INSERT INTO [dbo].[food_info]
([user_id]
,[food_type])
VALUES
(@Last_Inserted_ID
,'Food1')
PRINT 'Values inserted successfully...'
COMMIT TRANSACTION
END TRY
BEGIN CATCH
SELECT
@ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();
RAISERROR (
@ErrorMessage,
@ErrorSeverity,
@ErrorState
);
ROLLBACK TRANSACTION
END CATCH
重複の可能性http://stackoverflow.com/questions/4565195/mysql-how-to-insert-into-multiple-tables-with-foreign-keys –
あなたはMySQLを使用していますかMS SQL Server? (関与していない製品にはタグを付けないでください)。 – jarlh