Failed to initialize sqlcmd library with error number -2147024809.

Massimo 21 Reputation points
2021-04-06T13:13:00.583+00:00

Hi,
I use the following script to send the result of a query as attachment to email. The script works correctly if I delete the argument @Query _no_truncate = 1, instead when I use it the script returns the error in subject. Can anyone help me?

EXEC msdb.dbo.sp_send_dbmail
@Anton _name = 'XXXXXXXX',
@recipients='xxxxx@xxxxxxxxxxxxx .it',
@subject= @oggetto,
@Tomas Podoba ='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.',
@Query ='SELECT * FROM TABLENAME_CSV',
@attach_query_result_as_file = 1,
@Query _attachment_filename = @filename,
@Query _result_separator = '',
@Query _result_header = 0,
@append_query_error = 1,
@Query _no_truncate = 1;

thanks
Massimo

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,546 questions
{count} votes

3 additional answers

Sort by: Most helpful
  1. MelissaMa-MSFT 24,176 Reputation points
    2021-04-07T02:37:38.09+00:00

    Hi @Massimo ,

    Welcome to Microsoft Q&A!

    In the Official Document sp_send_dbmail, it mentions as below:

    [ @Query _result_header = ] query_result_header Specifies whether the query results include column headers. The query_result_header value is of type bit. When the value is 1, query results contain column headers. When the value is 0, query results do not include column headers. This parameter defaults to 1. This parameter is only applicable if @Query is specified.

    85009-note.png

    So as also mentioned by other expert, please setting @Query _result_header to 0 and setting @Query _no_truncate to 1 and have aother try.

    If above settings are still working , you could try with below:

    • Rather than using '*', actually list the columns you want.
    • To fully qualified the DATABASE + SCHEMA_NAME + TABLE_NAME ('select * from sales.dbo.sales_month'), even if you're in the same database.
    • Check whether the file attachment was too large.

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Massimo 21 Reputation points
    2021-04-07T06:20:56.923+00:00

    Hi
    I tried to set @Query _result_header to 1 and now script works without errors.
    many thanks


  3. Scott Minter 0 Reputation points
    2023-08-18T19:13:57.9966667+00:00

    I had this same error and it was for a different reason; In my query I had a where clause that included a variable. I had to hard code the value in the where clause instead of using a variable.

    0 comments No comments