Create new table procedure - sql-server-2008

I'm trying to create a procedure which adds / removes a table to the database. I'm working in SQL Server. The query works (it succeeds) but the table isn't added to the database.
I did refreshed...
ALTER procedure [dbo].[upgrade_1]
as
begin
create table awards (
ID int NOT NULL IDENTITY,
name nvarchar(256) DEFAULT 'award',
description nvarchar(256)
PRIMARY KEY (ID)
)
/*update goto_vs
set current_version = 1*/
end

The script you have in the question will only modify the PROCEDURE. The procedure needs to be executed for it to perform the required task e.g. create the table
Execute the procedure with this statement
EXEC upgrade_1
That should create the table

Related

Create a temporary table inside procedure and delete from event

I have a procedure like this :
create
procedure new_generation(IN id bigint unsigned)
BEGIN
create temporary table if not exists tmp_mine
(
id bigint unsigned primary key not null auto_increment,
created_at timestamp,
);
end
now I want to create an event for delete old rows.
create event per_ten_second on schedule
every '10' SECOND
starts '2021-10-26 11:49:16'
enable
do
delete from tmp_mine where created_at < now();
but it doesn't work. Can anybody give me a clue? Can we use generated temp table inside stored procedure out of it? for example inside an event.

MYSQL - Trigger -update /deletion

I have to create a trigger a which captures old and new value while updation & deletion taken place
Audit table :
create table if not EXISTS tbl_audits
(
TA_Auditid int AUTO_INCREMENT PRIMARY key,
TA_Name varchar(100),
TA_Oldvalue varchar(1000),
TA_Newvalue varchar(1000),
TA_Actiontaken varchar(100),
TA_createddt datetime,
TA_createdby varchar(100)
)
DELIMITER //
create TRIGGER TRfiledetails_update
BEFORE update on tbl_file_details
for EACH ROW
BEGIN
insert into tbl_audits
set TA_createddt =CURRENT_TIMESTAMP,
TA_Actiontaken ='update',
TA_Name ='File',
TA_Oldvalue =old.FL_dtstartdt ,
TA_Newvalue =new.FL_dtstartdt
end //
DELIMITER ;
In my main table i have to create trigger an event on two columns startdt and enddate.
In this case I would like to know whether I need to create two triggers separately for each column.
Is it possible to create two columns action on same trigger or need to create separate columns in audit table.

How can I update MySQL table row's when 'BEFORE UPDATE' trigger is firing?

I created a table
Databases Name - mytrigger;
Table name - employee_audit
use mytrigger;
create table employee_audit(
id int auto_increment primary key,
employeeNumber int not null,
lastName varchar(50) not null,
changee datetime default null,
action varchar(50) default null
);
After that, I created one update trigger
My trigger name is
before_employees_update
DELIMITER $$
create trigger before_employees_update
before update on employee_audit
for each row
begin
insert into employee_audit
set action ='update',
employeeNumber = OLD.employeeNumber,
lastName = OLD.lastName,
changee = now();
end$$
DELIMITER ;
After that, I inserted values in table using this command ->
insert into employee_audit values(1,112,'prakash','2015-11-12 15:36:20' ,' ');
After that, I want to update my table row where id =1
update employee_audit set lastName = 'Sharma' where employeeNumber =112;
But it is not executed give an error
ERROR 1442 (HY000): Can't update table 'employee_audit' in stored
function/trigger because it is already used by statement which invoked
this stored function/trigger.
When I searched on Google I found a lot of Question with the same error. But not able to fix my problem. what is the reason I'm not able to update my row?
What i suggest,you can create one log table like employee_audit_LOG .
And on every insert or update in main table you can make new entry in this table or update existing record.
Also you can add updated_timestamp column to that LOG table which maintain when did specific record get updated.
The error itself tells you the answer. This is because, you can't use the same table on which trigger is being executed. You need to store your audit logs into some different table.

MySQL Stored procedure for updating two tables (2nd with auto increment value from 1st)

I want to update two tables at the same time in my database. One table is for groups, and the other table is for members of groups:
CREATE TABLE IF NOT EXISTS groups (
group_id INTEGER UNSIGNED AUTO_INCREMENT,
group_name VARCHAR(150) NOT NULL DEFAULT '',
group_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (group_id)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8;
CREATE TABLE IF NOT EXISTS group_members (
group_mem_user_id INTEGER UNSIGNED NOT NULL,
group_mem_group_id INTEGER UNSIGNED NOT NULL,
group_mem_role TINYINT DEFAULT 1,
group_mem_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT group_mem_pk PRIMARY KEY (group_mem_user_id, group_mem_group_id),
FOREIGN KEY (group_mem_user_id) REFERENCES user (user_id),
FOREIGN KEY (group_mem_group_id) REFERENCES groups (group_id)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8;
I want to use a stored procedure to create an entry in group and create an entry in group_members with the id that was just created for the group.
I know how to do this on the server (I have a java server and I'm using Spring's JdbcTemplate to make calls to the database) but I thought it would be better and more efficient to do this in a stored procedure.
The two individual queries are (im using prepared statements):
INSERT INTO groups (group_name) VALUES (?)
and
INSERT INTO group_members (group_mem_user_id, group_mem_group_id, group_mem_role) VALUES (?,?,?)
But I'm not sure how to merge these into one stored procedure.
DELIMITER //
DROP PROCEDURE IF EXISTS create_group //
CREATE PROCEDURE create_group(
#in/out here
)
BEGIN
#no idea
END //
DELIMITER ;
Ideally I would like it to return some value describing whether the operation was sucessful or not.
I use the following procedure:
DELIMITER //
DROP PROCEDURE IF EXISTS create_group //
CREATE PROCEDURE create_group(
IN create_group_group_name VARCHAR(150),
IN create_group_user_id INT
)
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION, SQLWARNING
BEGIN
ROLLBACK;
END;
START TRANSACTION;
INSERT INTO groups (group_name) VALUES (create_group_group_name);
INSERT INTO group_members (group_mem_user_id, group_mem_group_id, group_mem_role) VALUES (create_group_user_id, LAST_INSERT_ID(), 2);
COMMIT;
END //
DELIMITER ;
In my server I use it like:
JdbcTemplate jt = new JdbcTemplate(DB.getDataSource(DB_USER));
int i = jt.update("CALL create_group (?,?)", new Object[] {groupName, userId});
if (i != 1)
throw new SQLException("Error creating group with name=" + groupName + " for userid=" + userId);
i == 1 if everything went well. Groups will never be created if the user is not added as a member (fixing the problem with my first iteration below).
OLD
(non-transactional, causes a problem if the second insert fails then the group is created with no members, it might work in some cases which is why I leave it here but it doesn't work for me)
The following procedure works. It does not return anything and I am just using the fact that the procedure completes without error to assume that it was all ok.
DELIMITER //
DROP PROCEDURE IF EXISTS create_group //
CREATE PROCEDURE create_group(
IN create_group_group_name VARCHAR(150),
IN create_group_user_id INT
)
BEGIN
INSERT INTO groups (group_name) VALUES (create_group_group_name);
INSERT INTO group_members (group_mem_user_id, group_mem_group_id, group_mem_role) VALUES (create_group_user_id, LAST_INSERT_ID(), 2);
END //
DELIMITER ;

Creating a table in mysql stored procedures

I'm trying to create a table inside a mysql stored procedure, but whenever I execute this procedure, I do not see the resulting table in my database. Can anyone tell me what's going on? The create table query works whenever I enter it in the cmd outside of a stored procedure.
delimiter //
drop procedure if exists gm //
create procedure gm()
begin
create table errorMessages (
error_id int not null auto_increment,
message varchar(200) not null default '',
primary key(error_id)
);
end //
delimiter ;
Are you executing the stored procedure or just creating it?
Try
call gm();
It gives any error?