how to call stored procedure from SSIS? - ssis

I want to create a SSIS package which will call the stored procedure and dump the output of the procedure into table.
SSIS package will return the output and i need this resultset ouput to dump into another table.
But before inserting i want to update the rows from that tables if any rows exist for that date or then insert the resultset.

I do't quite understand your requirements but you could do it this way:
Drop on an execute SQL Task
Ensure that YourStagingTable exists and columns match the stored procedure output
Here is some sample code to put in your Execute SQL Task that does what you want:
-- Clear Staging Table
TRUNCATE YourStagingTable
-- Save results of stored proc into staging table
INSERT INTO YourStagingTable
EXEC YourProc
-- Update any existing records
UPDATE YourFinalTable
SET YourUpdateField = YourStagingTable.YourSourceField
FROM YourStagingTable
WHERE YourFinalTable.JoinField1 = YourStagingTable.JoinField1
-- Insert any non existing records
INSERT INTO YourFinalTable (Column1,Column2)
SELECT Column1,Column2
FROM YourStagingTable
WHERE NOT EXISTS (
SELECT 1 FROM YourFinalTable
WHERE YourFinalTable.JoinField1 = YourStagingTable.JoinField1
)

Related

Call Stored Procedure While Inserting

I am trying to insert values from one table to another one using the following insert sql query
INSERT INTO [dbo].[table2]
SELECT Exec [StoredProcedure],
[Column1]
,[Column2]
FROM [dbo].[table1]
[table2] has the following columns:
RecNo, <-- INT
Column1, <--VARCHAR(50)
Column2 <--VARCHAR(50)
[StoredProcedure] main purpose is that every time a new row to be inserted in table2 it selects the maximum value from RecNo from table2 and adds 1 to that number to create the next number (sequential).
Here is the script for the stored procedure .
GO
ALTER PROCEDURE [dbo].[UpdateRcnoNumbers]
#MaxRcno INT OUTPUT
AS
BEGIN
SELECT #MaxRcno=MAX(Recno) FROM [table2]
SELECT #MaxRcno=#MaxRcno+1
RETURN #MaxRcno
END
But I am getting an error and I am not able to call the stored procedure ? Any suggestion please .
Thank you in advance
You should write a function for this purpose , read here
The problem is, Stored Procedures don't really return output directly. They can execute select statements inside the script, but have no return value.
MySQL calls stored procedures via CALL StoredProcedureName(); And you cannot direct that output to anything, as they don't return anything (unlike a function).
Here
MySQL Call Command

How to select content from a table into a new table in SQL stored procedures?

I was trying to write a procedure and needed to copy output_1 table into a new one.
This procedure :
BEGIN
SELECT * INTO newtable FROM output_1;
END
returns the following error :Undeclared variable: newtable
I thought it would create a new table and all its columns automatically.
How do I SELECT multiple columns of a table INTO a new table using a stored procedure?
EDIT :
In stored procedures, when you want to use a table to store data temporarily, you should consider using temporary tables.
Typically, if you try to store a table in a variable, you will get a multiple rows error ; in this case, temporary tables can replace variables.
CREATE TEMPORARY TABLE new_table AS SELECT * FROM output_1;
You cannot select into a table. You possibly intended
create table newtable as select * from output_1;
https://dev.mysql.com/doc/refman/8.0/en/create-table-select.html

Cannot insert new row on triggered table - "statement contains an output clause without into"?

I created a procedure, and I want to call it when row ll be inserted into my table. So I created trigger with After insert clause which executes my procedure.
And my question is why I have error while I am trying to insert new row into my table:
The target table 'dbo.mytable' of the DML statement cannot have any
enabled triggers if the statement contains an OUTPUT clause without
INTO clause.
I am working on SQL Azure, and inserting new row from Managment Portal if that's important information?
Procedure myproc
BEGIN
delete from mytable where Login like 'oo';
END
Trigger mytrigg
create TRIGGER mytrigg
ON dbo.mytable
AFTER INSERT AS
BEGIN
EXEC dbo.myproc;
END
GO

How to insert the data coming from stored procedure result into temp table with out creating temp table

I have a stored procedure which returns 110 columns, I want to insert the data coming from that stored procedure into a temp table.
Here is one solution
select *
into #tempTable
from users -- it is working fine
BUT coming to here
EXEC Sp_Users_get 12 into #tempTable
it is not working.
Please help me for solution
INSERT INTO #TempTable
EXEC Sp_Users_get 12
GO
See this, I think there may be your answer..
Select Columns from Stored Procedure Resultset
Check this out also
How to INSERT data from Stored Procedure to Table – 2 Different Methods

Querying on the stored procedure result set

Im using SQL Server 2008. I have a result set from the stored procedure and i want to fire some more queries on the result set for e.g order by desc /asc and some querying. So what is the best way to do it. Using views or by using OPENQUERY.
Please help.
You can save results of stored procedure calls in any table or table variable that has same number and type of fields as the result set returned by the stored procedure, for example:
CREATE TABLE #temp(col1 INT, col2 VARCHAR(10))
INSERT INTO #temp(col1, col2)
EXEC usp_Proc1(#param1)
SELECT *
FROM #temp
Condition is that usp_Proc1 returns rows consisting of columns of type INT and VARCHAR(10) (in that order).