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
 General SQL Server Forums
 New to SQL Server Programming
 Trigger

Author  Topic 

barnabeck
Posting Yak Master

236 Posts

Posted - 2012-05-23 : 11:58:38
After defining a trigger that reacts on the insertion of a new record, I can not launch the insert comand as I used to do through a web-application that uses the asp:sqldatasource module.

As soon as I remove the trigger, everything works the way it used to. Obviously there is a conflict between the asp insert command and the trigger itself. The trigger works smoothly if I manually insert the record.

Anyone a clue of how to avoid this?
Regards, Martin

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-05-23 : 13:46:39
I can't think of anything, so you'll need to show us the code starting with the trigger.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-23 : 14:42:10
quote:
Originally posted by barnabeck

After defining a trigger that reacts on the insertion of a new record, I can not launch the insert comand as I used to do through a web-application that uses the asp:sqldatasource module.

As soon as I remove the trigger, everything works the way it used to. Obviously there is a conflict between the asp insert command and the trigger itself. The trigger works smoothly if I manually insert the record.

Anyone a clue of how to avoid this?
Regards, Martin


what are you doing insert the trigger? is it doing some explicit transaction with rollback?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

barnabeck
Posting Yak Master

236 Posts

Posted - 2012-05-24 : 03:34:40
The trigger just sends a database-Mail to confirm that a new request came in:
ALTER TRIGGER [dbo].[TicketMailer] 

ON [dbo].[tickets]

AFTER INSERT


AS BEGIN

IF EXISTS (SELECT top 1 correo FROM tickets order by CREATEDDATE desc)

BEGIN

DECLARE @contenido varchar(4000),@str varchar(4000)

SELECT top 1 @str = correo, @contenido = 'Hola ' + remitente + CHAR(13) + CHAR(13) +'Confirmamos de ...' + CHAR(13)+ 'Descripción: '+ descripcion FROM tickets order by CREATEDDATE DESC;

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Help_Desk',
@recipients = @str,
@blind_copy_recipients = 'xxxx@xxx.xx',
@body = @contenido,
@subject = 'Help Desk';
END

END
while the aspx page collects input date and inserts the record:
<asp:SqlDataSource runat="server" id="SqlDataSource1" ProviderName="System.Data.SqlClient" ConnectionString="Data Source=XXXXXX;Initial Catalog=XX;User ID=XXXXX;Password=XXXXX" 
InsertCommand="INSERT INTO dbo.tickets ([titulo],[descripcion],[number],[remitente], [autor], [correo])
Values (@titulo, @descripcion, (select top 1 case when (number = NULL OR number ='999') THEN '1' ELSE number+1 END from tickets order by createddate desc), @remitente, @autor,
(select email from plantilla where plantilla.name = @remitente))"


<asp:FormView runat="server" id="FormView1" DefaultMode="Insert" DataSourceID="SqlDataSource1" AllowPaging="false">

<InsertItemTemplate>
<table>
<tr><td>Titulo:</td><td>
<asp:TextBox Text='<%# Bind("Titulo") %>' runat="server" id="TituloTextBox" />
</td></tr><tr><td>
Descripción:</td><td>
<asp:TextBox Text='<%# Bind("descripcion") %>' wrap="true" TextMode="MultiLine" runat="server" id="TextBox_Descripcion" style="overflow:auto;" />
</td></tr><tr><td>
Autor: </td><td>
<asp:RadioButtonList id=RadioButtonAutor runat="server" RepeatDirection="Horizontal" SelectedValue='<%# Bind("autor") %>' >
<asp:ListItem>Óscar</asp:ListItem>
<asp:ListItem>Martin</asp:ListItem>
<asp:ListItem selected>IT</asp:ListItem>
</asp:RadioButtonList>
</td></tr><tr><td>
Remitente:</td><td>
<asp:SqlDataSource runat="server" id="ADLookUp" ProviderName="System.Data.SqlClient" ConnectionString="Data Source=XXXXX;Initial Catalog=IT;User ID=XXXX;Password=XXXXX"
SelectCommand="SELECT DISTINCT(CASE WHEN name IS NULL THEN ' select' ELSE name END) AS who FROM plantilla FULL OUTER JOIN wildcard ON plantilla.name = wildcard.wildcard">
</asp:SqlDataSource>
<asp:DropDownList SelectedValue='<%# Bind("remitente") %>' runat="server" id="remitenteDropdown" DataValueField="who" DataTextField="who" DataSourceID="ADLookUp" >
</asp:DropDownList>
</td></tr>
</table>
<asp:LinkButton runat="server" Text="Saca el Ticket" CommandName="Insert" id="InsertButton" CausesValidation="false" />
</InsertItemTemplate>
</asp:FormView>

edit: to fix very long line
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-05-24 : 12:19:02
You shouldn't send email through a trigger as it's part of the transaction and thus making it slower. Maybe you are running into a timeout issue on the VS side?

Move the email part to a job instead, have the trigger write to a queue table that the job will read. The job would execute every minute and would determine if any emails need to be sent out.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-24 : 15:05:10
Only thing is it would be a bit asynchronous. But still should suffice for you as its just notification.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

barnabeck
Posting Yak Master

236 Posts

Posted - 2012-05-25 : 07:43:39
There must be something else that I'm missing. I did as you suggested and created a trigger that launches the job that then sends the mail. It didn't change anything.

It seems that as soon as I use the command 'exec' inside the trigger or the job, it interfers in some way with the insert command of the asp.net page. If I place a regular expression ('update of a column') inside of the trigger, everything works fine.
If I set up the trigger to launch a simple job ('update of a column') it doesn't work either. So it's not the execution of that specific sp_send_dbmail, but the EXEC in general that causes trouble.

Stuck
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-25 : 09:50:11
quote:
Originally posted by barnabeck

There must be something else that I'm missing. I did as you suggested and created a trigger that launches the job that then sends the mail. It didn't change anything.

It seems that as soon as I use the command 'exec' inside the trigger or the job, it interfers in some way with the insert command of the asp.net page. If I place a regular expression ('update of a column') inside of the trigger, everything works fine.
If I set up the trigger to launch a simple job ('update of a column') it doesn't work either. So it's not the execution of that specific sp_send_dbmail, but the EXEC in general that causes trouble.

Stuck


Sorry I thing you missed Tara's point
She was not suggesting to launch job from trigger

She was suggesting to populate a job queue table with required data inside trigger
The job will be scheduled in SQL Server agent so it will execute as per schedule regardless of trigger. Each time it runs it checks the job queue table and if it finds any record pending to be processed, it will do processing. To facilitate this you can add a status column in table which is set to pending by trigger and changed to processed by job. Each time job runs it specifically looks for records with pending status and if found, it does required processing and sends the mail. After that it changes status to Processed.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

barnabeck
Posting Yak Master

236 Posts

Posted - 2012-05-25 : 14:13:38
Ok, I see. That certainly is a solution although it implies generating a job that is launched every couple of minutes for nothing... at least in 95% of the cases.
I will think of a workaround. Would be cool to link schedule time to a column of that database to indirectly gain control, but I guess these job parameters are static.
Anyway thank you for your help
Martin
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-05-25 : 14:29:00
Even if it does nothing 95% of the time, it won't impact your resources. It is very lightweight. This is a common solution out there.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

barnabeck
Posting Yak Master

236 Posts

Posted - 2012-05-25 : 15:16:33
Thank you Tara, good to know that such a job wouldn't weight to heavy on the resources; I think I have to dig deeper into the almost unlimited possibilities with asp to get that immediate response as soon as a record has been added
Go to Top of Page
   

- Advertisement -