Using SHOW CREATE TABLE in a subquery - mysql

I want to get the script of existing table and assign to variable and save it in db.
I have used this script
SELECT `CreateTable` FROM (show create table tablename);
But it is not working
I want createtable column from the result plz review the result image:
if there is other way to get script of table also mention it.

Related

copy result of a stored procedure into an existing table using phpMyAdmin

I have the following stored procedure in phpMyAdmin:
I need to save the result into the following existing table:
but I don't know how to code this. The code should clear/update(?) the table every-time the stored procedure is executed and populate the last field "profile_completion_percentage" with the sum of the previous fields (excluded the id). Can you please help?
use
INSERT ... SELECT Statement
https://dev.mysql.com/doc/refman/8.0/en/insert-select.html
So you can add your result very simple to your table

How to use table name inside a trigger code as a variable? MySQL/MariaDB

How to reference the table triggered inside the trigger code without knowing the table name? And by that I mean that I need to get that code and paste it into 20 triggers, each for a different table. :-/
CREATE TRIGGER register_eventLog_for_table_xyz
AFTER UPDATE ON xyz
FOR EACH ROW BEGIN
CALL reg_event(#this_tableName, dataChanged);
END
Build a script to look into information_schema.TABLES to find the list of tables and create a TRIGGER for each table. It would also build the string for the tablename.

MySQL - Automate a view update

I'd like to know if it is possible to create an SQL function that will automatically update my View and add to it new tables in my DB.
My DB consist of multiple tables (same data structure) and are named as follow "MM_DD", now I would like to create a VIEW that joins all this data ( pretty simple , see query below) but I wish to automate the process so every time a new table is added the view will update.
CREATE OR REPLACE VIEW `viewTable` AS
select *,
md5(CONCAT(`columnA`,`columnB`)) AS myPK_id
from `05_06`
union all
select *,
md5(CONCAT(`columnA`,`columnB`)) AS myPK_id
from `05_08`
ect...
What I am doing at the moment is using PHP every time a table is added. It loops through the tables and create / update the view.
select * from information_schema.tables WHERE table_name LIKE '%05%'
Now that I have an array of table names -> create my Query string -> replace view...
Is this possible to do this in SQL?
If so, what is the correct approach?
Write a stored procedure to rebuild your view. You'll need to use what MySQL internally calls "prepared statements" so you can use string manipulation.
You'll still use your SELECT ... FROM information_schema.tables query to drive this stored procedure.
Once you get this working, you can embed it in a mySQL event, and arrange to run it automatically. For example, you could run it at the same time late at night.
Or, you can invoke the stored procedure immediately after you create one of these tables.

mySQL create view returns no rows, but create table works

I can't figure out the issue here and have done some testing, but I'm new to mySQL queries. Here's the issue. If I run this:
create view services_view as
SELECT `innovation_name`,`link`,`category`,`brief_description` FROM `innovation_db`
WHERE `category`= "Service"
I get a new view with all the correct columns, but the rows are empty. If I just run the query without the create view, it returns the data correctly. If I do this instead:
create table services_view
SELECT `innovation_name`,`link`,`category`,`brief_description` FROM `innovation_db`
WHERE `category`= "Service"
I get a new table with the correct columns AND the rows filled with all the correct data. Why won't the create view work if clearly the create table does?
Thanks

Assigning the variables to the Temporary table columns

I'm working on a Task where I should get the datas from all the table from the Database without knowing the table names. What I'm planning to do is to write a query to get all the table names and column names and store it in a temporary table.
Once I got the table name and column name I have to store the values to the variables like #Table_Name and #Column_Name. Then I have to call the table names within a Loop and have to write a query like this
"select * from '''+#Table_Name+''' where '''+#Column_Name+''' = 1"
Earlier I used cursor to get the values and store it in a variable and call it within the loop. But it seems that the cursor is consuming more time and memory. So I thought of changing it using temporary table.
My doubt is, is it possible to assign the Temporary table values to a variable to use it later. If possible how to do it?
Your idea may not work, I feel, since you are trying to incorporate the concept of arrays in SQL Server. SQL Server does not have arrays, but it has tables. You can obtain the data you need by creating a temporary table. Inserting into it by firing a query like
select * from sys.tables.
Then trying to do your operation by loading each row from this table. After your work is done, by all means drop the table.