SQL Server : Insert stored procedure results into table based on parameters - sql-server-2008

I have a stored procedure Test_Sp which returns data in this way:
Id Name Age Address State Country
1 ManiS 25 aaaa bbb ccc
This stored procedure is returning 6 columns of data, but I want to insert only the first 2 columns into a temp table..
My temp table variable is:
Declare #testTbl Table (RowId int identity, Name nvarchar(100), Age int);
INSERT INTO #testTbl(Name,Age) EXEC [Test_Sp] 23;
Select * from #testTbl;
But I get this error:
Msg 50000, Level 16, State 0, Procedure Test_Sp, Line 16
Cannot use the ROLLBACK statement within an INSERT-EXEC statement.
I am aware about Select * into & if I create a temp table with same columns as stored procedure output means it will work..
My question: is it possible to insert just two columns into a temp table variable from the stored procedure output based on parameters?

Option 1:
Create an intermediate temp table with all the columns that sp returns and then do this:
INSERT INTO Temp
Exec [Test_Sp] 23;
Then
INSERT INTO #testTbl(Name,Age)
select name,age from temp
Option 2:
Modify your sproc and add one more bit datatype parameter #limitedcolumn
If #limitedcolumn=true the return only required columns else return all columns
INSERT INTO #testTbl(Name,Age) EXEC [Test_Sp] 23,true;

Related

Stored Procedure Data Not being Populated in Table

CREATE TABLE TempTable (
[Fiscal Quarter] varchar(50),
[Unique ID] int,
[Forecasted Amount (converted)] Numeric (25,2),
[Lead Source] varchar(50),
[AD] varchar(50),
[Stage] varchar(50),
[Opportunity Name] varchar(50))
INSERT INTO TempTable Exec [MOPs].[MSP_INQTRWon_Final_Proc_VG]
**
Question: When I insert data from stored procedure into a table; it shows 0 rows affected. However when I execute the stored procedure it says 455 rows affected.
**
However when I execute the stored procedure it says 455 rows affected.
When you execute the stored procedure it should return rows like select statement in order to insert those rows into a table. Instead if it's just doing some operation on those rows there is nothing to insert.
Please confirm whether your stored procedure returns rows when you execute.
In MySQL stored procedure is executed via call procedurename();
https://dev.mysql.com/doc/refman/8.0/en/call.html
SQLServer use exec. Is your database MySQL? Then which version?

Is there any way to store a SELECT result and output the resultset at the same time?

In the next sample procedure, can I output the row from the queried table as a procedure resultset, and at the same time store a single field or some fields in a variable (similarly as when I use SELECT INTO)? The only way I can imagine is repeating the query.
CREATE PROCEDURE `Panel_TerminalesForm`(IN idTerminal INT)
BEGIN
declare somefield INT:
-- this select statement returns the found row as a resultset
SELECT terminales.*
FROM terminales
WHERE id_terminal = idTerminal;
-- but I also want to have here a table field inside the variable somefield
-- do some manipulation with somefield...
END;
If i understand you correctly, you need to create a temporary table, then you can manipulate the temporary the way you want.
MySQL:
CREATE PROCEDURE `Panel_TerminalesForm`(IN idTerminal INT)
BEGIN
declare somefield INT
CREATE TEMPORARY TABLE SampleTempTable
SELECT * INTO SampleTempTable FROM terminales WHERE id_terminal = idTerminal;
-- output the resultset
SELECT * FROM SampleTempTable
-- read the variables you want
SELECT field INTO somefield FROM SampleTempTable
-- Drop the temp table
DROP TEMPORARY TABLE SampleTempTable
END;
http://www.mysqltutorial.org/mysql-temporary-table/
SQL Server:
CREATE PROCEDURE `Panel_TerminalesForm`(IN idTerminal INT)
BEGIN
declare somefield INT
SELECT terminales.*
INTO #tempSample FROM terminales
WHERE id_terminal = idTerminal;
SELECT * FROM #tempSample
-- Drop the temp table
DROP TABLE #tempSample
END;
Note: Drop the temp table after using it to avoid errors.

Stored procedure used to insert datas in two tables

I am using a stored procedure to insert data into two tables. But when I insert the datas the total number of rows in the first table and the second table is different, so it means that sometimes it only inserted the datas in the first table but failed to insert it in the second table. But this case should not happen in my case as the Id of the two tables is related to each other. How can I solve this problem? So that when it will insert datas in both tables or no table if an error occurs so that the number of datas are the same in both the table. My stored procedure is as follows:
Begin
insert into base_table(imgPath,store,apparelType) values (imgPath,store,apparelType);
insert into data_table(cvID,color) values
(LAST_INSERT_ID(),color);
END
To make sure that the 1st query has been successfully executed, the best way would be to add an Identity column in your base_table, then proceed as follows;
DECLARE #LAST_INSERT_ID INT
DECLARE #EXECUTION_OK char(1)
SET #EXECUTION_OK = 1
insert into base_table(imgPath,store,apparelType) values (imgPath,store,apparelType)
SELECT #LAST_INSERT_ID = SCOPE_IDENTITY()
insert into data_table(cvID,color) values (#LAST_INSERT_ID, color)
GO
If exists( Select cvID from data_table where cvID= #LAST_INSERT_ID)
Begin
#EXECUTION_OK = 0
End
SCOPE_IDENTITY: Returns the last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch. Therefore, two statements are in the same scope if they are in the same stored procedure, function, or batch.
You can also use the mysql_affected_rows() function to verify that the query has been successful.

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).

SQL - Get stored procedure multiple record sets to temp table variable

Is it possible to get stored procedure multiple result set to temp table variable?
I wrote a stored procedure like,
Create proc test1
(
#param1 int,
#param2 int
)
as
Begin
Select Id, Name from Table1 where column1=#param1;
Select Id, Age, Address from Table2 where column1=#param2;
End
When i execute this sp it will return 2 tables(record set's).
Now, i want to get this 2 record sets to 2 temp table variables.
How can i achieve this?
I don't believe a stored procedure can return multiple result sets like you want it to. What I might suggest is to have the stored proc store the results in two global temp tables and then have the calling process (whatever is calling your stored proc) query the two global temp tables and put the results into your temp table variables.
I know this is an old question, and maybe I am misunderstanding what you want, but if you just want to take those two recordsets and put them into temp tables, couldn't you do this:
Create proc test1
(
#param1 int,
#param2 int
)
as
Begin
Select Id, Name
into #temp1
from Table1 where column1=#param1;
Select Id, Age, Address
into #temp2
from Table2 where column1=#param2;
End
now, I dont know what you're trying to achieve with this, but depending on how you're calling that sp, the #temp tables may not be accessible from outside of the sp.