Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Stored Procedure Logging Expand / Collapse
Author
Message
Posted Thursday, January 2, 2014 9:57 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, July 25, 2014 4:22 AM
Points: 249, Visits: 313
My current aim is to write the results from a T-SQL stored procedure into a text file for future reference / error capturing. The SP has been coded so that at set points messages are written to a table which is returned at the end of the script, e.g.

  DECLARE @Log TABLE
( ID INT IDENTITY(1,1)
, ErrorDescription VARCHAR(MAX)
)

INSERT @Log (ErrorDescription)
SELECT 'Server: ' + @@SERVERNAME + ' Database: ' + DB_NAME()

(Perform data manipulation tasks & write further messages to @Log)

SELECT COALESCE(ErrorDescription,'') AS ErrorDescription
FROM @Log
ORDER BY ID ASC


This was quite happily working for me using a Data Flow (with a Flat File Destination) until we included a temporary table into the stored procedure. This so included so that the main SP controlling the data could be broken down into different work areas whilst sharing a key table list (the temp table)

Seeing as SSIS does not like temp tables in a SP, due to the result definition being unknown, I've hit a problem.

I've tried the following suggestions to work round this, without luck:
http://blogs.conchango.com/jamiethomson/archive/2006/11/19/SSIS_3A00_-Using-temporary-tables.aspx
http://stackoverflow.com/questions/18346484/ssis-package-not-wanting-to-fetch-metadata-of-temporary-table (SET FMTONLY ON)

I've not been able to work around that issue (and really don't like the option of using an actual table instead a temp table), so I moved onto a different method.

Instead I've been trying to use a SQL Task Editor with the @Log result table being written to a Variable, which can then be written. With this method I'm now getting an error saying:

The type of the value (DBNull) being assigned to variable "User::MetadataLogging" differs from the current variable type (String). Variables may not change type during execution. Variable types are strict, except for variables of type Object.


This is occuring even though the SP is returning a string (as far as I can tell - SP has XML parameters of >20MB when used in full). I have converted the @Log table into a single string using the following:

SELECT COALESCE((
SELECT COALESCE(ErrorDescription,'') AS [text()]
FROM @Log
ORDER BY ID ASC
FOR XML PATH('')
),'') AS Logging;

Ultimately does anyone have any suggestions on how to work round either of these issues or an alternative method on how to get a T-SQL Stored Procedure with a #table to log to a flat file? I've been struggling with this all day, so any input would be welcome.
Post #1527204
Posted Thursday, January 2, 2014 10:21 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 8:24 AM
Points: 12,887, Visits: 31,835
can you add CLR procedures?

writing to a file is very, very easy if you add some CLR's;
i've got a simple example out on codeplex that i wrote as a proof of concept:

http://sqlclrexport.codeplex.com/

an example call would be like this; a CLR has access ot any table, temp table or table variable, if it is in scope and the calling user has permissions, of course.

EXECUTE CLR_ExportTableAppendToCSV @TableName = '@Log',
@FilePath = 'C:\Data\Logging',
@FileName = 'Procedure_logging.csv',
@IncludeHeaders = 1



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1527215
Posted Thursday, January 2, 2014 3:35 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, July 25, 2014 4:22 AM
Points: 249, Visits: 313
Lowell (1/2/2014)
can you add CLR procedures?


I've not thought of that, but I'll have a look at that later. I'm not 100% sure that I'll be able to create CLR procedures but I'll investigate it.
Post #1527309
Posted Thursday, January 2, 2014 11:23 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 8:25 AM
Points: 4,983, Visits: 11,675
Which version of SSIS are you using?


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1527351
Posted Friday, January 3, 2014 2:02 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, July 25, 2014 4:22 AM
Points: 249, Visits: 313
SQL Server 2012, with the SSIS Package being created in Visual Studio 2010
Post #1527387
Posted Friday, January 3, 2014 4:15 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 8:25 AM
Points: 4,983, Visits: 11,675
Ok. The workaround you found does not apply for 2012. Instead, please search for 'with result sets'

Sorry but I'm not at my PC so cannot post something more detailed right now.



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1527445
Posted Friday, January 3, 2014 2:11 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, July 16, 2014 3:21 PM
Points: 1,210, Visits: 2,512
Phil Parkin (1/3/2014)
Ok. The workaround you found does not apply for 2012. Instead, please search for 'with result sets'

Sorry but I'm not at my PC so cannot post something more detailed right now.


I think Phil's got it here. In the SQL Command window of your OLE DB Source (I assume you're using an OLE DB source since you're reading the data out of the table), after the EXECUTE statement, add a WITH RESULT SETS clause, like this:

EXECUTE dbo.myStoredProc WITH RESULT SETS ((col1 int, col2 varchar(20), col3 datetime))

Inside the double parentheses of the WITH RESULT SETS clause, list the columns in the results of your stored proc and their data types, just as if you were creating a table. Now, SSIS gets the metadata for the result set from this definition rather than trying to tease it out of the stored proc.


Jason Wolfkill
Blog: SQLSouth
Twitter: @SQLSouth
Post #1527724
Posted Saturday, January 4, 2014 12:37 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 9:51 AM
Points: 36,777, Visits: 31,233
At this point, I have to ask the question...

WHY are you writing this information out to a file? Why not just write it to a table where you can create reporting queries, etc, etc, from the error data?


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1527854
Posted Saturday, January 4, 2014 1:46 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 7:12 PM
Points: 2,148, Visits: 487
Although I am not totally clear on the end goal here, would an execute SQL task embedded in the OnError event handler of either the data flow task (local to the DTF) or at the package level (global to the package and having any package errors bubble up to that event handler) do the trick for you?

This would be very similar to what Microsoft published in Project REAL.

Post #1527862
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse