Stored Procedure Data Not being Populated in Table - mysql

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?

Related

MySQL Stored Procedure works as plain SQL

I'm trying to write a stored procedure that inserts into one table (A), then queries another table (B), then finally inserts into table (C) the last insert id, along with the result from table B. I have written a stored procedure named VetIdFromCode to do the selecting from table B, which works fine in isolation. When I run the query in isolation, subbing in value for the IN parameters then it runs fine, but when I try and save it as a stored procedure it tells me invalid SQL near 'SET #LIID...'
Many thanks for any help.
CREATE PROCEDURE `NewClientUser`(
IN `uemail` VARCHAR(60),
IN `uphash` CHAR(40),
IN `uvcode` VARCHAR(11))
DETERMINISTIC
MODIFIES SQL DATA
SQL SECURITY INVOKER
INSERT INTO users (user_id,user_email,user_hash,user_role)
VALUES (NULL,uemail,uphash,'1');
SET #LIID = LAST_INSERT_ID();
CALL `VetIdFromCode`(uvcode, #VID);
INSERT INTO user_vet_lookup(user_id,vet_id)
VALUES (#LIID,#VID);
You need to start the "code" of the procedure with the key word "BEGIN" and put an "END" at the end. Like:
CREATE PROCEDURE `NewClientUser`(
IN `uemail` VARCHAR(60),
IN `uphash` CHAR(40),
IN `uvcode` VARCHAR(11))
BEGIN
DETERMINISTIC
MODIFIES SQL DATA
SQL SECURITY INVOKER
INSERT INTO users (user_id,user_email,user_hash,user_role)
VALUES (NULL,uemail,uphash,'1');
SET #LIID = LAST_INSERT_ID();
CALL `VetIdFromCode`(uvcode, #VID);
INSERT INTO user_vet_lookup(user_id,vet_id)
VALUES (#LIID,#VID);
END
Check out the documentation: http://dev.mysql.com/doc/refman/5.7/en/create-procedure.html

Can I use select sql output as an input to Insert sql, in a single stored procedure?

I wanted to retrieve a value from the select statement and use it in the insert statement. But, both these operations to be done in a single stored procedure. I'm trying something like below.
CREATE PROCEDURE Illness_sp (IN DoctorNameIn varchar(255), IN IllnessNameIn varchar(255), IN IllnessSevIn int(10))
BEGIN
select doctorId from doctor where DoctorName=DoctorNameIn;
INSERT INTO Illness(IllnessName, IllnessSeverity, PreferredDoctorId) Values(IllnessNameIn, IllnessSevIn, doctorId);
END
I have two statements inside this stored procedure:
retrieving doctorId from the SELECT statement; and
wanted to INSERT it into another table in the second statement.
INSERT INTO `Illness`(IllnessName, IllnessSeverity, PreferredDoctorId)
SELECT IllnessName, IllnessSeverity, PreferredDoctorId
FROM `SomeTable`
WHERE ...

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 Server : Insert stored procedure results into table based on parameters

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;

Executing MYSQL Stored Procedures using DBVisualizer

I am using DBVisualizer for the first time. I have made a stored procedure in mysql database. However I am unable to execute it from DBVisualizer.
This is how the procedure looks like. Here // is used as delimiter. I have four columns in the table namely slno int (autoincrement), time timestamp, price int, and prodid varchar.
*DROP PROCEDURE `spTest`//
CREATE DEFINER=`root`#`localhost` PROCEDURE `spTest`()
BEGIN
SELECT * FROM dummy1 where prodid=pr01;
END*
In DBVisualizer, I am executing #call spTest()
Where am I going wrong?
As the definer of the stored procedure is root, your DBVizualizer connection to MySQL must be as the root user in order to execute it.