| Author |
Topic |
|
pyromelana
Starting Member
23 Posts |
Posted - 2009-03-20 : 09:18:47
|
hi all!i would like to have my sql (sql server2005) result in a file, e.g. like txt or cvs or whatever readable ... this should finally work with a trigger. on the special event the trigger fires and sends an email with the file to someone... I managed to send me an email with file, but thats it. We work with sp_send_dbmail, because xp_sendmail does not work on our server. EXEC msdb..sp_send_dbmail @recipients = 'status@mydomain.com',@subject = 'yeahaaaaaaaaa '; --@body = @message;END Compared to the MS manual (see below) .... quote: sp_send_dbmail [ [ @profile_name = ] 'profile_name' ] [ , [ @recipients = ] 'recipients [ ; ...n ]' ] [ , [ @copy_recipients = ] 'copy_recipient [ ; ...n ]' ] [ , [ @blind_copy_recipients = ] 'blind_copy_recipient [ ; ...n ]' ] [ , [ @subject = ] 'subject' ] [ , [ @body = ] 'body' ] [ , [ @body_format = ] 'body_format' ] [ , [ @importance = ] 'importance' ] [ , [ @sensitivity = ] 'sensitivity' ] [ , [ @file_attachments = ] 'attachment [ ; ...n ]' ] [ , [ @query = ] 'query' ] [ , [ @execute_query_database = ] 'execute_query_database' ] [ , [ @attach_query_result_as_file = ] attach_query_result_as_file ] [ , [ @query_attachment_filename = ] query_attachment_filename ] [ , [ @query_result_header = ] query_result_header ] [ , [ @query_result_width = ] query_result_width ] [ , [ @query_result_separator = ] 'query_result_separator' ] [ , [ @exclude_query_output = ] exclude_query_output ] [ , [ @append_query_error = ] append_query_error ] [ , [ @query_no_truncate = ] query_no_truncate ] [ , [ @mailitem_id = ] mailitem_id ] [ OUTPUT ]
... our trigger should look like this. Please, this is only an example and should never work on the live server. It is only for me to understand the syntax of t-sql and triggers. in a normal sql 'select * from basisdata where nummer = 100710' works perfectly, but not on triggers... who can help me? whats wrong? why doesn't it work?ALTER TRIGGER [dbo].[basisdata]ON [dbo].[YKUN0015]FOR INSERT,UPDATEASSET NOCOUNT ON@query nvarchar(max)@subject nvarchar(255)@attach_query_result_as_file bitEXEC msdb..sp_send_dbmail @recipients = 'status@mydomain.com',@subject = 'yeahaaaaaaaaa',@query = 'Select * from basisdata where nummer = 100710',@attach_query_result_as_file = 1; I have some error messages that maybe help, but I do not find my mistake1: errors message of the database programm (erp-system)"Microsoft SQL Server:22050[Microsoft][ODBC SQL Server Driver][SQL Server]Error formatting query, probably invalid parameters"2: Meldung 102, Ebene 15, Status 1, Prozedur basisdata, Zeile 14Falsche Syntax in der Nähe von '@query'.(that means something like 'notice 102, level 15, status 1, procedure basisdata, line 14, wrong syntax near of '@query'I do not care about the sense of this trigger. the only thing that is important for me: I want this da** email with an attachment, inside the attached file I want to find the result ( in this example one row of data...) Who can help? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
pootle_flump
1064 Posts |
Posted - 2009-03-20 : 09:24:48
|
| I would recommend you don't do any of this in a trigger. Triggers act as part of transactions. Emails & writing files should not be part of transactions, they should be asynchronous. As things stand, if you lose contact with the mail server your transaction will hang.Triggers are very tempting to play around with but most of the time I would only use them when there is little alternative.My alternative suggestion would be have a job poll the table regularly and perform these actions on inserted and updated rows. That would, it sounds, get round your problem anyway. |
 |
|
|
pyromelana
Starting Member
23 Posts |
Posted - 2009-03-20 : 09:47:39
|
quote: Originally posted by pootle_flump I would recommend you don't do any of this in a trigger. Triggers act as part of transactions. Emails & writing files should not be part of transactions, they should be asynchronous....
that is my problem, isn't it? so, next time pls help or do not write something that is neither helpful nor a solutionthx |
 |
|
|
pyromelana
Starting Member
23 Posts |
|
|
pootle_flump
1064 Posts |
Posted - 2009-03-20 : 09:51:23
|
quote: Originally posted by pyromelana that is my problem, isn't it? so, next time pls help or do not write something that is neither helpful nor a solutionthx
Public forum. Perhaps you should employ a competent professional to correct your design flaws if you want to keep things out of the public domain? kthxbai |
 |
|
|
pyromelana
Starting Member
23 Posts |
Posted - 2009-03-20 : 10:04:03
|
quote: Originally posted by pootle_flump
quote: Originally posted by pyromelana that is my problem, isn't it? so, next time pls help or do not write something that is neither helpful nor a solutionthx
Public forum. Perhaps you should employ a competent professional to correct your design flaws if you want to keep things out of the public domain? kthxbai
you have to register for this forum, so it is not a 100% public forum. and to post 'do and don't' is not a goob behavior in a forum, recommendations are welcome but only together with a helping hand - that is what i call good social behavior. thanks for your recomm. for a poll, but i wrote something like 'I do not care about the sense of this trigger' (maybe you read my threat) and therefore only have to get this trigger work. What it makes (successfull work or crash the server) I do not care... and yes, we need a sql/programming nerd, are you interested? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-03-20 : 10:08:39
|
quote: Originally posted by pyromelana
quote: Originally posted by visakh16 for creating the file with query result use bcphttp://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/bcp-export-data-to-text-file.aspx
can I create a procdure in a trigger? can you tell me, why Microsoft added the possibility to attach_query_as_file like readable in the manual? what is bcp?
why do you need to create a procedure in a trigger? can i ask what your exact requirement is? |
 |
|
|
pootle_flump
1064 Posts |
Posted - 2009-03-20 : 10:17:19
|
Registering is open to the public. Can you take books out of the public library without registering for a library card?Thanks for your advice. As something of a n00b to these sort of forums it is useful to have someone that has contributed so much around to guide me.quote: Originally posted by pyromelana and to post 'do and don't' is not a goob behavior in a forum, recommendations are welcome but only together with a helping hand
Perhaps you missed the alternative suggested, the same alternative suggested to you countless times in your other threads.Good luck with the training! I'm sure everyone will find it valueless....I mean priceless. |
 |
|
|
pyromelana
Starting Member
23 Posts |
Posted - 2009-03-20 : 10:28:46
|
quote: Originally posted by visakh16why do you need to create a procedure in a trigger? can i ask what your exact requirement is?
Did you read what is behind the link you posted? (See in quotes...) My requirement for this trigger is, that if some special case happens on the database a responsible person gets an email with the trigger result as an attachment. and the attachment is necessary because. and sorry, i am not an IT specialist, not an sql chief and therefore do not know everything that is easy for others... quote: Here is a simple method of exporting all the data from SQL Server table to a Text FileCREATE Procedure BCP_Text_File( @table varchar(100), @FileName varchar(100) ) as If exists(Select * from information_Schema.tables where table_name=@table) Begin Declare @str varchar(1000) set @str='Exec Master..xp_Cmdshell ''bcp "Select * from '+db_name()+'..'+@table+'" queryout "'+@FileName+'" -c''' Exec(@str) endelse Select 'The table '+@table+' does not exist in the database' Execute this procedure by giving Table Name and the File NameEXEC BCP_Text_File 'Employee','C:\emp.txt'Now all the data from Employee table will be exported to the text file which will be located at C:\emp.txt
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-03-20 : 10:37:49
|
| what do you mean by special case? is it the normal DML operations? |
 |
|
|
pyromelana
Starting Member
23 Posts |
Posted - 2009-03-20 : 10:55:20
|
quote: Originally posted by visakh16 what do you mean by special case? is it the normal DML operations?
yes it is. BUT it only happens once or twice a month. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-03-20 : 10:59:08
|
| ok. so is it enough to put this information in a table like logtable using trigger with date. then you may call a job to send the records in table as a mail based on inserted date |
 |
|
|
pyromelana
Starting Member
23 Posts |
Posted - 2009-03-23 : 10:34:21
|
| Maybe this works. But the case happens not on a regular basis. maybe today and tomorrow and then never again for 30 days... and the job sends out the data every e.g. 2 days... so it would be better to combine all in one. The event happens, the trigger fires and puts the result into a e.g. txt-file and sends it out to the responsible person.... Fire and forget principle, what do you think? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-03-23 : 12:54:47
|
| i would still think of scheduling a job for this |
 |
|
|
pyromelana
Starting Member
23 Posts |
Posted - 2009-03-24 : 03:13:24
|
| ok, thanks for your help and opinion |
 |
|
|
|