I have created a stored procedure with the following parameters:
#id int, #machine varchar(50)
The stored procedure queries 2 tables with a left join. Table 1 has 2,500,000 rows and table 2 has 750,000 rows.
The execution of the stored procedure takes about 72 seconds.
When I change name of param #machine to #machineID, the stored procedure takes about 10 seconds.
Do you know why this is so?
Related
I have created a stored procedure in MySQL named: inventory_procedure(fromDateTime)
A third party system executes this stored procedure at regular 1 hour interval by passing fromDateTime in dd-mm-yyyy hh:mm:ss format.
Sometimes, third party systems reports that stored procedure didn't returned any data.
When I hit the stored procedure it returns results for me.
is there any way where I can fetch information like:
When stored procedure was executed?
What was the fromDateTime passed in storedProcedure?
How many rows stored procedure returned?
Create a log table like this one:
create table inventory_procedure_log (
id int unsigned auto_increment primary key,
ts timestamp,
fromDateTime datetime,
rows_returned int
);
You will probably want to include some indexes for fast searches.
Insert the log entries within your procedure right after the final SELECT:
create procedure inventory_procedure (fromDateTime datetime)
begin
-- random dummy result set
select n
from (select 1 n union select 2 union select 3) as x
where rand() < 0.5;
-- insert log entry
insert into inventory_procedure_log(ts, fromDateTime, rows_returned) values (
now(),
fromDateTime,
found_rows()
);
end
Now you can search in your log table. For example - show all entries for the last 24 hours:
select *
from inventory_procedure_log
where ts > now() - interval 24 hour;
Se demo on dbfiddle.uk
You can extend the log table for more info (like session ID) as needed.
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?
Why is my query output different between SP and query execution. I am executing a simple select query to assign some value..
SP:
CREATE DEFINER=`root`#`localhost` PROCEDURE `INS_EXPENSES`(
IN CATNAME varchar(100),
IN SUBCATNAME varchar(100),
IN AMOUNT INT,
IN USERID INT,
IN DESCRIPTIONS varchar(100)
)
BEGIN
SELECT SUBCATNAME;
SELECT * FROM SUBCAT WHERE SUBCATNAME='MISC';
END
THE ABOVE SELECT QUERY RETURNS ALL DATA FROM THE SUBCAT TABLE. WHERE IN SAME QUERY WORKS FINE IF EXECUTED OTHERWISE.
Your procedure has parameter SUBCATNAME and the WHERE-clause is using that value instead of the column with same name when it compares with the "MISC"-string.
You should use a prefix for the procedure parameters so that they would not get mixed up with column names (use parameter names like in_SUBCATNAME or similar)
I have a stored procedure which contains more that 25 queries and pass the line limit of MySQL stored procedure causing memory exhausted error.
So what I want is to divide it into two stored procedures and create a third stored procedure to union the two procedures. Can anyone help me with this?
Just push the results of both sp calls into a temp table:
/*Create a table with the same columns that the sp returns*/
CREATE TABLE #tempblahblah(blahblahblah NVARCHAR(50))
INSERT #tempblahblah ( blahblahblah )
EXEC MyStored 0
INSERT #tempblahblah ( blahblahblah )
EXEC MyStored 1
SELECT * FROM #tempblahblah
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).