November 29, 2010 at 4:04 pm
I have 2 queries, one is a table valued function, the other is a stored procedure. The stored procedure generates a weekly email (set up in the maintenance plan), the table valued function when run on its on produces a table filled with data as we wish to see it displayed. When I see the csv file generated with the email the columns are not all in a row, the final column goes down under the second column, and the first record ends up being a bunch of dash's.
Any help would be greatly appreciated!! 🙂 The table valued function & Stored Procedure are attached, as well as a sample of the output.
November 29, 2010 at 6:16 pm
I think the best thing to do would be to generate the csv file through either the OpenRowset function, or via BCP. Then email that file.
By running the query as part of the sp_send_dbmail procedure with @query_result_header set to it's default, it's going to include the column headings, with the dashes.
You might also want to investigate the FOR XML clause for generating comma-delimited strings. If you don't know how, just ask.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
November 30, 2010 at 12:18 pm
Using BCP:
-- declare and initialize variables
declare @sql varchar(8000),
@q varchar(255),
@fn varchar(255),
@path varchar(100);
SET @q = 'SELECT * FROM master.sys.objects';
SET @path = 'c:\temp';
SET @fn = @path + '\sysobjects.csv';
-- You may need to use the -S parameter to specify the server
-- especially if it's a named instance.
-- You may need to use the -U & -P parameters to specify the
-- user name / password to log in as if Trusted Connections
-- (-T) does't work.
SET @sql = 'bcp "' + @q + '" queryout ' + @fn + ' -c -t, -T';
-- ensure that the output directory already exists
execute xp_create_subdir @path;
exec master..xp_cmdshell @sql, no_output;
-- Change your sp_send_dbmail to this to send the file
/*
EXEC msdb.dbo.sp_send_dbmail
@recipients='angela.smith@blueridge.ca',
--@recipients=@CompanyRepEmail,
@subject=@s,
@body=@b,
--@query=@q,
--@attach_query_result_as_file=1,
@query_attachment_filename=@fn,
--@query_result_separator='',
@profile_name='projects'
*/
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
November 30, 2010 at 1:48 pm
Awesome, thank you. I will give it a try!
December 2, 2010 at 7:10 pm
GekGurly (11/30/2010)
Awesome, thank you. I will give it a try!
And how is it working?
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
December 6, 2010 at 8:49 am
I am having issues with getting the send mail to generate a file. And have had to make changes to the stored procedure, and am having some issues with data. So I guess its not going as well as I had hoped it would be going.
Viewing 6 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply