Is it possible to find out when a Stored Procedure was last accessed?
I tried the following:
SELECT *
FROM sys.dm_db_index_usage_stats
WHERE [database_id] = DB_ID()
AND [object_id] = OBJECT_ID('stored procedure name')
and it returns a blank resultset.
I believe this is possible should the sproc still be in the procedure cache on the server at which point you can query sys.dm_exec_query_stats.
After that you are down to logging and tracing I'm afraid.
Add a log entry as the first line of the stored procedures:
insert into dbo.ProcLog (procname, date) values ('MyProc',getdate())
here is a generic line of code you can place in every procedure, it will include the proper procedure name, without hard coding it.
INSERT INTO YourLog
(RunDate,ProcedureName,...)
VALUES
(GETDATE(),OBJECT_NAME(##PROCID),...)
Related
I have a Stored Procedure wrote in SQL Server that I need to convert to MySQL, which has this code inside.
SQL server code
declare #resultatSP table (...);
...
insert into #resultatSP execute other_procedure(...);
...
My problem is that in SQL Server, the "result" of the last select done in the SP other_procedure is stored in #resultatSP. (I don't know if it is conventional but it works), but I cannot reproduce it in MySQL.
I tried things like this:
MySQL code
drop temporary table if exists resultatSP;
create temporary table resultatSP (...);
...
resultatSP = CALL other_procedure(...);
OR
CALL other_procedure INSERT INTO resultatSP;
But nothing works.
I read that in MySQL, call stored procedure does not return result and we must use OUT/INOUT parameters. But I cannot change the other_procedure(...).
What is the most confusing is that this other_procedure has been convert in MySQL and it is used in java with some Javax.persistance annotations as #NamedNativeQuery or #SqlResultSetMapping and these annoations succeed to get the "result" of the last select of the stored procedure other_procedure(...) converted in MySQL. I don't know how these annotations make it.
We have a lot of stored procedures which have an OUT parameter which we are assigning a value to by using SELECT LAST_INSERTID() INTO p_AutoNumber.
After this, we need to perform another INSERT which also uses the value of this variable - however it doesn't appear to run correctly.
Does performing a SELECT INTO on an OUT parameter return from the procedure immedately? I am unable to find any information on this in the MySQL docs for SELECT INTO.
You can use the out-parameter within the procedure. Just check the syntax (and the function name):
Use syntax:
SELECT LAST_INSERT_ID() INTO p_AutoNumber;
Alternatively you can use syntax:
SET p_AutoNumber = LAST_INSERTID();
I need to increment the next highest character field upon an insert - e.g. if '007' exists, then next is '008'
I have a stored procedure:
BEGIN
SELECT LPAD(CAST(MAX(Line_Order) AS SIGNED INTEGER) + 1,3,'0')
FROM bill_project_lineitems
WHERE Item_Id = vItem;
END
Then I have this trigger:
CALL MaxLineOrder(new.item_id,#new.line_order )
Which I call before.
When I try to INSERT, MySQL complains: 1415 - 'Not allowed to return a result set from a trigger'
So then how do I solve this problem?
Ok, I read again and MAYBE I undestood. (but why not pasting the full code?) The error is clear, You CANT return a resultset from a Trigger.
I suppose that you want to issue an INSERT and get a resultset. But you can't. So you should:
Move your INSERT into the Stored Procedure
DROP the Trigger
CALL the Procedure.
Stored Procedures can return a resultset (or even many resultsets), so this will work. I hope that this is what you want.
[SQL Server 2008 Std]
I have a stored procedure that is used to insert new data into a table, which works as expected. However I now need to call this stored procedure multiple times using the results of a query as the parameters.
e.g.
select
name, age, foo, bar
from
sometable
where
wobble = 'true'
exec insertProc name age foo bar
I know I can use a cursor to acheive this, but I keep reading that "cursors are bad"...but I don't know any other way to do this?
One solution is to use cursor. Other is to prepare your result set into temp table before calling the procedure and then supply it to the procedure ( you have to alter the procedure by adding table-value param as input param). Some info in msdn.
In a sql script that does sequential execution, is there a way one can introduce an IF THEN ELSE conditional to control the flow of query execution?
I happened to run into this http://www.bennadel.com/blog/1340-MySQL-Does-Not-Support-IF-ELSE-Statements-In-General-SQL-Work-Flow.htm
which says that the IF THEN ELSE will not work in a sql script.
Is there another way around?
Basically, I want to run a particular "select colName from table" command and check if colName corresponds to a particular value. If it does, proceed with the rest of the script. Else, halt execution.
Please advise.
I just wrap my SQL script in a procedure, where conditional code is allowed. If you'd rather not leave the statements lying around, you can drop the procedure when you're done. Here's an example:
delimiter //
create procedure insert_games()
begin
set #platform_id := (select id from platform where name = 'Nintendo DS');
-- Only insert rows if the platform was found
if #platform_id is not null then
insert into game(name, platform_id) values('New Super Mario Bros', #platform_id);
insert into game(name, platform_id) values('Mario Kart DS', #platform_id);
end if;
end;
//
delimiter ;
-- Execute the procedure
call insert_games();
-- Drop the procedure
drop procedure insert_games;
If you haven't used procedures, the "delimiter" keyword might need some explanation. The first line switches the delimiter to "//" so that we can include semi-colons in our procedure definition without MySQL attempting to interpret them yet. Once the procedure has been created, we switch the delimiter back to ";" so we can execute statements as usual.
After doing some research I think I may have found a way to work around this. I was looking for a way to verify if a script had already executed against a target database. This will be primarily for version control of my databases. I have a table created to keep track of the scripts that have been executed and wanted some flow inside my scripts to check that table first before execution. While I have not completely solved the problem yet I have created a simple script that basically does what I need, I just need to wrap the DDL into the selects based on the value of the variables.
step 1 - Setup a bit variable to hold the result
step 2 - do your select and set the variable if the result is found
step 3 - Do what you need to do on false result
step 4 - Do what you need to do on true result
Here is the example script
set #schemachangeid = 0;
select #schemachangeid := 1 from SchemaChangeLog where scriptname = '1_create_tables.sql';
select 'scriptalreadyran' from dual where #schemachangeid = 1;
select 'scriptnotran' from dual where #schemachangeid = 0;
I also recognize this is an old thread but maybe this will help someone out there trying to do this kind of thing outside of a stored procedure like me.