Showing posts with label trigger. Show all posts
Showing posts with label trigger. Show all posts

Friday, March 23, 2012

I want write a Trigger that make a 3nd table that contain all the record

Hi
I want to write Trigger tha do this:
I have 2 table

main table & sub main table that have data like this
for example I have Bank (code 001) in main table and visa(code 0001) & mastercard(code 0002) in submain table.

or I have BMW(code 101) in main table and X5(code 0001) & X3(code 0001) in sub main table.

I want write a Trigger that make a 3nd table that contain all the record of that two table like this:

NAME CODE
Bank 001
visa card 0010001
master card 0010002

BMW 101
X5 1010001
X3 1010002
..........................................

Trigger MUST make code for any record ( main code * 1000 + submain code ) that is unic. and record name.

1) Set a foreign key in the "submain" table to reference the unique 3-digit codes in the main table.

2) Write an INSERT tigger on the "submain" table that inserts a JOIN of main+submain into the 3rd table.

Something like what you see below. You could also use a computed column as explained in the other post.

Thanks

set nocount on
go

use tempdb
go

create table main(
the_name char(6) not null primary key
, the_main_code char(3) not null constraint main_unique unique
)
go

insert into main values ('Banks', '001')
insert into main values ('Goods', '101')
go

create table submain(
the_name varchar(20) not null primary key
, the_submain_code char(4) not null
, the_main_code char(3) foreign key references main(the_main_code)
)
go

create table combined (
the_main_name char(6)
, the_submain_name varchar(20)
, the_combined_code char(7)
)
go

create trigger make_combined on submain
for insert
as
insert into combined
select main.the_name, inserted.the_name, inserted.the_main_code + inserted.the_submain_code
from main join inserted
on main.the_main_code = inserted.the_main_code
go


insert into submain (the_name, the_submain_code, the_main_code) values ('visa card', '0001', '001')
insert into submain (the_name, the_submain_code, the_main_code) values ('master card', '0002', '001')
insert into submain (the_name, the_submain_code, the_main_code) values ('BMW X5', '0001', '101')
insert into submain (the_name, the_submain_code, the_main_code) values ('BMW X3', '0002', '101')
go

select * from combined order by the_combined_code
go


drop table submain
go
drop table main
go
drop table combined
go

Sunday, February 19, 2012

I need Help

Hi Freinds,
I need some help on SQL Server.
I need to create a trigger on the update of a particular field in a table.
This trigger has to perform some action after an hour since this field is updated.
Java has a sleep function, does SQL also have something like that.

Please (if ull can)Reply on my personal id.
parul.gulati@.india.birlasoft.com

Thanks and Regards,
ParulSQL Server has:

WAITFOR { DELAY 'time' | TIME 'time' }

The WAITFOR statement suspends the execution of a connection until either:

i) specified time interval has passed.
ii) specified time of day is reached.

Example:
WAITFOR DELAY '00:00:02'

However I would not use this within a trigger. Since a trigger is fired on a event (INSERT,UPDATE,DELETE), that event will also be suspend for an hour (your example), untill the trigger has completed.

If you need to perform some sort of action after an hour, look at:

1) use xp_cmdshell to call an external program which will sleep for the hour, then connect to the DB and perform the action.

2) Look at sp_OA procedures (sp_OACreate, sp_OAMethod, ...) which could be used to run a COM object program.

3) Have the trigger load a seperate table or set a flag indicating an action to be taken, then have a seperate stored procedure perform your work for you, the procedure could be scheduled using SQL Server's scheduler to run every 15 minutes or so.