SSIS SQL Task Parameter mapping - sql-server

I've a stored procedure which outputs a bit datatype. I'm trying to map the output of the stored procedure to a variable in the SSIS package (#myVar)
I'm running this from within a SSIS package and I'm trying to map the datatypes but I can't seem to figure out how to do this.
Output from Stored Procedure = bit
SSIS variable type #myVar = boolean
Mapped parameter = what??
There is no bit or bool in the tasks drop down menu, the closest is variant_bool which doesn't work, and I can't change the output type of the stored procedure -
as it is somebody's else's code.
Any Ideas, anyone?

Test code
create PROCEDURE dbo.uspGetTrueOrFalse
(
#CustomerID int,
#CustomerName nvarchar(101) output,
#IsItTrueOrFalse bit output
)
AS
BEGIN
SET NOCOUNT ON;
SET #CustomerName = 'Red Bicycle Company';
SET #IsItTrueOrFalse = 1; --Set to true
END;
GO
Setup SQL Tasks
Parameter mapping
Execution status of variables

Related

Declaring DATETIME in Stored proc MS SQL

I am trying to get System Datetime for a column when a new row is inserted or updated into a table using stored Proc in MS SQL. How can I achieve it?
I have tried below code
CREATE PROCEDUCE test_Cl_INSERT
#SRC_ID int,
#CREATED_BY datatime
AS
BEGIN
INSERT into dbo.CL_Batch(SRC_ID, Created_BY)
VALUES(#SRC_ID, CURRENT_TIMESTAMP)
END
EXEC dbo.test_Cl_INSERT
#SRC_ID=44
ERROR : #CREATED_BY parameter missing
This will work:
CREATE PROCEDURE test_Cl_INSERT
#SRC_ID int
AS
BEGIN
INSERT into dbo.CL_Batch(SRC_ID, Created_BY)
VALUES(#SRC_ID, CURRENT_TIMESTAMP)
END
EXEC dbo.test_Cl_INSERT
#SRC_ID=44
Your procedure signature is:
CREATE PROCEDUCE test_Cl_INSERT
#SRC_ID int,
#CREATED_BY datatime
You attempt to execute as:
EXEC dbo.test_Cl_INSERT #SRC_ID=44
Do you see something missing? You should. Your procedure has 2 parameters but you provide only 1 when you attempt to execute it. That is your problem. As already noted, you don't use that paramter within the logic of the procedure so why does it exist at all?
You must execute your procedure like this:
EXEC dbo.test_Cl_INSERT #SRC_ID=44, #CREATED_BY = '20201124 12:49';
Notice I just assigned a random value to the parameter since it (the parameter) is not used within your procedure code. That solves the question you ask. However, you have more important issues to consider.

SQL Server stored procedure only works with locally re-declared variables; not variables passed to the procedure

I have edited my SQL code blocks to more accurately show what is going on
Say I have a simple stored procedure:
CREATE PROCEDURE [DBO].[FOO]
(#VARIABLE VARCHAR(500))
AS
BEGIN
SELECT AVG(BAR)
FROM MYTABLE
WHERE THING = #VARIABLE AND RTRIM(LTRIM(THING)) <> ''
END
When I call this stored procedure from my classic ASP page; which in this case would be with:
Set foo = Server.CreateObject("ADODB.RecordSet")
curCmd = "Foo 'MYVARIABLE'"
foo.Open curCmd, connectionString
I get this error (on the same line as the page opens the foo object):
Arithmetic overflow error converting varchar to data type numeric.
If I call the stored procedure manually in the terminal (IDE?); then it works fine.
Also if I recreate the stored procedure as the following:
CREATE PROCEDURE [DBO].[FOO]
(#VARIABLE VARCHAR(500))
AS
BEGIN
DECLARE #VARIABLE2 VARCHAR(500) = #VARIABLE
SELECT AVG(BAR)
FROM MYTABLE
WHERE THING = #VARIABLE2 AND RTRIM(LTRIM(THING)) <> ''
END
Then the stored procedure runs fine.
I have tried dropping and recreating the stored procedure (without using the re-declaration trick), but it does not fix the issue.
*As an aside; there is validation on the data being inserted into the table to ensure that only numbers (integers) are being entered for the THING field. The THING field can also be blank; hence the where clause.
I basically have two questions:
Why does re-declaring the same variable type with the same data fix the issue?
Is there a way I can fix my problem without using this silly "re-declaration" trick?
Thanks in advance for any help with this.
I think you can get the same error if you use begin/end:
CREATE PROCEDURE [DBO].[FOO] (
#VARIABLE VARCHAR(500)
)
AS
BEGIN
DECLARE #VARIABLE2 VARCHAR(500) = #VARIABLE;
SELECT AVG(BAR) FROM MYTABLE WHERE THING = #VARIABLE2;
END;
Then, both statements will be part of the stored procedure body and you can work on fixing the data so it will work.

how to pass variable value to SSIS package executed by a job via stored procedure?

Well yeah, pretty specific.
I have a job that executes a SSIS Package. This package has a variable called "Email" which is a string.
I'm executing this job via stored procedure. I need to pass the value for that variable to the job, how do I do it?
This is the stored procedure I'm using to execute the job (#VarValue is the value I want to pass to the SSIS package):
USE [DevDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[spChecklistEmail]
#JobName [nvarchar](100),
#VarValue [nvarchar](255),
#ReturnValue [int] OUT
AS
BEGIN
SET NOCOUNT ON;
DECLARE #JobId binary(16);
DECLARE #job_status INT
SELECT #JobId = job_id FROM msdb.dbo.sysjobs WHERE (name = #JobName);
SELECT #job_status = [dbo].[fn_GetJobStatus](#JobName);
IF (#job_status IN (2, 4))
BEGIN
SET #ReturnValue=#job_status;
END
ELSE
BEGIN
IF (#JobId IS NOT NULL)
BEGIN
EXEC #ReturnValue=msdb.dbo.sp_start_job #job_id=#JobId;
END
ELSE
BEGIN
SET #ReturnValue=-2;
END
END
RETURN (#ReturnValue)
END
There isn't any real easy way to do this. The only thing I can think of is to take that value and before you call the package create a table or temp table and load the variable into that table you created. After that you could then access that varibale in you ssis package for what you need to use it for. Don't forget to clean up the extra table and/or value after the job.
Check out this link: https://msdn.microsoft.com/en-us/library/jj820152.aspx
Specifically part 2.
Looks like you can create variables and set them in the EXEC statement.
EXEC #ReturnValue=msdb.dbo.sp_start_job #job_id=#JobId, #parameter_name=N'Email', #parameter_value=#VarValue;
Something like that.

Creating t-sql procedure and output the contents

I am trying to create a stored procedure and I am so lost. I have included what I have done so far, but I know for sure I am missing a few things and can't figure it out. please help!
Question I am trying to solve:
Obtain the name and credit limit of the customer whose number currently stored in I_customer_num. place these values in the variables I_customer_name and I_credit_limit. Output the content of I_customer_name and I_credit_limit.
CREATE PROCEDURE USP_DISP_NAME_CREDIT
#CUSTOMERNUM char(3)
AS
SELECT CUSTOMER_NAME, CREDIT_LIMIT
FROM CUSTOMER
WHERE CUSTOMER_NUM = #CUSTOMERNUM
this is the way its done in sql server:
CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName>
<#Param1, sysname, #p1>
AS
BEGIN
SET NOCOUNT ON;
SELECT .......................
END
GO
also if you are using sql-server go to your database then programmability then in stored procedures right click then click on new stored procedure

SSIS Return few output xml parameters

In SSIS (sql server 2008) I have a Sql task which is calling my stored procedure.
The stored procedure gets 1 input parameter and return 2 output parameters.
This is the prototype of my SP:
declare spGetPersonDetails(personid int, #orders xml output, #names xml output )
as.....
The problem is that in my sql task in iis, i declared these 3 parameters. one input and 2 outputs, and when it's running only one of the output parameters gets a value from the sp.
Any idea?
Thanks.
I tried recreating this simply with a sample stored procedure.
Stored Procedure
CREATE PROCEDURE [dbo].[TestProcedure]
-- Add the parameters for the stored procedure here
#Input INT,
#Output1 INT OUTPUT,
#Output2 INT OUTPUT
AS
BEGIN
SET #Output1 = #Input + 1
SET #Output2 = #Input + 2
END
Then on the SQL task (which I used an OLE DB source) I had the sql statement set to
EXEC dbo.TestProcedure #Input = ?, #Output1 = ? OUTPUT, #Output2 = ? OUTPUT
Lastly all that was needed is valid parameter mapping ensuring the "Direction" column was set accurately.
I got most of this information from two sites:
http://blogs.msdn.com/b/mattm/archive/2006/11/08/stored-procedures-with-output-parameters.aspx
http://www.julian-kuiters.id.au/article.php/ssis-execute-sql-task-output-parameters
I hope this helps you, if you have more information with the specific values you have set on this SQL task I will be happy to update my answer.

Resources