Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 tables which are my_school and my_class

And "my_school" table has 'info_id' column and also "my_class" table has 'info_id' then I want to get a query that automatically generate "info_id" then I found solution..

Here are my working TRIGGER on "my_school" table...

SQL
CREATE OR REPLACE TRIGGER info_id

  before insert on my_direction              

  for each row

begin   

 if :NEW.WAY_ID is null then 

    :NEW.WAY_ID := example_id_seq.nextval; 

  end if;

  end;


It works and it's generating auto id when inserting value. But now how to get this trigger when users insert value then take id with my_school's "info_id" same time?
Posted
Updated 29-Mar-14 10:28am
v3

As per my knowledge..You should not use trigger here. Create a SP that inserts data in to your two tables.
Before inserting to any table store example_id_seq.nextval in to a varibale and use the same variable in to both the table insert statements.
 
Share this answer
 
You can add to multiple tables through Procedure in sql...


procedure
SQL
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		
-- Create date: 
-- Description:	
-- =============================================
ALTER PROCEDURE [dbo].[P_ItemMaster] 
	-- Add the parameters for the stored procedure here
@ItemId int=0,
@ItemName varchar(100)=null,
@SubCategory_Id int=0,
@DenominationId int=0,
@Created_By varchar(50)=null,
@Created_On varchar(100)=null,
@Modified_By varchar(50)=null,
@Modified_On varchar(100)=null,
@COND VARCHAR(1)=null,
@ItemCode VARCHAR(100)=NULL
	
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here

IF(@COND='I' OR @COND='i')
begin
insert into T_Itemmaster(ItemName,ItemCode,SubCategory_Id,DenominationId,Created_By,Created_On) values(@ItemName,@ItemCode,@SubCategory_Id,@DenominationId,@Created_By,@Created_On)
insert into T_item_masterDeatils(.......) values(......)

end

END
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900