Text output from a stored proc - sql

I have created a SQL stored procedure that produces the expected results when I call it from SQL Management Studio, it has one output parameter which is a message that tells me where the proc failed, if it fails. Here is the top part of the proc
CREATE PROCEDURE [dbo].[uspProgramUpdate]
#EmailFailMessage VARCHAR(100) OUTPUT
At the bottom of my stored procedure is where I set the value of the variable:
BEGIN CATCH
ROLLBACK TRAN [CoS1]
RAISERROR ('***An error occurred during processing, all transactions have been rolled back. Correct the error and restart this process.***', 16, 1)
SET #EmailFailMessage = '***An error occurred during processing, all transactions have been rolled back. Correct the error and restart this process.***'
END CATCH
What I am trying to do (that I can't figure out) is how to capture the text of EmailFailMessage so I can use that text in an email within SSIS.
I have created an Execute SQL Task in SSIS which executes the stored procedure successfully on the SQL server. I have successfully captured the RAISERROR event and I can see that in the output window within Visual Studio window when I run the SSIS package:
Error: 0xC002F210 at Execute SQL - run uspProgramUpdate, Execute SQL Task: Executing the query "EXEC uspProgramUpdate ?" failed with the following error: "***An error occurred during processing, all transactions have been rolled back. Correct the error and restart this process.***". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
Task failed: Execute SQL - run uspProgramUpdate
The SQLStatement property of the SSIS task is:
EXEC uspCoSSleepProgramUpdate ? OUTPUT
I have the following SSIS variable:
User::EmailFailMessage (string), value = asdf
and the parameter is mapped on the SSIS SQL task in Parameter Mappings as follows:
Variable Name| Direction | Data Type | Parameter Name | Parameter Size
User::EmailFailMessage | Output | VARCHAR | #EmailFailMessage | -1
I have an email task that is being called and for that email task I have set:
MessageSource = #[User::EmailFailMessage]
When I run the SSIS package and receive the email, the message (body) of the email doesn't have the message from SQL after running the stored proc. It does have the initial value set for the User::EmailFailMessage variable and I am expecting that to change, but it isn't.
I have also tried, among other things, using 0 for the parameter name in the Parameter Mapping area of my Execute SQL Task. Using that doesn't change the result.

Personally I use the error message variable that is available in the on Error event handler called User::dataFlowExceptionMessage. That will send the exact error message sent by the failing task. That would be what bubbled up from raiserror. BUt I would suggest that you use the real error message as part of your raise error mesaage to make it easier to daignose the reason why the rollback occurred. I want to know the exact error generally.
Another thing you can do is put your error into a table variable and then insert it into an exception table with the date and the proc name after the rollback. Table variables stay in scope thorugh a rollback. Then you could look in that table to pull the message for your email task and you also have a record of just exactly what failed and when and how often which is useful for troubleshooting.

Related

Error when run EXEC procedure in T-SQL Statement Task

I created few procedures that I want to run using Maintenance Plan's Execute T-SQL Statement Task box however I'm getting error that job is not found.
Maintenance Task's configuration:
Returned error for running EXEC [dbo].[sp_backup_full] N'db_name', N'path/where//to/store/backup':
End Progress Error: 2021-06-29 18:59:00.42 Code: 0xC002F210 Source: full_backup_and_delete_old_one Execute SQL Task
Description: Executing the query "EXEC [dbo].[sp_backup_full] N'db_name..." failed with the following error: "Could not find stored procedure 'dbo.sp_backup_full'.".
Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
If you dont specify the databasename, by default, it is run in the master database. You can give three part name for the stored procedure call, to make sure that it is using the right database for execution.
EXEC [DatabaseName].[dbo].[sp_backup_full]

If stored procedure failed then Get output variables values into ssis variables in Execute SQL Task

I am trying to get output messages from SP into SSIS variable inside Execute SQL Task. I am able to get messages if stored proc execute successfully, but in case stored procedures raise errors, then variables remain empty as below-
Now in case sp execute completed successfully then ssis variables got data,
but in case of sp execute with errors then ssis variables remain empty and execute SQL task component fails (which is desired behaviour).
Please guide. Thankyou!
If execute SQL task fails, output variables are not populated.
Best option is to change your stored procedure and wrap all the code in the try/catch block. This way you will be able to always populate variables, just be sure to fail SSIS package if you receive internal errors.
If you use RAISERROR in the stored procedure, only information available to SSIS is the error message withing the RAISERROR statement. You can retrieve this information by creating OnError handler in SSIS and then it will be available in the system variable: "System::ErrorDescription"

SSIS 2017 "Execute SQL Task" fails Could not find stored procedure

I'am using SSIS with an ADO.NET connection to Azure. Within SSIS I want to execute a procedure with one input and 2 output parameters. The input parameter is a static value. The Procedure works with T-SQL within SSMS.
I setup the "Execute SQL Task" as followed
General
SQLStatement: METRICE_VAULT.GP_1001_GENERIC_PRE_PROCESS 2, #INSTANCE, #PROCESS_STATUS
IsQueryStoredProcedure: True
ConnectionType: ADO.NET
ResultSet: None
SQLSourceType: Direct input
Parameter Mapping
When I execute the "Execute SQL Task" I received the following error
Must declare the scalar variable "#". Possible failure reason: Problem with the query, "ResultSet" property not set correctly..
Update
After adding parameters name i am receiving the following error:
Execute SQL Task] Error: Executing the query "METRICE_VAULT.GP_1001_GENERIC_PRE_PROCESS 2, #INS..." failed with the following error: "Could not find stored procedure 'METRICE_VAULT.GP_1001_GENERIC_PRE_PROCESS 2, #INSTANCE ,#PROCESS_STATUS'.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
Below is the way to configure your parameters and properties. Set IsQueryStoredProcedure property to True and instead of writing exec ProcName you just have to give ProcName in sqlstatment if you are using ADO.NET Connection.
Change the direction of your parameters to INPUT.
Since you are using ADO.NET You must specify the parameter name. As example:
METRICE_VAULT.GP_1001_GENERIC_PRE_PROCESS 2,#param1, #param2
And don't use parameter index in parameter mapping tab as shown in the screenshot.
Additional Information
You can refer to the following official documentation for more details and examples:
Map Query Parameters to Variables in an Execute SQL Task
Update 1
Try using a fully qualified name:
<database name>.<schema name>.<stored procedure name>
Or add a USE <database> command before the stored procedure execution.

Execute SQL Server stored procedure through SSIS

I have this stored procedure:
CREATE PROCEDURE [dbo].[sp_Carrier_Scan_Compliance]
(#RETAILERID INT OUTPUT,
#SYSTEM_ID VARCHAR(10) OUTPUT)
AS
BEGIN
SET #RETAILERID = 2
SET #SYSTEM_ID = 'DMASOS'
...
END
I have created a SSIS package using a Execute SQL Task in the control flow.
These are my Execute SQL Task editor settings:
This are my Variable settings:
These are my Parameter Mapping settings:
When I run the SSIS package, I get an error:
Error: 0xC002F210 at Execute SQL Stored Procedure (to copy data from 'BI-Datatrunk' source table) Task, Execute SQL Task: Executing the query "exec = [sp_Carrier_Scan_Compliance] ? OUTPUT, ? O..." failed with the following error: "Incorrect syntax near '='.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
Task failed: Execute SQL Stored Procedure (to copy data from 'BI-Datatrunk' source table) Task
Warning: 0x80019002 at Carrier_Scan_Compliance_SP: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED. The Execution method succeeded, but the number of errors raised (1) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
I am not sure what I am missing.
Please help me.
Thanks
The key part of the last error is
The EXECUTE permission was denied on the object
'sp_Carrier_Scan_Compliance', database 'DATAK', schema 'dbo'."
You need to assign EXECUTE permissions to the SQL user executing the Proc
USE DATAK
GO
GRANT EXECUTE ON sp_Carrier_Scan_Compliance TO <sql user>
GO

SSIS Generic Error for SP Execution

In a for each loop container , for each job reference.
I am running an execute sql task to get a list of 21 INPUT parameters from my stored procedure ( Task 1 ). The INPUT parameters
are then stored in SSIS variables.
Then I have an execute sql task to run the stored procedure, where I set the 21 Input Paramters and an Output parameter. ( Task 2 )
When running the package it fails at Task 2 with the following generic error:
Error: 0xC002F210 at Run sp_insert_Package, Execute SQL Task:
Executing the query "DECLARE #TestFaultStageID int DECLARE #TestID..."
failed with the following error: "Multiple-step OLE DB operation
generated errors. Check each OLE DB status value, if available. No
work was done.". Possible failure reasons: Problems with the query,
"ResultSet" property not set correctly, parameters not set correctly,
or connection not established correctly. Task failed: Run
sp_insert_Fault2 Error: 0xC002F210 at Run sp_insert_Package, Execute
SQL Task: Executing the query "DECLARE #TestFaultStageID int DECLARE
#StoreID..." failed with the following error: "Multiple-step OLE DB
operation generated errors. Check each OLE DB status value, if
available. No work was done.". Possible failure reasons: Problems with
the query, "ResultSet" property not set correctly, parameters not set
correctly, or connection not established correctly. Task failed: Run
sp_insert_Fault2 Warning: 0x80019002 at 02-01-02-Epoch-Jobs-Load: SSIS
Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED. The Execution method
succeeded, but the number of errors raised (2) reached the maximum
allowed (1); resulting in failure. This occurs when the number of
errors reaches the number specified in MaximumErrorCount. Change the
MaximumErrorCount or fix the errors.
I have tried to find the error, by checking all the data types and conversions and other stuff but no luck so far. Is there an easy way to track down or narrow down cause ?
Sql Server 2012
In Execute SQL Task component, in SQLStatement field, you should have question marks (?) set in place where you want to put parameters. They should also be defined in Parameter mapping tab.
Are you sure those are set correctly? Could you share your SQLStatement field value?
Sorted ? missing for the OUTPUT parameter was the problem. Sorry All ! Arghhhh