I have nested stored procedures and I need to create a LOCAL TEMP TABLE in child procedure and be able to use it in the parent procedure.
EX:
Parent procedure:
EXEC ChildProcedure
SELECT * FROM #TempTable
Child procedure:
CREATE TABLE #TempTable (Field1 VARCHAR(1000),Field2 VARCHAR(1000))
INSERT INTO #TempTable (Field1,Field2) VALUES ('1','2')
When I try this, SQL says:
Invalid Object Name '#TempTable'
Is there any way to achieve this without GLOBAL TEMP TABLES ?
Well I think I finally found the answer to my question in https://msdn.microsoft.com/en-us/library/ms174979.aspx.
A local temporary table created in a stored procedure is dropped automatically when the stored procedure is finished. The table can be referenced by any nested stored procedures executed by the stored procedure that created the table. The table cannot be referenced by the process that called the stored procedure that created the table.
So the answer to my own question is NO. I can't do that in that way.
The best approach (as #Damien_The_Unbeliever said) is to create the table in the parent procedure and populate it inside the child procedure.
Instead of Procedure you can create a table valued function like this and use a
Create Function ChiledFunction ()
Returns #TempTable Table (Field1 VARCHAR(1000),Field2 VARCHAR(1000)) AS
Begin
INSERT INTO #TempTable (Field1,Field2) VALUES ('1','2')
Return
end
and parent procedure is here
Create Procedure ParentProc
As
begin
select * from dbo.ChiledFunction()
end
Related
I have a stored procedure called Sp_Ejecucion inside it create a temporary table with the following structure:
CREATE TABLE #CambioResult (FOL INT IDENTITY, RESULT INT)
and after that command to run another Sp as follows
BEGIN TRAN T1
DECLARE #vnCambiaRollTurnoResult INT = 0,
#vnReacomodoMarcajesResult INT = 0,
#Result INT = 0
BEGIN TRY
exec nsp_Exec #nClaEmp = #pnClaEmpresa,
#nClaTrab = #pnClaTrab,
END TRY
BEGIN CATCH
GOTO RETURN_ERROR
END CATCH
And the inside of the second stored I want to insert data to the table created in the first stored, How can I do it? what I have intended is the following:
INSERT INTO OBJECT_ID('tempdb..#CambioResult')
select #Error
pero marca error en esta parte OBJECT_ID
As you are inserting within the nested procedure, you will be able to reference the parent procedure temporary table #CambioResult inside nested procedure.
You have to change your INSERT statement in the nested procedure: nsp_Exec as given below:
INSERT INTO #CambioResult
SELECT ....
About Temporary table scope, reference msdn
Temporary tables are automatically dropped when they go out of scope,
unless explicitly dropped by using DROP TABLE:
A local temporary table created in a stored procedure is dropped automatically when the stored procedure is finished. The table can be
referenced by any nested stored procedures executed by the stored
procedure that created the table. The table cannot be referenced by
the process that called the stored procedure that created the table
I have a question here regarding filling temp table from stored procedures in SQL Server.
When we already have table schema of a table we can fill it from stored procedure as:
Create #tempTable (Id int, Value varchar(50))
Insert into #tempTable
exec GetValues
Where GetValues returns the same schema as declared for #tempTable.
Here is another case when we fill a temp table from another table
Select colA,colB into #tempTableA from SomeTable
Here we don't need to know the schema of #tempTableA, it will be same as based on selected columns from table SomeTable.
My question is: how can we fill a #temptable without knowing it's schema, from a stored procedure? As we do when filling a temp table from some other table.
SELECT * INTO #tmpTable FROM OPENQUERY(YOURSERVERNAME, 'EXEC test.dbo.prc_test 1');
Insert results of a stored procedure into a temporary table
I am trying to create a stored procedure which returns a temp table to another stored procedure. However, I cannot seem to be able to set a table as an output variable. For instance:
#t table(ID int) output
as a parameters does not work. However the following does not complain:
#t int output
Is there a way to get one stored procedure to obtain a table generated by another stored proceedure? Thanks ahead of time!
The answer is No.
You could pass the name of the created table from one Stored Procedure to another and then in the second SP you can use the name to query thru the table.
Yes, you can achieve this by using a User Defined Table Type. All you need to do is
Create a User defined Table Type.
Create your first Stored Procedure to Accept a Table Type variable.
In your second procedure declare a Table Type variable, populate it with the data you want and call the first procedure by passing in the Table Type variable.
Here is a sample code.
1) Create the User Defined Table Type. Under the Programmability -- > Types -- > User Defined Table Types -- > Right click to create your table type
CREATE TYPE [schemaname].[Ttype] AS TABLE
(
EmployeeID int,
EmployeeName varchar(100),
)
GO
The above type is just an example, put in the columns that you require in it. Here Ttype is the Name of the TableType (you can name it as per your convenience)
2) Create your first stored procedure that will accept the Table type as variable so that you can use it.
CREATE PROCEDURE [schemaname].[StoredProcName]
#TableType Ttype READONLY
AS
BEGIN
-- Just an example on how to use the TableType.
INSERT INTO TableName(EmployeeId,EmployeeName)
SELECT EmployeeID,EmployeeName FROM #TableType
END
GO
The above Stored Procedure accepts a Table Type variable. The Ttype indicates that the #Tabletype variable is user defined table type. The READONLY indicates that no DML operations can be performed on the Table type parameter.
3) Now, create your second procedure that will pass the Table Type parameter to the above procedure created.
CREATE PROCEDURE [schemaname].[StoredProc2Name]
AS
BEGIN
SET NOCOUNT ON;
DECLARE #Table as Ttype
INSERT INTO #Table(EmployeeID,EmployeeName)
SELECT EmployeeID,EmployeeName FROM schemaname.Employees
EXEC [schemaname].[StoredProcName] #Table
END
GO
In the above SP, you declare a variable named #Table which is of the Table Type (Ttype). Now you populate it with the data you need and then call the first procedure by passing in the #Table variable.
I have to create a 10 stored procedure as follows:
In stored procedure # 1 I create temp table 1 and this temp table is used in stored procedure 2 to create another temp table and this new tem table is used in another STORED PROCEDURE and so on.
I am not sure how to create these stored procedure, because for these stored procedure I need to have temporary tables present in temdb.
Any help
Can you user Global Temporary Tables?
SELECT * INTO ##Users FROM UserTable
The Global temp tables will remain in tempdb until deleted and can be used across different stored procs.
Assuming you want to name the table (or some of its columns) that's about to be created based on the data present in the temp table, you might want to resort to dynamic SQL, since you can't use variables like this:
declare #foo varchar(50)
select #foo = tableName from #tempTable
create table #foo (fooColumn int)
But before you even think of using dynamic SQL, you've got to ask yourself whether you really need this solution.
I need to access the result of a stored procedure within a select statement, i.e.:
SELECT * FROM [dbo].[sp_sample]
in SQL_Server 2005.
This isn't possible. You would have to create a temporary table to store the results.
Create Table #tmp
(
...
)
Insert into #tmp
Exec dbo.StoredProcedure
The table structure must match the output of the Stored Procedure.
#Barry is right you need to create a temp table and insert into it first, then join that table in your select.
However, there are numerous ways for sharing data between stored procedures, see this excellent article: How to Share Data Between Stored Procedures by Erland Sommarskog
One method that may work for you is to "share" a temp table. The #temp table is created in the Parent procedure and can be used by the Child: http://www.sommarskog.se/share_data.html#temptables