Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 TRIGGERS!!

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-12-13 : 08:19:44
tony writes "Ok...here i go.First, i don´t speak (write) english as good as i´d like, so escuse me.

I´m having lots of troubles with triggers in my database that i didn´t have with the stored procedures. Here´s an example of i´m trying to do. The thing is to create a registre in the table "trabajos" when i insert a new registre on the table "contratos" Looks simple, but i can´t!!

*Is posible to asign the value of inserted table to a insert sentence?

insert into trabajos (ref_contr) values (inserted.ref_contr)
i don´t think so...

*In the table "trabajos" i have the foreign key of "contratos" named ref_contr and the primary key named ref_trab (autoincrementated)
For insert a new registre with the trigger i´m trying to do like this:

-------------------------------------------
CREATE TRIGGER t_contratos ON dbo.contratos
FOR INSERT
AS

declare @ref_contr int
set @ref_contr = (select ref_contr from inserted)

insert into trabajos (ref_trab) values (@ref_contr)
-------------------------------------------

The check is successful, but....

-------------------------------------------
*/*
Error Type:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
[Microsoft][ODBC SQL Server Driver][SQL Server]Cursorfetch: The number of variables declared in the INTO list must match that of selected columns.



--------------------------------------------

CREATE TRIGGER t_contratos ON dbo.contratos
FOR INSERT
AS
declare cuinserted2 cursor for select * from inserted i inner join contratos c on (i.ref_contr = c.ref_contr);

declare @ref_contr int, @num_contr varchar (20), @fech_contr datetime, @ref_ab int, @ref_agen int,
@ref_inst int, @ref_us int, @est_contr int, @tipo_contr varchar(20), @so_contr varchar(20), @idm_contr varchar(20),
@com_contr varchar(500), @ins_pref varchar(15), @ins_tjt varchar (15), @pdf_contr varchar (200)

begin

open cuinserted2

fetch from cuinserted2 into @ref_contr , @num_contr, @fech_contr, @ref_ab , @ref_agen,
@ref_inst , @ref_us , @est_contr , @tipo_contr , @so_contr, @idm_contr,
@com_contr, @ins_pref, @ins_tjt , @pdf_contr

while @@fetch_status = 0 begin

insert into trabajos (ref_contr) values (@ref_contr)

fetch from cuinserted2 into @ref_contr , @num_contr, @fech_contr, @ref_ab , @ref_agen,
@ref_inst , @ref_us , @est_contr , @tipo_contr , @so_contr, @idm_contr,
@com_contr, @ins_pref, @ins_tjt , @pdf_contr

end

close cuinserted2
deallocate cuinserted2

end
----------------------------------------

Here i had an extrange problem cause i have a cursor and when i finish i deallocated it. But the error says it already exists...

Please help me!"

nr
SQLTeam MVY

12543 Posts

Posted - 2004-12-13 : 08:50:21
Before reading any further does

insert into trabajos (ref_contr) select inserted.ref_contr
help.

Have a look at
http://www.nigelrivett.net/Triggers_1_A_beginners_guide.html

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -