How to find total number of rows inserted in while loop in an transaction.? - sql-server-2008

Hi i need to find total number of rows inserted in a transaction with while loop for log table.I have tried using ##rowcount but its showing as null in the output. Could anyone say if there is a way to find ?
Sp_example
As
Begin
Declare #position int
Begin try
Begin transaction
Set #position =1
While #position <100
Begin
Select * into temp from example
Set #position = #position+1
Commit transaction
Insert into log_table(rowcount)
Select ##rowcount
End
End try
Begin catch
Example...
End catch

Related

How can I prevent insertion into a table through trigger?

I've to prevent the insertion into table when the number of rows having colour as Red exceeds 100. Here's the code that i've written, just dont know what to write in the blank.
CREATE TRIGGER onRegisterInsert BEFORE INSERT ON table_name FOR EACH ROW
BEGIN
DECLARE n INT DEFAULT 0;
IF New.Colour='Red' THEN
SELECT COUNT(*) INTO n FROM table_name WHERE Colour='Red';
if n > 100 THEN
---
END IF
END IF
END
I thought of writing SIGNAL SQLSTATE '45000' to prevent insertion but i dont want to throw an error, because i'm inserting a list of data and if i throw an error the insertion of data after this will also be stopped.
I also thought of writing an after insert trigger instead of before.
DELIMITER //
CREATE TRIGGER onRegisterInsert After INSERT ON table_name FOR EACH ROW
BEGIN
IF New.Colour='Red' THEN
SELECT COUNT(*) INTO #count FROM table_name WHERE Colour='Red';
IF (#count >= 100) THEN
delete from table_name new;
END IF;
END IF;
END //
but this code too gives an error.
One thing you could do is use INSERT IGNORE in the calling app instead of INSERT.

trigger to update values of another table according to input of first table

Create or replace Trigger t1 AFTER INSERT ON feedback
FOR EACH ROW
BEGIN
IF new.rating = 1 THEN update count SET count=count+1 WHERE scale="one";
ELSE IF new.rating = 2 THEN UPDATE count SET count=count+1 WHERE scale="two";
ELSE IF new.rating = 3 THEN UPDATE count SET count=count+1 WHERE scale="three";
ELSE IF new.rating = 4 THEN UPDATE count SET count=count+1 WHERE scale="four";
ELSE IF new.rating = 5 THEN UPDATE count SET count=count+1 WHERE scale="five";
END IF;
END
//
table feedback
table count
I want to write trigger which update value of count in table count according to new insert values in feedback.
e.g. if I insert value of rating as 3 in 'feedback' table then it will automatically update value of count of 'three' in 'count table' by 1.And suppose if I insert value of rating as 5 in 'feedback' table then it will automatically update value of count of 'five' in 'count table' by 1.
syntax error in above code
This
Create Trigger <trigger_name> AFTER INSERT OR UPDATE ON <FIRST_TABLE_NAME>
FOR EACH ROW
BEGIN
//update statement
END;
//
Source : https://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html
Here trigger is written on feedback table and inside trigger value of counter updating which is in another count table so that is not possible. when you write trigger you should update values of columns present in table on which trigger is written only. so here first we should write procedure and call it from trigger.
procedure:
create procedure c(cnt int(10))
begin
DECLARE rating_temp int (20);
DECLARE cur CURSOR FOR SELECT cnt;
OPEN cur;
FETCH cur INTO rating_temp;
IF rating_temp=5 then
update count1 SET counter=(counter+1) where scale="five";
ELSEIF rating_temp=4 then
update count1 SET counter=(counter+1) where scale="four";
ELSEIF rating_temp=3 then
update count1 SET counter=(counter+1) where scale="three";
ELSEIF rating_temp=2 then
update count1 SET counter=(counter+1) where scale="two";
ELSEIF rating_temp=1 then
update count1 SET counter=(counter+1) where scale="one";
END IF;
CLOSE cur;
END$$
2) trigger-
CREATE TRIGGER t1
AFTER INSERT
ON feedback
FOR EACH ROW
BEGIN
Declare cnt2 int(10);
select new.rating into cnt2;
call c(cnt2);
END$$

MySQL query results are incorrect

im trying to update a table using this query:
DELIMITER $$
CREATE PROCEDURE updateDataSetHasChildren()
BEGIN
DECLARE data_set_id INT;
DECLARE done INT DEFAULT FALSE;
DECLARE result INT DEFAULT FALSE;
DECLARE data_set_cursor CURSOR FOR
SELECT id_data_set FROM data_set;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN data_set_cursor;
myloop: LOOP
-- Perform the first fetch.
FETCH data_set_cursor into data_set_id;
IF done THEN
LEAVE myloop;
END IF;
IF (SELECT COUNT(*) FROM data_sub_set WHERE id_data_set = data_set_id)>0 THEN
UPDATE data_set
SET has_children = TRUE
WHERE id_data_set = data_set_id;
ELSE
UPDATE data_set
SET has_children = FALSE
WHERE id_data_set = data_set_id;
END IF;
FETCH data_set_cursor into data_set_id;
END LOOP myloop;
CLOSE data_set_cursor;
END$$
DELIMITER ;
The problem is that when i run it it fills the column of the table with 1 0 1 0 1 0...and when i check the values are incorrect, what am i missing here?
Regards,
Remove second
FETCH data_set_cursor into data_set_id;
at the end of the myloop. Now it is working by steps:
Fetch id.
Process row.
Fetch id (loop end).
Fetch id (second loop iteration started).
Process row.
Fetch id.
Fetch id and so on...
Fetch is performed twice, and you are really processing every second row.

Stored procedure go to infinite loop

I am making a stored procedure in order to check that whether a user exists or not. The problem is whenever I pass it the correct username, it executes successfully but when I pass wrong username, it go through an infinite loop.
Where am I going wrong? Here is my Stored Procedure:
CREATE PROCEDURE `VerifyUserNPass`(userParam varchar(50), out result int)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE tempUser varchar(50) default '';
DECLARE count int default 0;
DECLARE noRows int;
DECLARE userList cursor for select userName from users;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
select count(*) into noRows from users;
OPEN userList;
read_loop: LOOP
FETCH userList into tempUser;
IF tempUser = userParam THEN
SET #count = count + 1;
LEAVE read_loop;
ELSEIF count > noRows THEN
LEAVE read_loop;
END IF;
END LOOP;
CLOSE userList;
select count into result;
END
To fix your code, use a conditional test on the done variable to determine whether to leave the loop. (The done variable is initialized to FALSE, and then set to TRUE in the CONTINUE HANDLER when there are no more rows.)
Get rid of the query "count(*)" query, and the noRows variable; those are not needed. (In a concurrent system, it's possible for that count(*) query to return a value that differs from the number of rows returned by a later query. (Consider that its possible for other sessions to insert or delete rows while your procedure is running.)
Also get rid of the references to the #count user variable. You have a mixture of references, to both a user variable #coount and a procedure variable count. These are independent variables. There's no need for your procedure to use a user variable. Instead, stick with using variables that are declared in your procedure. (Save the user variables for when you have a need.)
-- select count(*) into noRows from users;
read_loop: LOOP
FETCH userList into tempUser;
IF done THEN
LEAVE read_loop;
END IF;
IF tempUser = userParam THEN
SET count = 1;
LEAVE read_loop;
END IF;
END LOOP;
A more efficient way to code this would be to let the database find the row(s) you are interested in, by adding a WHERE clause on your query. (There's no need for you to fetch rows that aren't going to match the condition you are interested in.)
Modify your cursor definition to include a predicate (i.e. a condition in a WHERE clause) to limit the rows returned:
DECLARE userList cursor for select userName from users
WHERE userName = userParam LIMIT 1;
I don't understand the need for a procedure here. A native SQL statement would be much more efficient, e.g.
SELECT 1 AS found FROM users u WHERE u.userName = 'foo' LIMIT 1;
Make sure you increment your loop counter for all branches not only in the succes branch and have result variable to hold your outcome.
DECLARE userFound int default 0;
......
read_loop: LOOP
FETCH userList into tempUser;
SET #count = #count + 1;
IF tempUser = userParam THEN
SET #userFound = 1
LEAVE read_loop;
ELSEIF count > noRows THEN
LEAVE read_loop;
END IF;
.....
select userFound into result;
And I would expect that this would return the same result (but I'm not an MySql expert)
CREATE PROCEDURE `VerifyUserNPass`(userParam varchar(50), out result int)
BEGIN
select COUNT(userName) into result from users where username = #userParam
END

Execute query based on number of rows?

I'm not too great with MySQL, but I'm trying to create a trigger so that whenever a row is inserted, if the total count of rows in the table is bigger than 10 a certain row is deleted.
What I'm looking for, without the trigger syntax, is something like this:
IF (SELECT COUNT(*) FROM table) > 10 THEN
//do some stuff
END IF;
However that does not seem to be acceptable syntax. How should I go about doing this?
DELIMITER &&
CREATE TRIGGER trigger_name AFTER INSERT ON table_name FOR EACH ROW
BEGIN
DECLARE x INT;
SET x = (SELECT count(*) FROM table_name);
IF x > 10 THEN
DELETE FROM table_name where condition;
END IF;
END&&
DELIMITER ;
select count(1) into #cnt from table;
if (#cnt > 10) then
// do some stuff
end if;
instead of #cnt you can used declared variable