I use below code to create procedure to using temp table
Go
create procedure testTempTable
as
INSERT INTO #resultTbl (code,userName) SELECT code,userName FROM Customer
select * from #resultTbl
Go
When I want to run the procedure with exec testTempTable says
Invalid object name '#resultTbl'.
How can I use temp table in the procedure?
Because your temp table might not be created, so you can't get result set from #resultTbl. you can try to use SELECT ... INTO temp table or create a temp table before you use it.
create procedure testTempTable
as
BEGIN
SELECT code,userName
INTO #resultTbl
FROM Customer
SELECT *
FROM #resultTbl
END
Go
Related
This my code where i want to insert the result of my stored procedure into #TempTable
SELECT * INTO #Tempannuelguiftrecap
EXECUTE PSGetDetailMensuelPPM #Year,#Mois
You can't SELECT from a Stored Procedure. Something like SELECT * FROM EXECUTE dbo.MySP isn't going to work.
You can still INSERT the data from a Stored Procedure into a table, however, you must first define the table, and then INSERT the data. This is Pseudo-SQL as we have no definitions of your objects, however, this should get you on the right path:
CREATE TABLE #MyTempTable({Column1} {Data Type}[,
{Other Column(s)} {Column DataType(s)} ..... ]);
INSERT INTO #MyTempTable ({Columns List})
EXECUTE dbo.MyStoredProcedure #MyParam;
I have a stored procedure ModifiedName, is it possible to list the name of all the rows in a table and display the "modified name" next to them?
Something along:
SELECT name, EXEC ModifiedName name
FROM table
but which would work...
First you have to write a function instead of procedure. You cannot call a procedure inside a select statement ,But a function can.
Inside the function write your logic to modify the name and use it in the select query.
For example write a function like below.(This will append 'Mr.' with name )
CREATE FUNCTION dbo.ModifiedName ( #name VARCHAR(50))
RETURNS VARCHAR(150)
AS
BEGIN
DECLARE #ModifiedName VARCHAR(150)
SET #ModifiedName= 'Mr.'+#name
RETURN #ModifiedName
END
And use the below script to get the original name and modified name in the select list.
SELECT name, dbo.ModifiedName (name)
FROM table
You can do
CREATE TABLE SomeName(... appropriate definitions ...);
INSERT INTO SomeName
EXEC YourSP;
SELECT * FROM SomeName;
But - to be honest - I think you are trying something you should solve in another way...
Create a function
Try to inline your logic
UPDATE This is for #NEER
Hi NEER, don't know what you mean with your comment, but try this:
CREATE PROCEDURE dbo.tmpTest AS
BEGIN
SELECT TOP 3 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
END
GO
CREATE TABLE #tmpTable(ColName NVARCHAR(MAX));
INSERT INTO #tmpTable
EXEC dbo.tmpTest;
SELECT * FROM #tmpTable;
GO
DECLARE #tmpTbl TABLE(ColName NVARCHAR(MAX));
INSERT INTO #tmpTbl
EXEC dbo.tmpTest;
SELECT * FROM #tmpTbl;
GO
DROP TABLE #tmpTable;
DROP PROCEDURE dbo.tmpTest;
But - in general - if you just want to read data one should rather use a VIEW or an inline TVF. Procedures are designed to do something...
If parameterless sp, you can with a temp table. Otherwise you should use a function.
CREATE TABLE #tmp (Name INT)
INSERT INTO #tmp
EXEC ModifiedName
SELECT name, (SELECT TOP 1 Name FROM #tmp) name
FROM table
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
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 have a SQL script that is setting up two database tables with their keys and constraints without any problem. I won't include the whole code but the 'skeleton' of it looks like this:
BEGIN
CREATE TABLE [table] (
)
CREATE TABLE [table2] (
)
ALTER TABLE table...
ALTER TABLE table2....
END
I am stuck trying to add stored procedures to this script though, ideally I would like to include this all within the same script. Could someone tell me how to include the following stored procedure into the above script?
CREATE PROCEDURE Test
#x int
AS
BEGIN
SELECT COUNT(*)
FROM table
END
GO
I have tried putting it towards the end of the script and have also tried with and without the BEGIN, END and GO tags but I keep getting an error that says 'incorrect syntax near PROCEDURE'.
Try it like this:
USE BDNAME
GO
BEGIN
CREATE TABLE [table] (
)
CREATE TABLE [table2] (
)
ALTER TABLE table...
ALTER TABLE table2....
END
USE BDNAME
GO
CREATE PROCEDURE Test
#x int
AS
BEGIN
SELECT COUNT(*)
FROM table
END
GO
Instead of using BEGIN END, put GO between all your Statements like Create, Alter. Also I would like to inform you that putting GO will create blocks in your script, so if you create some local variable in one block, it is not accessible in another.
CREATE Table Table1(
--Your Code
)
GO
CREATE PROCEDURE Test
#x int
AS
BEGIN
SELECT COUNT(*)
FROM Table1
END
GO
--Continue your script
Hope this helps.