We have an SSIS package that calls a Stored Proc to populate a variable object (full result set).
This is fine.
Now we have a need to call that same stored proc again but with different parameters. So really two sets of data that we want stored all together in the same variable object.
If I populate it a second time, does it overwrite what was there, or append to it?
I do this
Execute SQL Task
ResultSet = Full result set
SQLStatement - executes the stored procedure etc.
Result Set
Result Name = 0
Variable Name = User::Subscriptions
I want that to stay. But want to have a second Execute SQL Task that does the exact same thing, just executes the same stored proc with a different parameter. And I want the User::Subscription variable to hold the results of the 1st Execute SQL Task plus the 2nd Execute SQL task. Is this possible?
Adding another Execute SQL Task will overwrite first result set, you can achieve this using the following workaround:
In the Execute SQL task create a Temp table, Insert all result sets into it, select all recored from this temp table in your result set:
Your query will look like:
CREATE TABLE #TblTemp(Column1 varchar(50), .....)
INSERT INTO #TblTemp(Column1 varchar(50), .....)
EXEC Stored_Procedure_1
INSERT INTO #TblTemp(Column1 varchar(50), .....)
EXEC Stored_Procedure_2
SELECT * FROM #TblTemp
Related
We have parameter direction as output in parameter binding and at the same time we do have result binding. So if we are having output variable or return type variable which will be available at output, so why do we need Result binding then.
ResultSets and output parameters are not the same, each one of them has it own use:
ResultSets are used to store a result of a Select query: It can be a one or more columns and it can be a single row or a full result set. ResultSets are retrieved as ADO RecordSets and can be stored within variables. In general a RecordSet can be consumed on time.
Output Parameters are used to store some values that can be set any part of the SQL command (not necessary at the end). Parameters have the same concept of a SQL stored procedure parameters. The value can be used several times.
You can have an Execute SQL Task with output parameters and a ResultSet.
Additional Information
SQL Server Performance ResultSet vs Output Parameter vs Return Value
Result Sets in the Execute SQL Task
Map Result Sets to Variables in an Execute SQL Task
Parameters and Return Codes in the Execute SQL Task
Update 1 # 2019-16-08
You can use the output parameter as input parameter in another stored procedure but you have to execute it in a separate Execute SQL Task.
If you need to execute both stored procedures within one Execute SQL Task, then you can use SQL variables as mentioned in the example below:
DECLARE #output VARCHAR(50)
EXEC proc1 #output OUTPUT
EXEC proc2 #output
Update 2 # 2019-19-09
Recently I published a detailed article about this topic on SQL Shack, you can check it on:
Execute SQL Tasks in SSIS: Output Parameters Vs Result Sets
I want to create temp table with their unique name by a select query in a stored procedure in SQL Server.
For example: whenever I run the select query at that time different temp table name want to create.
Let be more clear, at the first time when I will run the select query at that time temptable name is temptable1, while at the second time the table name will be temptable2 and so on.
I want to know the syntax for executing the select query and creating the temptable with their unique name in a stored procedure in SQL Server.
In the context of the SQL Server Stored Procedure, the engine is handling itself the names of the temporary tables.
There is no need to worry if many users are executing the same stored procedure in same time - the temporary objects cannot be shared across them and no conflicts are going to happen.
Also, naming a temporary table in stored procedure with different name can be done using a dynamic T-SQL statement. You can for example, use a sequence to get such number and concatenate it to the table name. But, if you do so, you need to use sp_executesql to create your table and do things with it. In such way, no other stored procedure would be able to read/modify the table you have created in the current stored procedure. In other words, the temporary table cannot be shared over the routines if created using dynamic T-SQL statement. So, there is absolutely no point of doing such thing.
I using ssis package.I want insert flat file source (text file) to sql.
Addres of text file is dynamic so i define variable for path.I have the sp like this:
CREATE PROCEDURE [dbo].[Insert_FileMaster]
#FILE_PATH nVARCHAR(MAX)
,#id int OUTPUT
AS
BEGIN
insert into [dbo].[FileMaster] ([FM_Name])
values(#FILE_PATH))
set #id = ##IDENTITY
END
I want exec this sp With variable parameter.
this is my package:
Which ssis tool should i use?and how to get output from sp (return parametert must be use in another sp in package)?
You will want to add an Execute SQL Task before your Data Flow Task (this will be at the Control Flow level).
You will need to configure the Execute SQL task as described in this answer.
Insert a single row and return its primary key
I have 2 systems that use the same stored procedure.
The problem is that one system needs data as normal select statement inside the stored procedure, while the other needs the data in XML format
I just looking for best practice,
do I have to create 2 stored procedures so each system read from its own stored procedure?
or I create one stored procedurefor normal data and the other stored procedure read data from the first one and fill it in temp table using OPENROWSET?
or add a parameter for data output so if I want data i pass 1 if I want xml I pass 2?
any other suggestions?
Thanks
Depends on the details of the stored procedure but as a rule of thumb I prefer to add a parameter to determine the output type. In this way you will avoid to maintain two procedures and the risk of going out of sync at some point in the future.
I would suggest Two Stored Procedures. as using OPENROWSET might have some performance issues.
When you say you need data in XML format, this query will look very different from the query which returns data in rowset and both queries will have a very different execution plan.
So if you decide to embed this non-XML procedure inside a OPENROWSET query and then try to pull XML OUTPUT from that query might result in a very inefficient query plan.
I would say simply create Two Separate procedures and have two separate Execution plans for each procedure which can possibly result in a better performance.
Another Option that comes to my mind is that you can add a Parameter to your Procedure like #XML_OUTPUT and at run time check the value of Parameter and run respective queries, something like this...
CREATE PROCEDURE Get_DATA
#Param1 DataType,
#Param2 DataType,
#XML_OUTPUT BIT
AS
BEGIN
SET NOCOUNT ON;
IF (#XML_OUTPUT = 1)
BEGIN
SELECT *
FROM TABLE
FOR XML PATH('')..... bla bla
END
ELSE
BEGIN
SELECT *
FROM TABLE ... bla bla
END
END
And at run time if #XML_OUTPUT was set to 1 , the procedure will return XML Data, else it will return non-xml data.
I have two stored procedures; I am calling one stored proc from another.
I return a table from the 1st stored procedure. When I execute the 1st alone, I get the table correctly.
But when I call the 1st stored procedure from another stored procedure, it always returns command completed successfully and no results.
I call it like this in stored proc 2:
set #query = 'exec servername.dbo.storedproc1 #ClassName = ''' +
#ClassName +''', #StatusName = ''' + #StatusName
exec(#query)
Inside the outer procedure, create a temporary table with a similar schema to the result set returned from the inner procedure.
When calling the inner procedure, use insert..exec, like this:
insert #tempTable exec InnerProcedure
Then select the data from the temporary table.
First create a temporary table that captures data obtained from executing first procedure . Then use Select statement to retrieve desired data from temporary table. Don't forget to use drop the temporary table table after Select statement else next time you execute the procedure error will be thrown saying table already exist . On other hand you can use table variable to avoid dropping table as scope of table variable is limited to lifetime of the containing stored procedure.
`
Insert into #temptable
Exec sprcdre1
Select * from #temptable