SQL Script converted to MySQL - mysql

I am trying to fix this migration history problem but can't get this to work in MySQL
SELECT *
INTO [TempMigrationHistory]
FROM [__MigrationHistory]
DROP TABLE [__MigrationHistory]
EXEC sp_rename 'TempMigrationHistory', '__MigrationHistory'
How would I get this working in mySQL ?

A close equivalent mysql SQL for the SQL Server t-sql provided is something like what follows here. (Obviously field1, field2 are placeholders.)
CREATE TABLE `TempMigrationHistory` LIKE `__MigrationHistory`;
INSERT INTO `TempMigrationHistory` (field1, field2)
SELECT field1, field2
FROM `__MigrationHistory`;
DROP TABLE `__MigrationHistory`;
RENAME TABLE `TempMigrationHistory` to `__MigrationHistory`;
Points to note...
mysql does not support SELECT INTO but it does have the CREATE TABLE LIKE statement that SQL Server does not.
Backticks (`) are used by convention in mysql instead of square brackets ([) in t-sql.
mysql often has special system commands that take the place of SQL Server system stored procedure functionality (like sp_rename with RENAME TABLE in this case).
At the end of this sequence of operations it seems you have exactly what you started with so I'm a bit confused about the desire to execute this sequence of events, but I suppose that's a tangent to your question.

Not all RDBMS's behave the same. Have you considered searching for the MySQL syntax for creating an INSERT statement? E.g.,
INSERT INTO TempMigrationHistory
(Col1,
Col2,
Col3)
SELECT Col1,
Col2,
Col3
FROM __MigrationHistory
Also, sp_rename is a SQL Server Stored Procedure. An equivalent in MySQL is RENAME. In your case, this will probably work:
RENAME TABLE TempMigrationHistory TO __MigrationHistory;

Related

SELECT * INTO statement

I was trying to use this statement SELECT * INTO new_table FROM old_table but it's giving me the error of undeclared value. I wanted to create a back table from one existing table to another new table.
Do I have first to create another table? or am I missing something.
the link below shows that the same statement can be used, so I don't why it's not working for me
https://www.w3schools.com/sql/sql_select_into.asp
You should use the INSERT ... SELECT statement for MySQL instead.
INSERT INTO new_table
SELECT *
FROM old_table
But new_table must exist. If you have MySQL version >= 8.0.19, then you can use instead the following syntax:
INSERT INTO new_table TABLE old_table;
You may check 13.2.6.1 INSERT ... SELECT Statement.
Background information
The SELECT...INTO is not exactly standard, it has selective support by vendors.
The statement does exist in MySQL, but is used for "query result to be stored in variables or written to a file".

How do I use an IF statement with an ALTER statement in MySQL?

I'm looking to add a column only if it does not already exist before. The motivation for this is that we can upgrade any version of a production instance to the latest version.
This is what I'm trying, but I keep getting a syntax error near the IF statement:
use database_name
SELECT #rowcount:=COUNT(column_name)
FROM information_schema.columns
WHERE table_name = 'table_name'
AND column_name = 'column_2';
IF #rowcount < 1 THEN
ALTER TABLE table_name
ADD COLUMN column_2 VARCHAR(42) DEFAULT 'abcd',
END IF;
commit;
What am I doing wrong?
You asked:
How do I use an IF statement with an ALTER statement in MySQL?
You Can't Do Thatâ„¢. MySQL's DML and DDL aren't as well integrated as other makes and models of table server.
You can't put DML inside stuff like IF statements or transactions. If you need to do that sort of operation you'll have to use an application programming language to issue simpler SQL.
I think you should change
IF #rowcount < 0 THEN
ALTER TABLE table_name
ADD COLUMN column_2 VARCHAR(42) DEFAULT 'abcd',
END IF;
and check if table exist.
As #Ollie Jones mentions and from looking at Using an IF Statement in a MySQL SELECT query, it looks like it's not possible to use if statements outside of sprocs or functions.
I followed the steps on http://www.cryer.co.uk/brian/mysql/howto_add_column_unless_exists.htm:
Create the sproc (see link)
Call the sproc when wanting to add a new column to a table
Alternatively, as #Ollie Jones mentions, you can do it at an application level, but I'm using .sql files in my case. It also saves on making multiple calls to the DB vs. having it done at the server level.

SQL insert query inside select or where clause

Maybe my question above may be could be stupid , but I just want to know if is it possible to have insert query inside select or where.
The reason that I want to know that is if someone hack website or any application database, can the hacker input data to hacked database without my knowledge ?
the following example of SQL injection I see in other sites
http://www.example.com/empsummary.php?id=1 AND 1=-1 union select 1,group_concat(name,0x3a,email,0x3a,phone,0x2a),3,4,5,6,7,8,9 from employee
I know what exactly that above query does, but can the hacker input (use insert query) on the database or on any table ?
Yes, it can happen, if the database interface is configured to allow multiple statements in a query.
An INSERT can't run as part of a SELECT statement. But it's possible that the exploit of a vulnerability could finish a SELECT and then execute a separate insert.
Say you have a vulnerable statement like this:
SELECT foo FROM bar WHERE fee = '$var'
Consider the SQL text when $var contains:
1'; INSERT INTO emp (id) VALUES (999); --
The SQL text could be something like this:
SELECT foo FROM bar WHERE fee = '1'; INSERT INTO emp (id) VALUES (999); --'
If multi-statement queries are enabled in the database interface library, it's conceivable that an INSERT statement could be executed.
See: https://www.owasp.org/index.php/SQL_Injection

Use of SELECT INTO

Is it necessary to define the new table definition before using SELECT INTO query in MYSQL.
I am getting problem to execute the query when I writ e like:
SELECT *
INTO newtable
FROM oldtable
WHERE 1=0;
the error showing is:
Undeclared variabie: newtable
if you have newtable
try :
INSERT INTO newtable SELECT ...
if you don't have newtable
try :
CREATE TABLE newtable AS SELECT ...
The MySQL manual search engine is terrible but googling for something like mysql 5.5 select into will normally take you to the right page:
MySQL Server doesn't support the SELECT ... INTO TABLE Sybase SQL
extension. Instead, MySQL Server supports the INSERT INTO ... SELECT
standard SQL syntax, which is basically the same thing.
If you read the documentation here it says:
With INSERT ... SELECT, you can quickly insert many rows into a table from one or many tables
So, yes, you need to create the new table first.
You can use CREATE TABLE new LIKE old to create a new, empty table, which is a copy of the original table structure.

how to row wise manipulate the data from a "show databases" query result set

Suppose I have a database data1 which gives me this:
show tables;
table1
table2
table3
Now instead of individually executing "select * from each table" i want to create a procedure which goes through each database shown in "show databases;" resultset, and then executes select * from each table of that database. I thought of using cursors which would scroll down the resultset, hold each database name in a variable and then execute select statement on each table of that database traversing in the same way. Can someone kindly help me out with how to use cursors in this case, as i am only aware of using cursors for SELECT and UPDATE statements.
btw i use MYSQL.
I'll refrain from asking why you would do this. Here is a general strategy in pseudocode;
[words in parentheses are (SQL commands and tables) which will run on your mySQL server.]
Connect to your mySQL server with your favourite tool/programming language and:
-- (USE information_schema;)
for db in (select distinct table_schema from tables;)
do:
for table in (select table_name from tables where table_schema='$db';)
do:
select field,column,attribute from $table;
done
done
Good luck!
you can get the query from infromation_Schema instead of 'show datbases'