I'd like to combine multiple "Collect Statistics" after insert statement when execute openquery on SqlServer to Teradata. Is there a way to do it? I am not able to find an answer online. Here is an example:
EXEC ('INSERT INTO TABLE1 SELECT * FROM TABLE2;
COLLECT STATISTICS ON TABLE1 INDEX ( IDX1,IDX2);
COLLECT STATISTICS ON TABLE1 INDEX ( IDX3);') AT [linkserver];
COLLECT STATS is a DDL-statement and must be immediately commited, there's no way to run multiple collects in a Multi Statement Request.
But you can simply collect multiple stats in a single collect, this should work:
EXEC ('INSERT INTO TABLE1 SELECT * FROM TABLE2;
COLLECT STATISTICS INDEX ( IDX1,IDX2), INDEX ( IDX3) ON TABLE1;') AT [linkserver];
Related
I have a table with many columns in SQL Server and I have move part of data into MySQL. I made a view or function on the table in SQL Server and these two databases must be synced once a day through job. Because the data of this view may change every day.
View return a table with 3 columns: (char, varchar, varchar) that none of them are unique or primary key.
My solution is:
create a job
execute view on SQL Server
return result of view
create temp table with 3 column in MySQL
move result view from SQL Server to temp table
move records from temp table to new table one by one if not exist before
delete temp table
To transfer without using the temp table, I wanted to use below type of query but could not find the correct query. That's why I used the temp table:
insert into new_table
values (array of records) where record if not exist in new table.
And for the solution I mentioned above, I used the following query:
insert into new_table
select *
from temp_table
where not exist new_table.column = temp_table.column
Do you have a better suggestion that new records can be fetch and added to previous records?
It should look more like this:
insert into new_table
select *
from temp_table
where not exists (
select 1
from new_table
where new_table.column = temp_table.column
)
or maybe this:
insert into new_table
select *
from temp_table
where not exists (
select 1
from new_table
where new_table.column = temp_table.column
and new_table.column2 = temp_table.column2
and new_table.column3 = temp_table.column3
)
Hello after creating a SQL database in the following way:
Create table kante (v int, n int);
INSERT INTO kante (n,v) VALUES (1,2);
INSERT INTO kante (n,v) VALUES (2,6);
I want to see the start and end of the path. However, the following does not work:
WITH RECURSIVE pfad (v,n) as ((SELECT v,n from kante)
UNION ALL (Select k.V, p.N FROM Kante k, pfad p WHERE k.N=p.V))
SELECT * FROM pfad;
Also see the code at http://www.sqlfiddle.com/#!9/144162/4
Using SQLFiddle it produces the result:
DDL and DML statements are not allowed in the query panel for MySQL;
only SELECT statements are allowed. Put DDL and DML in the schema
panel.
What do I have to do differently?
Thank you!
Is it possible to select from mysql so that the result are insert statements?
I have to transfer "some rows" from my online database to my development workstation. This is always a lot of manual work.
It would be great if i could somehow:
select * from mytable where mycondition=myvalue AS SQL INSERT
and would get something like
INSERT INTO mytable VALUES ( 1, 'foo' );
INSERT INTO mytable VALUES ( 2, 'bar' );
...
This I could use to copy and paste into my local database. Is this (or something like that) possible?
EDIT: I'm looking for an easy/lazy solution which doesn't require much effort.
EDIT2: The mysqldump --where solution works for my question and 80% of my "real world" usage so thanks for the answer. Still it would be great if I could use complete queries for selection of the rows so that I could use joins or alter values.
Yes. You need mysqldump utility which does exactly that.
Use CONCAT to construct the INSERT statement:
SELECT CONCAT("INSERT INTO mytable VALUES(", id, ", '", somecolumn, "');\n") AS sql_insert
FROM mytable
WHERE mycondition = myvalue
I need to insert metadata values in a temp table. what is the easites thing to do it?
I have values like 3390,3391,8978,9899,7677,9656,5463 about 30-40 of them. I want to insert them into a temp table. Do not want to query a table since that is a big table and using an IN operator is very low in performance.
IS this the best way?
INSERT INTO #Table
Select '3390'
UNION ALL
select '3391'
UNION ALL
select '8978'
Any other suggetions?
It could be bit easier this way;
Insert into #temp (field)
select number
from (values (123),(456),(678),...,(432)) as t(number)
Or search for a split function and do it like;
insert into #temp (field)
select item from dbo.split('123,456,789',',')
To create and insert at the same time, you could use 'Select Into' syntax.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I merge two MySql tables?
I want to merge multiple tables that have the same structure and make one large table. The tables have similar names, so I want to use the LIKE statement. Can anyone tell me how I can do this?
The tables are very simple, each having an ID column and a few other columns, but there are a large amount of tables, all of which have names like 'TX-xxx', where 'TX' means Texas, and 'xxx' are the counties in Texas; you know there are more than 200 counties in Texas. (In fact, I have to do this for all the states.) So I want to use the statement "LIKE 'TX-___'".
Thanks!
You would have to give more information so we know exactly what you want but you could create a view
CREATE VIEW myViewName AS
select *
from table1
union all
select * from
table2
This way it would show the information from all your tables (and can be limited so in the selects to not show everything) and when table1, table2, etc are changed the view will reflect this. You can change it at anytime and fetch from it as you would a table:
select * from myViewName
Now for grabbing from specific tables I am not sure how you can do this in mysql though I have done it in tsql. This previous question would help you so you might have something like:
-- Create temporary table of varchar(200) to store the name of the tables. Depending on how you want to go through the array maybe an id number (int).
insert into tempTableName (name)
SELECT table_name FROM information_schema.tables WHERE table_schema = 'database_name' and table_name like 'TX_%';
declare #sqlQuery varchar(max)
--Then you will want to loop through the array and build up an sql statement
-- For each loop through:
if len(#sqlQuery) = 0 begin -- first time through
set #sqlQuery = 'select col1, col2, col3 from ' + currentTableName
end else begin -- second+ time through
set #sqlQuery = 'union all select col1, col2, col3 from ' + currentTableName
end
-- after the loop add the create view. Could double check it worked by checking length = 0 again
set #sqlQuery = 'CREATE VIEW myViewName AS ' + #sqlQuery
Once the query string is built up you will execute it with
PREPARE stmt FROM #sqlQuery;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
If I understand your question correctly UNION is what you need. Something like
SELECT field1, field2
FROM (
SELECT field1, field2 from table1
UNION
SELECT field1, field2 from table2
) all_tables
WHERE all_tables.field1 like "%whatever%
Assuming they have the same columns or similar:
insert into #table
Select * from (Select * from tbl1
Union
select * from tbl2
Union
select * from tbl3)
If they don't have the same number/type of columns then you should provide us with that information.