call aspnet_Delete_User SP in trigger - sql-server-2008

I have a trigger in students table that deletes related records from other tables when i delete a student. I want to delete student's membership data by calling aspnet_delete_user stored procedure in the trigger . but this works just if I delete one student . and if I remover multiple students in one query it doesn't work .
How to call this SP for multi-row operation trigger ?

You'd have to use a cursor or build a dynamic SQL string (which uses a cursor without saying so). Alternatively, you could copy the logic from the stored procedure and see if you can tailor it to become set-based - I haven't looked at the procedure, so I'm not sure if this is feasible, practical or even possible, but it's the first thing I try to do before adding cursors or dynamic SQL to a trigger.
For a cursor, something like this (I'm guessing you pass a GUID or something to the procedure, but I have no idea):
CREATE TRIGGER dbo.StudentAfterDelete
ON dbo.Students
AFTER DELETE
AS
BEGIN
SET NOCOUNT ON;
DECLARE #MemberID UNIQUEIDENTIFIER;
DECLARE c CURSOR LOCAL STATIC FORWARD_ONLY READ_ONLY
FOR SELECT MemberID FROM deleted;
OPEN c;
FETCH NEXT FROM c INTO #MemberID;
WHILE ##FETCH_STATUS <> -1
BEGIN
EXEC dbo.aspnet_delete_user #MemberID;
FETCH NEXT FROM c INTO #MemberID;
END
CLOSE c;
DEALLOCATE c;
END
GO
Dynamic SQL, same assumptions:
CREATE TRIGGER dbo.StudentAfterDelete
ON dbo.Students
AFTER DELETE
AS
BEGIN
SET NOCOUNT ON;
DECLARE #sql NVARCHAR(MAX) = N'';
SELECT #sql += 'EXEC dbo.aspnet_delete_user '''
+ CONVERT(VARCHAR(36), MemberID) + ''';' FROM deleted;
EXEC sp_executesql #sql;
END
GO
However including the missing information up front is more useful. Don't assume that everyone who works with SQL Server has any clue what aspnet_delete_user does.

Maybe something like this:
CREATE TRIGGER TheNameOfTheTrigger
ON YourTableYouWantToTriggerOn
AFTER DELETE
AS
DECLARE #yourPrimaryKey int
DECLARE delete_cursor CURSOR FOR
SELECT
yourPrimaryKey
FROM
deleted
OPEN delete_cursor
FETCH NEXT FROM delete_cursor
INTO #yourPrimaryKey
WHILE ##FETCH_STATUS = 0
BEGIN
EXEC aspnet_delete_user #yourPrimaryKey
FETCH NEXT FROM delete_cursor INTO #yourPrimaryKey
END
CLOSE delete_cursor
DEALLOCATE delete_cursor
GO
See more information here and here

Related

Updating several tables column value at once in MYSQL

I have within a database several tables where they all have username column. I would like to update one username and naturally I should update it in all tables.
I have this working solution:
UPDATE `user`,
`user_images`,
`user_comments`
SET `user`.`username` = 'new_name',
`user_images`.`username` = 'new_name',
`user_comments`.`username` = 'new_name'
WHERE `user`.`username` = 'old_name'
AND `user_images`.`username` = 'old_name'
AND `user_comments`.`username` = 'old_name'
I am hoping for a better query that can do the same action, as if table numbers got increased, do I really need to do this in 100 lines?
It sounds painful if you have to update each table. I would suggest using a stored procedure to finish the tedious job. Fisrt of all , make a table(named tablelist) which list all the tablename you would like to update. Then call the procedure by providing the two parameters where the o_name is the name you would like to change and the n_name is the new name to be changed into.
delimiter //
drop procedure if exists update_name//
create procedure update_name (o_name varchar(30),n_name varchar(30))
begin
declare t_name varchar(30);
declare done bool default false;
declare csr cursor for select tablename from tablelist;
declare continue handler for not found set done=true;
open csr;
lp: loop
fetch csr into t_name;
if done=true then
leave lp;
end if;
set #prep=concat('update ',t_name,' set `username`= "',n_name,'" where `username`= "',o_name,'";');
prepare prep_stat from #prep;
execute prep_stat;
deallocate prepare prep_stat;
end loop lp;
close csr;
end//
delimiter ;
The following call will change the name from john(case insensitive) to Xero in all tables listed in the tablelist table.
call update_name('John','Xero');

Cursor Fetch Returns Null

The problem is quite clear but I have tried many things to fix it, including using different variable names than table fields. The fetched value from cursor always returns null. The value that is assigned to the cursor fetch is the same data type (int(11)). What I am doing is to assign fetched key_id value from cursor's select table into #my_key_id int(11) variable but it keeps coming as null.
declare my_key_id int(11); /*variable that will be assigned from the value in cursor*/
DECLARE done INT DEFAULT FALSE; /*for cursor break*/
DECLARE cr_cursor cursor for select key_id from tmp_valuesss; /*cursor declaration*/
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; /*break thingy*/
open cr_cursor;
read_loop: LOOP
IF done THEN
LEAVE read_loop;
END IF;
select #my_key_id;
FETCH cr_cursor INTO my_key_id;
END LOOP;
close cr_cursor;
You are mistaking User Variables (the ones with an # sign) with Local Variables (the ones with DECLARE).
Your User Variable was never set and thus always null.
Also, DEALLOCATE any PREPARE var.
The Fetch was moved up to occur at the beginning of LOOP.
drop procedure if exists calculate_thingy;
delimiter $$
CREATE PROCEDURE calculate_thingy
(
IN table_name VARCHAR(100)
)
BEGIN
DECLARE SQL_STATEMENT NVARCHAR(8000);
drop table if exists tmp_valuesss;
SET #SQL_STATEMENT = CONCAT('CREATE TABLE IF NOT EXISTS tmp_valuesss AS (SELECT * FROM ', table_name, ')');
PREPARE STMT FROM #SQL_STATEMENT;
EXECUTE STMT;
DEALLOCATE PREPARE STMT; -- Drew added -------------------
alter table tmp_valuesss add the_field float;
begin
declare my_key_id int(11); /*variable that will be assigned from the value in cursor*/
DECLARE done INT DEFAULT FALSE; /*for cursor break*/
DECLARE cr_cursor cursor for select key_id from tmp_valuesss; /*cursor declaration*/
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; /*break thingy*/
open cr_cursor;
read_loop: LOOP
FETCH cr_cursor INTO my_key_id;
IF done THEN
LEAVE read_loop;
END IF;
select my_key_id;
END LOOP;
close cr_cursor;
end;
END $$
DELIMITER ;
A few things to note. First, the reason why the whole block is presented above is to aid the future visitor here that may be having a problem with creating the stored proc due to the DELIMITER issue many struggle with. Also, the drop at the beginning is important for edits to the proc the 2nd time and thereafter.
Also, this is clearly just a testing a concept stored proc that you presented. Meaning, by doing a select my_key_id; in the LOOP, you are effectively creating an additional resultset on the consumer side (that which called the stored proc with the call statement). Whether or not you are equipped to handle multiple result sets coming back in your code will impact your review of this.
Also, it is driven by the state of the table name you pass as a parameter. So if that table contains values or nulls, you get what you get based on your code and your decision to pass that table name. A table that allegedly contains the column key_id because that is the way you wrote the stored procedure to begin with.
A view below is of it working, with multiple result sets.
That above image shows a table that had 2 rows in it for fish and frog. Two resultsets were returned (that is how you coded it). I Highlighted the second result set, and it showed the value coming back from it (which was id 2) ... the last row that occurred before the LOOP was finished.
Instead of attempting to edit this answer for a third time, I recommend you join me in chat in the link I gave in a comment under your question.

mySQL drop tables with Wildcard using only SQL statement?

Seen a lot for dropping tables using a wildcard but not a direct SQL statement except this one:
http://azimyasin.wordpress.com/2007/08/11/mysql-dropping-multiple-tables/
It says:
SHOW TABLES LIKE ‘phpbb_%’;
then DROP TABLES, is there a neat way to combine this all into one SQL Statement?
You could use dynamic SQL to do it, inside a stored procedure. It'd look something like this (untested):
CREATE PROCEDURE drop_like (IN pattern VARCHAR(64))
BEGIN
DECLARE q tinytext;
DECLARE done INT DEFAULT FALSE;
DECLARE cur CURSOR FOR
SELECT CONCAT('DROP TABLE "', table_schema, '"."', table_name, '"')
FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_name LIKE pattern;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
drop_loop: LOOP
FETCH cur INTO q;
IF done THEN
LEAVE drop_loop;
END IF;
PREPARE stmt FROM #q;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP;
CLOSE cur;
END;
Using dynamic SQL in a query, as per derobert's answer, is the only to do this with pure SQL (no app code).
I wrote a generalized procedure to do this sort of thing (run a query for every table in a database) that you can find here - to use it, you would just need to run this query:
CALL p_run_for_each_table('databasename', 'DROP TABLE `{?database}`.`{?table}`');
It works in essentially the same way as derobert's answer.
However, the writer of that blog post was probably expecting you to write app code to turn the names of tables into a single DROP statement.
To do this, you would iterate over the results of the SHOW TABLE in your code and build a single query like this:
DROP TABLE table1, table2, tablewhatever;
This can be achieved via stored procedure, for example:
CREATE DEFINER=`some_user`#`%` PROCEDURE `drop_tables`()
LANGUAGE SQL
NOT DETERMINISTIC
MODIFIES SQL DATA
SQL SECURITY DEFINER
COMMENT ''
BEGIN
#We need to declare a variable with default 0 to determine weather to continue the loop or exit the loop.
DECLARE done INT DEFAULT 0;
DECLARE archive_table_name VARCHAR(100);
#Select desired tables from `information_schema`
DECLARE cur CURSOR FOR
SELECT t.`TABLE_NAME` FROM information_schema.`TABLES` t WHERE t.`TABLE_NAME` LIKE 'some_table_name%'
AND t.CREATE_TIME BETWEEN DATE_SUB(NOW(), INTERVAL 9 MONTH) AND DATE_SUB(NOW(), INTERVAL 6 MONTH);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur;
read_loop: LOOP
#Fetch one record from CURSOR and set variable (if not found, then variable `done` will be set to 1 by continue handler)
FETCH cur INTO archive_table_name;
IF done THEN
LEAVE read_loop; #If done is set to 1, then exit the loop, else continue
END IF;
#Do your work
-- Create the truncate query
SET #s = CONCAT('DROP TABLE IF EXISTS ', archive_table_name);
-- Prepare, execute and deallocate the truncate query
PREPARE drop_statement FROM #s;
EXECUTE drop_statement;
DEALLOCATE PREPARE drop_statement;
END LOOP;
CLOSE cur; #Closing the cursor
END
Pay attention to the database user, which is creating/executing the stored routine: it must have appropriate credentials for executing/dropping tables.

Dynamic cursor in stored procedure

I would like to use LIMIT in a cursor. The cursor should be used and updated several times within a loop, each time with different parameters of LIMIT. Here some code:
DELIMITER $$
CREATE PROCEDURE `updateIt`() READS SQL DATA
BEGIN
declare done int(1) default 0;
declare counter int(10) default 0;
declare xabc int(10) default 0;
declare tab1Cursor cursor for select abc from tab1 limit 100000*counter, 100000;
declare continue handler for not found set done=1;
loopCounter: LOOP
set done = 0;
open tab1Cursor;
igmLoop: loop
fetch tab1Cursor into xabc;
if done = 1 then leave igmLoop; end if;
-- do something
end loop igmLoop;
close tab1Cursor;
if (counter = 1039)
leave loopCounter;
end if;
set counter = counter + 1;
END LOOP loopCounter;
END $$
DELIMITER ;
This, however, does not work (I also tried it with the cursor in the LOOP counterLoop). Can Mysql deal with dynamic cursors?
From the MySQL Manual
a cursor cannot be used for a dynamic statement that is prepared and
executed with PREPARE and EXECUTE. The statement for a cursor is
checked at cursor creation time, so the statement cannot be dynamic.
However there are 2 ways, according to this post in mysql forums:
The first is for cases where absolutely only one user at a time will be running the procedure. A prepare statement can be used to create a view with the dynamic SQL and the cursor can select from this statically-named view. There's almost no performance impact. Unfortunately, these views are also visible to other users (there's no such thing as a temporary view), so this won't work for multiple users.
Analogously, a temporary table can be created in the prepare statement and the cursor can select from the temporary table. Only the current session can see a temporary table, so the multiple user issue is resolved. But this solution can have significant performance impact since a temp table has to be created each time the proc runs.
Bottom line: We still need cursors to be able to be created dynamically!
Here's an example of using a view to pass the table name and column name into a cursor.
DELIMITER //
DROP PROCEDURE IF EXISTS test_prepare//
CREATE PROCEDURE test_prepare(IN tablename varchar(255), columnname varchar(50))
BEGIN
DECLARE cursor_end CONDITION FOR SQLSTATE '02000';
DECLARE v_column_val VARCHAR(50);
DECLARE done INT DEFAULT 0;
DECLARE cur_table CURSOR FOR SELECT * FROM test_prepare_vw;
DECLARE CONTINUE HANDLER FOR cursor_end SET done = 1;
SET #query = CONCAT('CREATE VIEW test_prepare_vw as select ', columnname, ' from ', tablename);
select #query;
PREPARE stmt from #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
OPEN cur_table;
FETCH cur_table INTO v_column_val;
WHILE done = 0 DO
SELECT v_column_val;
FETCH cur_table INTO v_column_val;
END WHILE;
CLOSE cur_table;
DROP VIEW test_prepare_vw;
END;
//
DELIMITER ;

Need an abstract trigger in MySQL 5.1 to update an audit log

I need a way to check for and pass entries into an audit log for any entries in a table that have been changed. It needs to be abstracted away from the table structure.
For example:
CREATE TRIGGER table1_update
BEFORE UPDATE ON table1
FOR EACH ROW BEGIN
DECLARE i_column_name varchar(32);
DECLARE done INT;
DECLARE cursor1 CURSOR FOR SELECT column_name FROM information_schema.columns WHERE table_name = 'table1';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cursor1;
REPEAT
FETCH cursor1 INTO i_column_name;
IF NOT done THEN
--pass the variable column_name and its old.i_column_name and new.i_column_name values to the audit table
END IF;
UNTIL done END REPEAT;
CLOSE cursor1;
END$$
We have too many tables that need to be audited to custom build every single INSERT, UPDATE, and DELETE trigger. I've tried a number of things and I'm thinking I'm out of luck. Anyone have any ideas?
You can't have an abstract trigger, it must be defined on a specific table. The closest you can get is to put the code for the trigger into a stored procedure, and then the triggers for each table will just call the procedure.
CREATE PROCEDURE audit_update (IN tablename VARCHAR(64))
BEGIN
DECLARE i_column_name varchar(32);
DECLARE done INT;
DECLARE cursor1 CURSOR FOR SELECT column_name FROM information_schema.columns WHERE table_name = tablename;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cursor1;
REPEAT
FETCH cursor1 INTO i_column_name;
IF NOT done THEN
--pass the variable column_name and its old.i_column_name and new.i_column_name values to the audit table
END IF;
UNTIL done END REPEAT;
CLOSE cursor1;
END
CREATE TRIGGER table1_update
BEFORE UPDATE ON table1
FOR EACH ROW BEGIN
CALL audit_update('table1');
END
You should be able to easily script up something that will create the triggers for all of your tables using the information_schema or something along those lines.
SELECT CONCAT('CREATE TRIGGER ', table_name, '_update BEFORE UPDATE ON ', table_name, ...) FROM information_schema...