How to count rows in SSIS based on specific conditions? - sql

I have a Stored Procedure in SQL Server 2008 like below.
ALTER PROCEDURE myStoredProcedure
#Id int,
#hin varchar(30),
#checkValue varchar(30),
#CounterDeceasedPatients int=0 OUTPUT
insert into myTable
values (#Id, #hin, GETDATE())
if (#checkValue is not null)
BEGIN
set #CounterDeceasedPatients = #CounterDeceasedPatients + 1;
update myTable
set hin= #checkValue
where Id = #Id
RETURN;
END
I am calling this SP via SSIS, by using an OLE DB Command in Data Flow, which enables each rows in my file go to the SP - with the sql command: EXEC [dbo].[myStoredProcedure] ?,?,?. (The order of data (?) in my file is: Id, hin, checkValue)
What I want to do is to count how many different records (different rows) entered the if condition in my SP. SO I believe need to place a "row counter" somewhere, filtering its usage where #checkValue is not null. But I couldnt find it how. I am a newbie in SSIS, so I appreciate if someone helps me to figure this out. Thanks.
EDIT: I am trying to select only #checkValue as an input parameter for my ROW COUNT, but it is giving error:
EDIT2: I updated my SP. I added "CounterDeceasedPatients" variable as Int32 in SSIS and assigned it to 0. My sql execute command is: EXEC [dbo].[myStoredProcedure] ?,?,?,?,CounterDeceasedPatients
This is giving me the error:
Source: "Microsoft OLE DB Provider for SQL Server" Hresult:
0x80040E07 Description: "Error converting data type nvarchar to
int.".
When I use EXEC [dbo].[myStoredProcedure] ?,?,?,?,CounterDeceasedPatients output as SQL command, then I receive the error:
Description: "Cannot use the OUTPUT option when passing a constant to
a stored procedure.
I need help.

Use a script transformation and a DataFlow-level package variable.
Create the int-type variable with a default of 0, and in the script transformation, increment the variable if checkvalue is not null for the incoming row, and then use the value of the variable to set the value of your counter column.
Note that I am suggesting this INSTEAD of trying to update the counter with an OUTPUT variable in your stored procedure, and not as a way of trying to get that idea to work.

Related

SSIS Looping with Return Value from Stored Procedure

I am trying to create a SSIS Package that loops based on the return value of a stored procedure run in the loop. I keep getting a super NOT helpful error of:
"Error: 0xC002F210 at Load Order, Execute SQL Task: Executing the query "EXEC ? = [Load_Focus_OrderNum] ?, 1" failed with the following error:
"Value does not fall within the expected range.".
Possible failure reasons:
Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
Task failed: Load Order"
Here is my setup:
The Load Order stored procedure loads a table with 500 orders at a time, then the last order number is returned (I have confirmed it returns correctly).
DECLARE #spOut int
EXEC #spOut = Load_Focus_OrderNum 1, 1
PRINT #spOut
Returns 638 as expected
I then want it to process the next 500 starting at the next order.
I'm calling my stored procedure with:
EXEC ? = sp_LoadOrders ?, 1
Procedure snippet:
ALTER PROCEDURE [dbo].[LoadOrders]
(#PK_ID INT, #OrdType INT)
AS
-- Loads OrderNumTbl table
RETURN (SELECT TOP 1 ID FROM OrderNumTbl ORDER BY ID DESC)
GO
My parameter mapping for it is:
And my expressions for the loop are:
What am I missing? Any help is appreciated!
In the parameter Mapping section, replace the parameter name value with the parameter index >> replace #OrderID with 0 and #T1_ID with 1
References
SSIS: Value does not fall within the expected range
SQL Server Central - SSIS: Value does not Fall Within the Expected Range
Parameters and Return Codes in the Execute SQL Task

Getting result from SQL Server Stored Procedure

I need to get the result from a stored procedure in a MSSQL Server database. I tried different ways:
Using a Table Input Step, with this query: exec dbo.storedproc #param1 = 5, #param2 = 12, #param3 = null, #param4 = null, #param5 = '2017/08/29', #param6 = 1. When I right click on it -> Show output fields, it shows me the output fields from the stored procedure, but I don't know how to get the results and dump them into another table.
Using the Call DB Procedure step. With this one, I set up the input parameters, and tried to pass them through a Generate Rows step. But, With this one I don't even get the output parameters.
Please help me figure out how to do this.
With regards,
As #Brian.D.Myers suggested, the best approach is execute the stored procedure in a Table input step, and connect it to a Table output step. That, alongside the fact that the stored procedure must be executed with the following syntax: exec stored_procedure_name [#param = value,]
Thanks a lot!

SQL Server - Pass image to stored procedure, invalid local var type

I have two tables that contain document content: one for temporary staging, other for permanent storage. The content is stored as type image (cannot change this since it's current functionality).
I need a stored procedure that does the following:
Pass in a TempDocumentID that exists in temp document table.
With that TempDocumentID, select image content from temp document table.
Exec existing stored procedure that takes an image parameter to insert into permanent document table.
My problem is two-fold:
I can't declare a local variable of type 'image' to fill from the select statement of temp table. It throws error 'The text, ntext, and image data types are invalid for local variables.'
I don't know of a way to exec stored proc with direct results from select statement of temp table.
Here is my SQL Fiddle example: http://sqlfiddle.com/#!3/09384/5
Thanks,
Greg
Try this, it doesn't get an error in SQL Fiddle. I believe it will pass the result from the sub-query:
CREATE PROCEDURE MoveDocumentFromTemp
(
#TempDocumentID numeric(18,0)
)
AS
BEGIN
EXEC InsertDocumentContentFinal (SELECT TempContent
FROM DocumentContentTemp (NOLOCK)
WHERE TempDocumentID = #TempDocumentID)
END
You should be able to use VARBINARY(MAX) with SQL Server 2005 and later.
CREATE PROCEDURE MoveDocumentFromTemp
(
#TempDocumentID numeric(18,0)
)
AS
BEGIN
DECLARE #ContentToMove varbinary(max)
SELECT #ContentToMove = cast(TempContent as varbinary(max))
FROM DocumentContentTemp (NOLOCK)
WHERE TempDocumentID = #TempDocumentID
EXEC InsertDocumentContentFinal #ContentToMove
END
GO
For SQL Server 2000, you'll just have to include the INSERT code from MoveDocumentFromTemp directly into your wrapper stored procedure.

How to get current value of unique identifier,assigned it to global variable and map it in ssis execute SQL task

I am using SSIS execute SQL task to execut a stored procedure.In the stored procedure I am passing certain input values and inserting it to a table.This table has got a unique identifier as primary key.As a return output i need the current value of the unique identifier.So I am taking that to a variable #logid as shown below
INSERT INTO logging.execution_log
(ParentLogID,
Description,
PackageName,
PackageGuid,
MachineName,
ExecutionGuid,
LogicalDate,
Operator,
StartTime,
EndTime,
Status,
FailureTask)
SELECT #ParentLogID,
#Description,
#PackageName,
Cast(#PackageGuid AS UNIQUEIDENTIFIER),
#MachineName,
Cast(#ExecutionGuid AS UNIQUEIDENTIFIER),
#logicalDate,
#operator,
Getdate(),
NULL,
#status,
NULL
SET #LogID = Cast(Scope_identity() AS INT) ----#logid is output variable from ssis
but it is not giving the intended result .Here the assignment of the global variable has been done using set operator
to #logid and mapped to execute sql ssis task variable.
After some research i found that
in order to assign the global variables i have to set SET NOCOUNT option ON as the first statement in the SQL query .I have done this as well.But it is not returning anything........by the way *why do we need to set this SET NOCOUNT parameter???*any help welcome
In order for a parameter to return the value assigned in the procedure body, the parameter must be declared with the OUT/OUTPUT keyword in the procedure, like this:
CREATE PROCEDURE procname (
#logID int OUTPUT,
other parameters
)
AS
body statements
Also, the corresponding argument in the EXEC statement should be supplied with that same keyword (OUT or OUTPUT), like this:
EXEC procname #someGlobalVar OUTPUT, other arguments
You could always SELECT SCOPE_IDENTITY() AS LogID at the end of your Execute SQL Task and then set your expected result set to single row and configure the result with the single column of LogID and map that to your SSIS global variable.
Let me know if you need me to clarify any of that.
You can use Scope_Identity to get a uniqueidentifier, that is used for Identity column.
To get a uniqueidentifier, you will need to assign it to a variable before using it
Set #LogID = NewID()
INSERT INTO logging.execution_log VALUES(...., #logID, ...)
Except if you misunderstand what a uniqueidentifier means
Identity columns are auto-generated sequence of integer 1,2,3,4... and works with Scope_Identity.

How to pass variable as a parameter in Execute SQL Task SSIS?

I have ssis package in that I'm taking values from flat file and insert it into table.
I have taken one Execute SQL Task in that creating one temptable
CREATE TABLE [tempdb].dbo.##temptable
(
date datetime,
companyname nvarchar(50),
price decimal(10,0),
PortfolioId int,
stype nvarchar(50)
)
Insert into [tempdb].dbo.##temptable (date,companyname,price,PortfolioId,stype)
SELECT date,companyname,price,PortfolioId,stype
FROM ProgressNAV
WHERE (Date = '2011-09-30') AND (PortfolioId = 5) AND (stype in ('Index'))
ORDER BY CompanyName
Now in above query I need to pass (Date = '2011-09-30') AND (PortfolioId = 5) AND (stype in ('Index'))
these 3 parameter using variable name I have created variables in package so that I become dynamic.
In your Execute SQL Task, make sure SQLSourceType is set to Direct Input, then your SQL Statement is the name of the stored proc, with questionmarks for each paramter of the proc, like so:
Click the parameter mapping in the left column and add each paramter from your stored proc and map it to your SSIS variable:
Now when this task runs it will pass the SSIS variables to the stored proc.
The EXCEL and OLED DB connection managers use the parameter names 0 and 1.
I was using a oledb connection and wasted couple of hours trying to figure out the reason why the query was not working or taking the parameters. the above explanation helped a lot
Thanks a lot.
Along with #PaulStock's answer, Depending on your connection type, your variable names and SQLStatement/SQLStatementSource Changes
https://learn.microsoft.com/en-us/sql/integration-services/control-flow/execute-sql-task
SELECT, INSERT, UPDATE, and DELETE commands frequently include WHERE clauses to specify filters that define the conditions each row in the source tables must meet to qualify for an SQL command. Parameters provide the filter values in the WHERE clauses.
You can use parameter markers to dynamically provide parameter values. The rules for which parameter markers and parameter names can be used in the SQL statement depend on the type of connection manager that the Execute SQL uses.
The following table lists examples of the SELECT command by connection manager type. The INSERT, UPDATE, and DELETE statements are similar. The examples use SELECT to return products from the Product table in AdventureWorks2012 that have a ProductID greater than and less than the values specified by two parameters.
EXCEL, ODBC, and OLEDB
SELECT* FROM Production.Product WHERE ProductId > ? AND ProductID < ?
ADO
SELECT * FROM Production.Product WHERE ProductId > ? AND ProductID < ?
ADO.NET
SELECT* FROM Production.Product WHERE ProductId > #parmMinProductID
AND ProductID < #parmMaxProductID
The examples would require parameters that have the following names:
The EXCEL and OLED DB connection managers use the parameter names 0 and 1. The ODBC connection type uses 1 and 2.
The ADO connection type could use any two parameter names, such as Param1 and Param2, but the parameters must be mapped by their ordinal position in the parameter list.
The ADO.NET connection type uses the parameter names #parmMinProductID and #parmMaxProductID.
A little late to the party, but this is how I did it for an insert:
DECLARE #ManagerID AS Varchar (25) = 'NA'
DECLARE #ManagerEmail AS Varchar (50) = 'NA'
Declare #RecordCount AS int = 0
SET #ManagerID = ?
SET #ManagerEmail = ?
SET #RecordCount = ?
INSERT INTO...