let me first explain a little abot my setup before letting you know the issue Im dealing with:
I have two database servers: SQL 2008 and MySql. I have a procedure in SQL that first, deletes a table in MySql and then populates it with some data from the SQL itself. For both actions im using OPENQUERY. But, as you know, it doesnt know support large data. It becomes really slow eventhou for truncating the table in MySql (it was taking like 14 minutes to delete 60k rows... insane!).
So i found out i could use this syntax for truncating:
EXEC ('TRUNCATE TABLE table_name') AT LINKEDSERVER
But i havent been able to find the syntax for inserting the results of the SQL table to my MySql table thru this. It works correct when I insert static data, like this example:
EXEC ('INSERT INTO table_name (row1, row2) VALUES (value1, value2)') AT LINKEDSERVER
But I want to insert the results from a SQL, as I said, something like this:
EXEC ('INSERT INTO table_name (row1, row2) SELECT r1, r2 FROM SQL_DB.sql_table_name') AT LINKEDSERVER
I bet that i cannot do it because in that syntax, Im executing the exec in the MySql server and the SQL table doesnt exist there. So how can i referrence my SQL table inside the "EXEC() AT" syntax?
Thank you.
you have to use "OPENQUERY" to do this - see
https://learn.microsoft.com/de-de/sql/t-sql/functions/openquery-transact-sql?view=sql-server-ver15
You can also use this:
INSERT INTO [LinkedServer].Database.Schema.Table(Attributes)
From
Select (attributes) from Database.Schema.Table as TableAlias
Related
I have about 50 WordPress databases on a server. I need to quickly pull all values for admin_email from the wp_options table across each database.
What I'm hoping for is something like:
-- psuedocode, not sure how to correctly format something like this
foreach database in database_names
SELECT `option_value` FROM `wp_options`
WHERE `option_name`='admin_email';
I'm not sure how to go about this. I saw other questions recommending using the information_schema table, but all of my schemata.schema_name values are in the format of 'db_structure.php?db=db_name&token=some_token' --- I'm not sure if this is expected, and if so, how one would go about looping through each database name.
I'm using phpMyAdmin as my DB administration tool.
you can occupy a query in sql that returns you all list with UNION
SELECT email FROM dbo.tbname1
UNION
SELECT email FROM dbo.tbname2
UNION
SELECT email FROM dbo.tbname3
I work on a SQL Server 2016. In this Server has a linkedserver connection to MySQL. Now I want to insert from a local mssql table to a MySQL table.
My code is:
INSERT OPENQUERY (
MYSQL_BEWERTUNG
,'SELECT PERSONALNR, EINSATZSART, KUNDENNR FROM tb_bewertung'
)
SELECT b.PERSONALNR
,b.DATUMVON
,b.KUNDENNR
FROM ext_bewertungen b
After that execution, I get an message that 136 rows are effected.
If I look into the MySQL table, I can't find the new rows. But if I look from the MS SQL server with
SELECT PERSONALNR
,EINSATZSART
,KUNDENNR
FROM OPENQUERY(MYSQL_BEWERTUNG, 'SELECT PERSONALNR, EINSATZSART, KUNDENNR
FROM tb_bewertung')
ORDER BY 1;
the new rows were shown.
Could someone explain me what am I doing wrong?
Thanks a lot!
Solved problem, the t-sql is right, my IT change the webserver and change in my holidays my linkedserver also, thats why i did not see any changes... thanks #MandyShaw for the idea if I am on the wrong server =)
I have a table which get new data inserted every minute or so. According to the source code I have here, it is only done in one class which is not used anymore.
Is there any way to trace the inserts? What I mean is to see which queries they were inserted by, who sent those queries etc. As much info as possible.
I have tried several ways myself (e.g.sp_who2 'Active' stored procedure) without any success. I also have access to the machine running the SQL server and to the transaction backup files (.trn files) but have no idea how to open those files.
Add trigger to the table which follows inserts and insert to other table these variables:
getdate(),
host_name(),
App_Name(),
suser_sname()
Seems to me that this is enough
The trigger looks like this:
CREATE TRIGGER YourTrigger On YourTable
AFTER INSERT
AS
SET NOCOUNT ON;
INSERT logtable
SELECT APP_NAME(), HOST_NAME(), SUSER_SNAME(), GETDATE(), * FROM INSERTED
GO
OR
you can use Sql Server Profiler for catching the queries - it may be more flexible
You may use sp_depends like this:
sp_depends tablename
This only states information in the same database but it might say what you need!
I am converting mysql sprocs to SQL Server. I've come across a select statement in mysql that I don't quite understand what it's doing and my google-fu/so-fu has failed me. Here is the gist of it:
SELECT AccountType = dbo.functionToGetAccountType() FROM AccountLookup
I don't have the ability to debug the original mysql. I do know that the function only returns a single value.
Is the mysql statement assigning a default value to 'AccountType' in the event there are no rows in the AccountLookup table?
Thanks for your time.
The select statement is executing the function dbo.functionToGetAccountType() and aliasing the column as AccountType. It could be re-written as:
SELECT dbo.functionToGetAccountType() as AccountType
FROM AccountLookup
After renaming one of my MySQL 5.1 MyISAM tables from test_tablename to tablename, I have found that if I try to execute an INSERT (or REPLACE) query, I get the following message:
INSERT INTO tablename (...) VALUES (...)
1146: Table 'dbname.test_tablename' doesn't exist
I have triple-checked my database abstraction code, and verified this by running the query directly on the server.
According to the MySQL server, the CREATE TABLE syntax is tablename, as expected, and when I run SHOW TABLES, it lists tablename as expected.
Is there any reason for this to happen?
More importantly, is there an easier way to fix this than dumping, dropping, re-creating, and reloading the table?
This is likely to be caused by a trigger that has not been updated accordingly.
If you renamed test_tablename to tablename, shouldn't the following be true ?
1146: Table 'dbname.test_tablename' doesn't exist
Be sure to use tablename in your queries, not test_tablename.
Are you sure you are not inserting into a view that still references test_tablename?