Hi, sending a mail using sp_send_dbmail (as below) with attachment works. However the characters in the attacheted file gets truncated to 256. The file is populated with the results from the @query string.
The value of MaxFileSize is 1 Mb when executing sysmail_help_configure_sp. It seems as if the @query_no_truncate value is being ignored by sp_send_dbmail procedure.
I'm trying to get sp_send_dbmail to attach the @query result set as an xml file.
The email is being sent and the attachment is present, but rather than ascii xml, it contains a chunk of nonsense like this 0x440352004F005700440470006B00650079004409660069007200730074006E0061006D00650044086C006100730074006E0061006D0065004410700068
I've tested the query with the first bit of code and sent the email with the second bit. I think I can jimmyjack it to write the xml to a temp file on the server and send that file using the @file_attachments parameter, but I'm hoping someone out there knows how to get this one to work.
Thanks in advance for any help you can give me.
Here's my code:
-- test that xml output is being generated DECLARE @OUTPUT xml SET @OUTPUT = (SELECT * FROM database..table FOR XML PATH('ROW'), root('ROOT')) SELECT @OUTPUT
-- run the email proc EXEC msdb.dbo.sp_send_dbmail @QUERY= 'SET NOCOUNT ON SELECT * FROM database..table FOR XML PATH(''ROW''), root(''ROOT'')' ,@RECIPIENTS= 'userid@companyname.COM' ,@subject= 'Subject' ,@attach_query_result_as_file= 1 ,@query_attachment_filename= 'ATTACHMENT.xml' ,@query_result_width= 32767 ,@query_no_truncate= 1 ,@append_query_error= 1 ,@query_result_header= 1
Hello, In the query analyser, I execute a select ' restore database ' + name + ' from disk = " D:sql7dumpackup ' + name + '' + Name + ' _du_21.bak " with move ' ' + name + ' _Données " to " e:sqlbasesasesdata ' + name ' _données.mdf ", move ' ' + name + ' _Journal " to " d:sqllogs ' + name + ' _journal.ldf ", replace From sysdatabases
And some lines are truncated in results ?. how to avoid that.
Hi everyone, i'd like to assign "@recipients" for sp_send_dbmail derived from a query as follows:
Code SnippetDECLARE @mailist VARCHAR(2000) SET @mailist= 'select email from server.db.dbo.table where lastname=' +''''+'aaa'+'''' exec msdb.dbo.sp_send_dbmail @profile_name='myprofile', @recipients=@mailist, @subject='email address through a query', @...
when i run it i dont get any error message but mails to dot arrive. any suggestion why ? Thanks a lot
I am writing a stored procedure which finally has to send mail to the customer with a list of phones that have been added to his account. I am getting these phones from a SQL Table in the form of a query, and including the output of the query(list of phones) , in the body of the mail.
I'm having a little trouble with a large query I have written to be sent as an email.
I want to send it as a csv so I've added commas in between each value in the query.
This is causing an error though, I think the ' that surround the commas are closing the query early. Is there any way around this? like encapsulating the query?
Hi, I have hit a brick wall with this. My code is as below
public void fillCustomer() { string connectionString = "server='local'; trusted_connection= true; integrated security=sspi; database='Mrbob'"; System.Data.SqlClient.SqlConnection dbConnection = new System.Data.SqlClient.SqlConnection(connectionString); string queryString = "SELECT * FROM [Customer] WHERE ([CustomerID] = @CustomerID)"; System.Data.SqlClient.SqlCommand dbCommand= new System.Data.SqlClient.SqlCommand(); dbCommand.CommandText = queryString; dbCommand.Connection = dbConnection; System.Data.IDataParameter param_CustomerID = new System.Data.SqlClient.SqlParameter(); param_CustomerID.ParameterName ="@CustomerID"; param_CustomerID.Value = customerID;dbCommand.Parameters.Add("@CustomerID", SqlDbType.Int); dbCommand.Connection.Open(); System.Data.IDataReader dataReader = dbCommand.ExecuteReader(); dbCommand.Connection.Close(); while(dataReader.Read()) { customerID = dataReader.GetInt32(0); date = dataReader.GetDateTime(1); eposCode = dataReader.GetInt32(2); } dataReader.Close();
}
The error I am getting is
Prepared statement '(@CustomerID int)SELECT * FROM [Customer] WHERE ([CustomerID] = ' expects parameter @CustomerID, which was not supplied.
As you can see from my queryString the @CustomerID parameter is passed in. It seems as if the string is being truncated at 64 characters long. If I remove the paramter to pass the relevant infomration and pass in a customerID I know exists it works.
I am really stumped on this and would really appreciate any pointers
Hi! When I run a select statement, it would retrieve a product description. In some rows, it is long. Consequently, the product description was truncated. Did anybody have resulotion for this issue?
hi, like, if i need to do delete some items with the id = 10000 then also need to update on the remaining items on the with the same idthen i will need to go through all the records to fetch the items with the same id right? so, is there something that i can use to hold those records so that i can do the delete and update just on those records and don't need to query twice? or is there a way to do that in one go ?thanks in advance!
Hello. I currently have a website that has a table on one webpage. When a record is clicked, the primary key of that record is transfered in the query string to another page and fed into an sql statement. In this case its selecting a project on the first page, and displaying all the scripts for that project on another page. I also have an additional dropdownlist on the second page that i use to filter the scripts by an attribute called 'testdomain'. At present this works to an extent. When i click a project, i am navigated to the scripts page which is empty except for the dropdownlist. i then select a 'testdomain' from the dropdownlist and the page populates with scripts (formview) for the particular test domain. what i would like is for all the scripts to be displayed using the formview in the first instance when the user arrives at the second page. from there, they can then filter the scripts using the dropdownlist. My current SQL statement is as follows. SelectCommand="SELECT * FROM [TestScript] WHERE (([ProjectID] = @ProjectID) AND ([TestDomain] = @TestDomain))" So what is happening is when testdomain = a null value, it does not select any scripts. Is there a way i can achieve the behaivour of the page as i outlined above? Any help would be appreciated. Thanks, James.
Hi, When I insert a varchar greater than size 256 through Query Analyzer and retrieves it back, it shows only the first 256 part of it. Is ther any server seetings I have to alter to incorporate bigger varchar values ? does anybody have any ideas? Thanx, Nisha
ok can someone tell me why i get two different answers for the same query. (looking for last day of month for a given date)
SELECT DATEADD(ms, - 3, DATEADD(mm, DATEDIFF(m, 0, CAST('12/20/2006' AS datetime)) + 1, 0)) AS Expr1 FROM testsupplierSCNCR I am getting the result of 01/01/2007
-- The 3rd query uses an incorrect column name in a sub-query and succeeds but rows are incorrectly qualified. This is very DANGEROUS!!! -- The issue exists is in 2008 R2, 2012 and 2014 and is "By Design"
set nocount on go if object_id('tempdb.dbo.#t1') IS NOT NULL drop table #t1 if object_id('tempdb.dbo
[code]....
This succeeds when the invalid column name is a valid column name in the outer query. So in this situation the sub-query would fail when run by itself but succeed with an incorrectly applied filter when run as a sub-query. The danger here is that if a SQL Server user runs DML in a production database with such a sub-query which then the results are likely not the expected results with potentially unintended actions applied against the data. how many SQL Server users have had incorrectly applied DML or incorrect query results and don't even know it....?
For each customer, I want to add all of their telephone numbers to a different column. That is, multiple columns (depending on the number of telephone numbers) for each customer/row. How can I achieve that?
I want my output to be
CUSTOMER ID, FIRST NAME, LAST NAME, TEL1, TEL2, TEL3, ... etc
Each 'Tel' will relate to a one or more records in the PHONES table that is linked back to the customer.
When I run the following query from Query Analyzer in SQL Serer 2005, I get a message back that says. Command(s) completed successfully. What I really need it to do is to display the results of the query. Does anyone know how to do this? declare @SniierId as uniqueidentifierset @SniierId = '85555560-AD5D-430C-9B97-FB0AC3C7DA1F'declare @SniierAlias as nvarchar(50)declare @AlwaysShowEditButton as bitdeclare @SniierName as nvarchar (128)/* Check access for Sniier */SELECT TOP 1 @SniierName = Sniiers.SniierName, @SniierAlias = Sniiers.SniierAlias, @AlwaysShowEditButton = Sniiers.AlwaysShowEditButtonFROM SniiersWHERE Sniiers.SniierId=@SniierId
When using "sp_send_dbmail", messages are queued through Service Broker. Other than the system views, "dbo.sysmail_...", in MSDB, is there another way to know if an email was sent successfully?
Also, in 2000 you did not need to supply the whole email address in order for xp_sendmail to work. For example, I could use my name, "RGioia" as the recipient and it would send email to rgioia@<my current domain>. Does anyone know of a work-around for this in 2005 or do you just have to ensure that full email addresses are used?
CREATE TRIGGER TR_ABC ON TABLE_A AFTER INSERT AS DECLARE @E_MAIL VARCHAR(255), @MESSAGE VARCHAR(255) SET @MESSAGE = 'A new call request has been logged'
SELECT @E_MAIL = E_MAIL FROM TABLE_A AS A LEFT OUTER JOIN TABLE_B AS B ON B.SYSADMIN = A.SYSADMIN WHERE SYS_ADMIN = 1
The problem is everytime I insert data into the table I get an error stating that-: 'At least one of the following parameters must be specified. "@recipients, @copy_recipients, @blind_copy_recipients". Mail queued.'
SQL Server 2005Help. I have a problem:On a 2000 box, I am calling the sp_send_dbmail stored procedure on a2005 box (they are linked servers).This is how I'm calling the proc (from the 2000 box), which usessp_send_dbmail to send the mail.EXEC My2005Server.DatabaseName.dbo.SendNotification@recipients = 'me@yahoo.com',@subject = 'SENDING @TOTAL,@body = @text1 + @text2 + @text3 + @text4 + @text5@text1 - @text5 are varchar(8000).As you can see I'm trying to take advantage of the varchar(max) that@body allows.However, I get this error:Server: Msg 170, Level 15, State 1, Line 22Line 22: Incorrect syntax near '+'.I need to be able to concatenate these somehow... as I cannot put theminto one large variable from the 2000 server side.Can you see the problem here?Help.Thanks
Msg 15404, Level 16, State 19, Server TAKKARA, Line 1 Could not obtain information about Windows NT group/user 'WECLICKisantos', error code 0x5.
If I remove the @query, @exclude_query_output and @append_query_error options, I can send the email without a problem, just whenever I try to add a query to it that it gives me this error message.
I'm a sysadmin, I can run that query, I have access to the DatabaseMailUserRole under msdb and the profile that I'm using is public... I'm kind of stuck, let me know if you can help.
I have a query that performs a comparison between 2 different databases and returns the results of the comparison. It returns 2 columns. The 1st column is the value of the object being compared, and the 2nd column is a number representing any discrepancies.What I would like to do is use the results from this 1st query in the where clause of another separate query so that this 2nd query will only run for any primary values from the 1st query where a secondary value in the 1st query is not equal to zero.I was thinking of using an "IN" function in the 2nd query to pull data from the 1st column in the 1st query where the 2nd column in the 1st query != 0, but I'm having trouble ironing out the correct syntax, and conceptualizing this optimally.
While I would prefer to only return values from the 1st query where the comparison value != 0 in order to have a concise list to work with, I am having difficulty in that the comparison value is a mathematical calculation of 2 different tables in 2 different databases, and so far I've been forced to include it in the select criteria because the where clause does not accept it.Also, I am not a DBA by trade. I am a system administrator writing SQL code for reporting data from an application I support.
declare @dbccdate varchar(60) set @dbccDate='dbcc east' + convert(varchar(60),getdate(),10) exec msdb.dbo.sp_send_dbmail @recipeints='human@gmail.com', @body = 'This is a test' @subject= @dbccdate , @query='dbcc checkdb (msdb)', @attach_query_result_as_file=1, @query_attachment_filename='Dbcc_MSDB_Results.txt'
This works fine but, I'd really like the subject to contain the line "Checkdb found 0 errors..." So mgmt doesn't have to open the txt file to view the results.
I've tried dumping the results to a txt file first. Then trying to read the text file for the line inquestion. I wrote a for loop to parse the results command line, but can't figure how to add that value to the subject.
The script is this:
for /f %i in ('findstr /B /I "CHECKDB" y:dbcclogmsdblog.txt') do echo %i
Have a funny one that I've not yet resolved, and was wondering if I am the only one.
SQL2005 SP1 server, Ent Ed, 64 bit, clustered machine. SP2a installed over Easter weekend.
about 12 days after apparently succesful upgrade, the cluster was failed over. After that fail-over, an existing job started failing, with the following message: quote:Error executing extended stored procedure: Invalid Parameter [SQLSTATE 42000] (Error 22050). The step failed.
All the job does is build a SQL query, then supply the query to sp_send_dbmail for execution:
One 'funny' I noticed is that up until the failover, SSMS seemed to erport the version of the server as still the SP1 (2157). After failover, it now lists (3042). This may simply be a symptom of SSMS not auto-refreshing though. We have (I believe) failed back again since then, still no joy.
Any Ideas?
*##* *##* *##* *##* Chaos, Disorder and Panic ... my work is done here!
This thread points to a KB article http://support.microsoft.com/kb/910416 which is close except for the fact that these were not upgrades, but clean installs. I'm leary of applying the hotfix to my servers.
Is this a known issue internal to MS and is the referenced hotfix appropriate to install?
I have a stored procedure that sends an mail using sp_send_dbmail. My problem is, if I execute the stored procedure via my ASP.NET 2005 application, the email will not send. I know the stored procedure is being called and works because: 1. The stored procedure does 2 things, sets a status, then sends the email. The status is being changed, but the email is not being sent. 2. When I investigate with Sql Server Profiler, I see the stored proc being called and the values of the parameters are set as they should be. What I don't understand is, I can copy the call in Sql Server Profler, and paste it in a Query Analyzer window and execute that same call, and everything works fine, status is changed, and the email is sent (to myself for testing and I receive it just fine). Is there a reason why the email won't send via code? Am I suppose to setup some security that will allow my application to send emails? Any info would be GREATLY appreciated.
We have a DTS package in the old SS2000 that we are still using in SS2005. We change the old xp_sendmail to use the new sp_send_dbmail. This runs fine without attachment. But with attachment, I'm encountering an error when running the package.
The task reported failure on execution. The client connection security context could not be impersonated. Attaching file requires an integrated client login. Syntax error or access violation.
I cannot find much topic on the net about this error.
Hi everyone, I'm trying to usie "sp_send_dbmail" and i'm required to provide value to: "@profile_name" argument. The trouble is i have no idea what it means.. How do i get to know what is my "profile_name" ? Thanks.
I want to write a trigger that sends me an email, when new records are added into a table.
For testing I have created one table called location1, which has simple data from AdventureWorks.Production.Location. And another table is called new_location, which stores the newly added records.
Here is the trigger:******************************** use [AdventureWorks] if object_id ('location1') is not null drop table location1 go create table location1(LocationID int, LName varchar(50))
if object_id ('new_location') is not null drop table new_location go create table new_location (LocationID int, LName varchar(50))
if object_id ('change_ID', 'TR') is not null drop trigger change_ID go create trigger change_ID on location1 for insert as delete new_location insert into new_location select * from inserted
exec sp_send_dbmail @profile_name = 'Profile', @recipients = 'email', @subject = 'New Record(s) added.', @query = 'select * from new_location order by LocationID'; go ********************************
After I insert statement, Management Studio runs the executing throughout. insert into location1 select LocationID, Name from Production.Location order by LocationID
I'm using SQL Server 2005. I'm trying to send an email with query results attached. I've enabled database mail (surface config, config wizard, profile set, accounts loaded). I know database mail itself works as I'm able to send plain text messages as well as messages with HTML embedded. However, when I try to format a query to send attachments with mail, as shown in BOL; I always get this error message - <snip>
Msg 14661, Level 16, State 1, Procedure sp_send_dbmail, Line 476
Query execution failed: Error initializing COM
Msg 0, Level 11, State 0, Line 0
A severe error occurred on the current command. The results, if any, should be discarded. <snip>
Here is a sample of the query...
DECLARE @WHO VARCHAR(255)
SET @WHO = '<name would go here>'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'sqlmail'
,@recipients = 'my.name@wouldgohere.com'
,@subject = 'testing attachment'
,@query = N'SELECT email_body
FROM WFS_non_stock_email_sendit_history
WHERE who_entered = @WHO
ORDER BY date_created DESC
OPTION(MAXDOP 1)'
,@execute_query_database = 'whaley'
,@attach_query_result_as_file = 1
,@query_attachment_filename = 'TESTATTACH.txt'
,@query_result_separator = ';'
If I were to run this query outside of this dbmail code, the query works fine.
If I can believe the go to line editor, line 476 in the sp_send_dbmail sproc is...
DELETE sysmail_attachments_transfer WHERE uid = @temp_table_uid
My problem here is this is a system sproc that MSFT built. I don't understand why I would get this error message instead of something more definitive and indicative of the problem.
Has anyone else seen this problem? I really need to be able to send attachments.