Thursday, August 29, 2013

Email the results of SQL Query, using SQL DB Mail. The contents is formatted as a table, using XML

Email the results of SQL Query, using SQL DB Mail. The contents is formatted as a table, using XML

It is a compilation of information found online, with some modifications made by me.

DECLARE @xml NVARCHAR(MAX)
DECLARE @body NVARCHAR(MAX)

SET @xml = CAST(( SELECT
                     Col1 AS 'td','',
                     Col2 AS 'td','',
                     Col3 AS 'td','',
                     Col4 AS 'td','',
                     Col5 AS 'td','',
                     Col6 AS 'td','',
                     Col7 AS 'td','',
                     Col8 AS 'td','',
                     Col9 AS 'td','',
                     Col10 AS 'td','',
                     Col11 AS 'td','',
                     Col12 AS 'td','',
                     Col13 AS 'td','',
                     Col14 AS 'td','',
                     Col15 AS 'td','',
                     Col16 AS 'td'
From DBName.dbo.Table1
Where New_Status = (Select Col1 From DBName.dbo.Table2 Where DBName.dbo.Table2.PONbr = DBName.dbo.Table1 .PONbr)
ORDER BY [PONbr] Desc
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))

SET @body ='<html><body><H3>Table Name</H3>
<table border = 1>
<tr>
       <th> [Col1] </th>
       <th> [Col2] </th>
       <th> [Col3] </th>
       <th> [Col4] </th>
       <th> [Col5] </th>
       <th> [Col6]] </th>
       <th> [Col7] </th>
       <th> [Col8] </th>
       <th> [Col9] </th>
       <th> [Col10]] </th>
       <th> [Col11] </th>
       <th> [Col12] </th>
       <th> [Col13] </th>
       <th> [Col14] </th>
       <th> [Col15] </th>
       <th> [Col16] </th>
</tr>' 

SET @body = @body + @xml +'</table></body></html>'

EXEC msdb.dbo.sp_send_dbmail
@profile_name='Profile_Name',
@body = @body,
@body_format ='HTML',
@recipients='Recipient@Domain.com',

@subject='Email Subject';

Tuesday, August 27, 2013

SQL Server - SSIS Error: "Syntax error, permission violation, or other nonspecific error". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.

I got this error in an Execute SQL Task.

"Syntax error, permission violation, or other nonspecific error". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.

This time the issue was that I had commented out one line in the Update statement.

This was the original statement (I've simplified it in order to make this post simpler):


update
  Table1 
set
    Table1.qtyavail=Table2.qtyavail,
    --Table1.price=Table2.price,
    Table1.Weight=Table2.Weight
from
   Table1 
join Table2
       On Table1.ID = Table2.ID

I had to delete the commented out line to be able to resolve the issue:

update
  Table1 
set
    Table1.qtyavail=Table2.qtyavail,
    Table1.Weight=Table2.Weight
from
   Table1 
join Table2

       On Table1.ID = Table2.ID

Thursday, August 15, 2013

Stefan Nachev's Blog: SQL Server - SSIS

I am getting an intermittent error in one of my SSIS Packages. One of the SQL Tasks was getting deadlocked. This was the error - 

Executed as user: DOMAIN\administrator. Microsoft (R) SQL Server Execute Package Utility  Version 10.50.2500.0 for 32-bit  Copyright (C) Microsoft Corporation 2010. All rights reserved.    Started:  2:15:33 AM  Error: 2013-08-15 02:15:39.92   
Code: 0xC002F210     Source: Update SL_InvtID From UPC Execute SQL Task    
Description: Executing the query "X888_VendorInv_Update_SLInvtID_FromUPC_Pre ?" failed with the following error:
"Transaction (Process ID 70) was deadlocked on lock |
communication buffer resources with another process and has been chosen as the deadlock victim. Rerun the transaction.".
Possible failure reasons: Problems with the query,
"ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
End Error  DTExec: The package execution returned DTSER_FAILURE (1).  Started:  2:15:33 AM  Finished: 2:15:39 AM  Elapsed:  6.209 seconds.  The package execution failed.  The step failed.



The issue seems to be that within the Update SL ID (Pre) sequence container, there were two sequence containers.They were running in parallel (not sequentially).