'master..sysdatabases' SQL Server syntax equivalent in MySQL - mysql

I am converting an SQL Server stored procedure to MySQL and there is the below code line in one stored procedure:
declare c1 cursor static for
select name from master..sysdatabases
where left(name,3) in ('ADM','ENT','DEM')
order by name asc
What is the functionality of 'master..sysdatabases '?
What is the equivalent syntax for master..sysdatabases in MySQL?

what is the functionality of 'master..sysdatabases '?
SQL server divides databases into
1.User databases
2.System databases..
User database are the ones which users created,system databases(master,model,tempdb,msdb )are the one which comes with SQL installation..
In this case,user is trying to query all available database names matching them with string functions..
In MYSQL,you can use this..
select * from information_schema.SCHEMATA
http://dev.mysql.com/doc/refman/5.7/en/schemata-table.html

Related

is there a Syntax like MSSQL's "INSERT INTO table (fieldlist) OUTPUT INSERTED.id VALUES" in MySQL?

I like to get the id of the record that's been inserted by a statement as a result of that statement. I know about SELECT LAST_INSERT_ID(); but can not use it in my scenario because LAST_INSERT_ID() works on a per-connection-basis and I am currently using a convenient function in my framework to access the db - but that convenience implies that the connection only exists during execution of that statement - so I'm wondering if there is a syntax as in Microsoft SQL Server as shown in the title?
In my environment, I am building the command looks like this:
INSERT INTO Bookings (Status,YEarlyBird,YDays,YHotel,YBanquet,Name,Company,Address,Town,Region,Postcode,CountryCode,Second
Name,SecondEmail,RoomType,SpouseMealPlan,Notes,Conference_VAT,Accommodation_VAT,Conference_id,InvoiceAmount,Conferen
ceGross,Accomodation,SpouseGross,SpouseNet,TotalGross,TotalNet,ConferenceNet,Courses) VALUES (:<C1:,:<C1:,:<C6:,:<C
6:,:<C1:,:<C12:,:<C28:,:<C15:,:<C7:,:<C5:,:<C5:,:<C2:,:<C1:,:<C1:,:<C15:,:<C3:,:<C1:,:<F:,:<F:,:<I:,:<F:,:<F:,:<F:,:
<F:,:<F:,:<F:,:<F:,:<F:,:<C1:);SELECT LAST_INSERT_ID();
(the :<:-stuff are placeholders for Bind-variables)
If a ran the INSERT on its own, it works. If I append the SELECT LAST_INSERT_ID() as shown, I get:
[MySQL][ODBC 5.3(w) Driver][mysqld-5.7.18-log]You have an error in your SQL syntax; check the manual that corresponds to y
our MySQL server version for the right syntax to use near 'SELECT LAST_INSERT_ID()' at line 1
P.S: using that convient way to access the DB is perfectly ok in my use-case because the DB is written once only - exactly with the statement i am working on.

Querying SQL server DB from linked Oracle DB - Column names are too long (>30 char) - any way to select from them?

I have an oracle database(11g) that is linked to an SQL Server database(2008).
Column names in oracle are max 30 chars. Column names in SQL server are max 128.
The SQL server database has a table, employee, which has columns: 'some_really_long_column_name_here' and 'short_col_here'
When trying to read from the SQL server database from the oracle database via a database link, like this:
select * from "engagements"#myDatabaseLink;
I get an error like:
ORA-28500: connection from ORACLE to a non-Oracle system returned this message:
[Oracle][ODBC SQL Server Driver][SQL Server]Invalid column name 'some_really_long_column_name_h'.
ORA-02063: preceding 2 lines from SQL_SERVER_DATABASE_SCHEMA
28500. 00000 - "connection from ORACLE to a non-Oracle system returned this message:"
(note that the column name is truncated in the returned error message)
I can do
Select "short_col_here" from "engagements"#myDatabaseLink;
and get the correct result.
however I can't do
select 'some_really_long_column_name_here' AS "shortAlias" from "engagements"#myDatabaseLink;
as I just keep getting:
ORA-00972: identifier is too long
If I have no means to modify the SQL server schema (for example to create a view on that side that has shorter column names...) Is there any way I can query the data in the column 'some_really_long_column_name_here' via the Oracle database?
I realise this is an old thread, but I have found an answer, for future ref. We are still working with 11g, and this works in 11g too.
Use DBMS_HS_PASSTHROUGH. Search Oracle documentation for your db version, but example below. You can insert/update, but need different calls for those.
This passes the query in its entirety through to the remote db, and you pull back the column data only, no column names.
It can also be used with varchar(max) columns. (Although it might truncate the data, or even fail.) The varchar(max) columns still cause a problem in 19c, unless you are willing/able to change your default characterset (according to https://community.oracle.com/tech/developers/discussion/2178584/db-link-from-11g-to-sql-server-2008-and-varchar-max-datatype). Eg.
DECLARE
l_csr_id BINARY_INTEGER := DBMS_HS_PASSTHROUGH.OPEN_CURSOR#<dblink>();
l_col1 VARCHAR2(250);
l_col2 VARCHAR2(250);
l_col3 VARCHAR2(4000);
l_rows BINARY_INTEGER;
BEGIN
DBMS_HS_PASSTHROUGH.PARSE#<dblink>( l_csr_id,
'SELECT "TblPk", "ReallyLongColumnNameThatIsTooLongForOracle",'
||' CAST("VarcharMaxCol" AS VARCHAR(4000)) "VarcharMaxCol2"'
||' FROM "dbo"."YourTableName"'
);
LOOP
BEGIN
l_rows := DBMS_HS_PASSTHROUGH.FETCH_ROW#<dblink>( l_csr_id, FALSE );
EXIT WHEN l_rows = 0;
EXCEPTION WHEN NO_DATA_FOUND
THEN DBMS_OUTPUT.PUT_LINE('=== NDF ==='); EXIT;
END;
--DBMS_OUTPUT.PUT_LINE('rows: '||l_rows);
DBMS_HS_PASSTHROUGH.GET_VALUE#<dblink>( l_csr_id, 1, l_col1 );
DBMS_HS_PASSTHROUGH.GET_VALUE#<dblink>( l_csr_id, 2, l_col2 );
DBMS_HS_PASSTHROUGH.GET_VALUE#<dblink>( l_csr_id, 3, l_col3 );
DBMS_OUTPUT.PUT_LINE('1:'||l_col1||', 2:'||l_col2||', 3: '||l_col3);
END LOOP;
DBMS_HS_PASSTHROUGH.CLOSE_CURSOR#<dblink>(l_csr_id);
DBMS_OUTPUT.PUT_LINE('=== FIN ===');
END;

Insert into table using string in MySql

CREATE TABLE IF NOT EXISTS tmp_park_log (
uniqueid VARCHAR (20),
parked_sec INT
) ;
DELETE
FROM
tmp_park_log ;
INSERT INTO tmp_park_log (uniqueid, parked_sec)
SELECT
uniqueid,
SUM(parked_sec)
FROM
park_log
GROUP BY uniqueid ;
This is executing successfully in MySql:
But, when i put this in a string and use Prepared Statement it gives following Error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELETE FROM tmp_park_log; INSERT INTO tmp_park_log (uniqueid, parked_sec) SELEC' at line 1
SQL syntax for prepared statements does not support multi-statements (that is, multiple statements within a single string separated by “;” characters).
See here: http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html
In the first case you arent using one commando instead you use a few commandos and when you put it in a string as a prepared statement you must create one prepared statement for every single commando you want to execute.
Since you didn't attached the Java code I assume you are
trying to d prepare statement for all the text and not to prepare every and execute it.
few hints:
There is no need to prepare the CREATE TABLE statement you can just use create statement and execute it (as no bind variables are used) http://docs.oracle.com/javase/tutorial/jdbc/basics/tables.html
If your application allows it , consider the use of truncate instead of delete, it will be faster and reduce the log generation.
Don't forget to commit :).
BR

How to use MySQL user-variables with ADO.NET

MySQL SQL commands can contain user variables that start with '#'.
The MySQL ADO.NET connector also uses '#' for command parameters.
Is there a way to escape the '#' character so I can use a user-variable in an SQL statement sent to MySQL via ADO.NET?
I'm trying to do something like this:
UPDATE company
SET next_job_id = #jobid:=next_job_id+1
WHERE company_id = #companyid;
SELECT #jobid;
Where #jobid is a MySQL user variable and #companyid is a parameter.
It's a connection string option - "Allow User Variables=true"
When set to true, parameters are prefixed with '?'.
Update: I've written up more on this, including how to get this to work with ADO.NET and Subsonic. See Here.

Delete all procedures except those which contain a string in their name

I want to drop a lot of SPs from my SQL Server database. However, I want to write "Delete all procedures except those which contain a certain string in their name".
How can I do this? I am using SQL Server 2008 R2.
Thank
What about:
SELECT 'DROP PROCEDURE ' + name
FROM sysobjects WHERE xtype = 'U' AND
name LIKE 'usp_%' -- Here you'll define your criteria
After running this, you just need to execute this command output.