Difference between INSERT INTO and SELECT INTO when calling OPENROWSET - sql

select 1 as X,d.* into [TravelData] from OPENROWSET('SQLNCLI','Server=<redacted>',
'exec [OtherDB].[GetTravelData] 1, ''28-Nov-2016 16:00'', ''28-Nov-2016 19:00''') as d
I have this as a way to slurp remote DB into a local table. This syntax appears to work BUT I get error:
There is already an object named 'TravelData' in the database.
Makes sense, SELECT INTO is supposed to create the table. But thinking I'd simply change SELECT to INSERT I then get syntax errors. What should the correct syntax be to get this data into an existing DB table whose structure matches the query output?

It has nothing to do with using OPENROWSET.
INSERT INTO ... requires that the table already exist.
SELECT ... INTO requires that the table not exist. The table will be created by the statement using the columns defined in the SELECT.

Here is the INSERT INTO SELECT syntax
INSERT INTO [TravelData]
(X,
col1,
col2,
...)
SELECT 1 AS X,
d.col1,
d.col2,
.....
FROM OPENROWSET('SQLNCLI',
'Server=<redacted>',
'exec [OtherDB].[GetTravelData] 1, ''28-Nov-2016 16:00'', ''28-Nov-2016 19:00''') AS d
Note : Instead of * in select list add the column list. Also in Insert mention the column list

Related

Generate insert column based on select columns

I have a scenario, where 100's of select statements sql's are in one metadata table or some text file.
Need to insert all sql results into one specific table. (master table has col1, col2,col3 .... 200columns )
problem im facing(ORA-00947) is every select statement has different number of columns.
.. i need to generate INSERT PART.
CASE 1 : INSERT INTO (COL1,COL2,COL3) <<this select part comes from file/variable>>
CASE 2 : INSERT INTO (COL1) <<this select part comes from file/variable>>
CASE 3 : INSERT INTO (COL1) <<this select part comes from file/variable>>
have to figure out how many columns are in select part then generate INSERT part.
.
Thought of create as select but problem is some select statement has max(col) without alias so it will fail.
This is too long for a comment.
If you are storing SQL in a table, then you are constructing your query dynamically. So, update the table and list the columns that you want.
You could then construct the inserts as :
insert into master_table (<column list here>)
<select here>;
Both the select and column list would come from the table.
By far the easiest is to create a view for each SELECT statement. Then you can query the USER_TAB_COLUMNS view on the view name and get the column names.
Best regards,
Stew Ashton

insert data from one mysql table to another

i am trying to run this SQL query to copy data from one table to another but it shows zero rows returned:
INSERT INTO ticket_updates
VALUES
(
SELECT *
FROM ticket_updates2
WHERE sequence = '4715'
)
You cannot put a select statement into a values method.
Your syntax should look like this:
INSERT into ticket_updates(all,my,columns)
select all,my,columns from ticket_updates2 where sequence = '4715'

H2 database, insert by selecting results from CSVREAD

I have a CSV file like
1,hello,13
2,world,14
3,ciao,26
I'm trying to use CSVREAD function to read this file into database, like this
insert into my_table( id, message, code ) values (
select convert( "id",bigint ), "message", convert( "code", bigint)
from CSVREAD( 'myfile.csv', 'id,message,code', null )
);
For some reason I keep on getting SQL error stating that the column count does not match.
The table is created with Hibernate/GORM and contains the fields I try to insert into.
The select itself seems to work, or at least it does not cause any errors when executed alone. What's wrong with my statement?
You have used
insert into my_table(...) values (select ...)
but you should use, as documented in the SQL railroad diagrams,
insert into my_table(...) select ...
Actually, for H2, it is a bit faster if you create the table as follows, but I understand it is not always possible:
create table my_table(...) as select ...

copy the tables after execution

I would like to create new table after executing that query
create table newTable as select * from oldTable
However, this does not appear to work. How do I get the new table after executing some queries?
The syntax in general is like:
CREATE TABLE new_table
AS (SELECT * FROM old_table);
For example:
CREATE TABLE suppliers
AS (SELECT id, address, city, state, zip
FROM companies
WHERE id > 1000);
Try removing the stars (*) and add the brackets.
Read here for more examples.
I am not sure what DBMS you are using or what errors you are getting, so I will try to answer for multiple systems.
If you are working with Oracle or PostgreSQL (there might be some other systems that this rule applies to), your syntax seems to be correct. Just make sure your new table doesn't exist yet - otherwise it's going to error out. In case if you are trying to insert into an existing table - which I don't think the case is, however - you can try something like -
INSERT INTO newTable SELECT * FROM oldTable
On the other hand, if you are working with T-SQL (SQL Server), you could SELECT INTO the new table. The new table will be created with the old table's schema.
You can read more about the INTO Clause at MSDN Library.
Your code should look like -
SELECT *
INTO newTable
FROM oldTable
And, specifying the column names and filters also works the similar way -
SELECT Column1, Column2, Column3, ...
INTO newTable
FROM oldTable
WHERE <Filter Condition>
Whatever the case is, you would get more help if you specify the details.
As You Said you want to copy the values into new table after execution
whether if you are running the stored procedure using cursor let the cursor shuld be closed then use query as follows
Select * into Table1 from Table2
if you want to copy selected colums go for
Select Coloumn1 ,column2,... into table1 from table 2 where ............

Insert result set from UNPIVOT into table

I am trying to find the syntax for inserting the results from an UNPIVOT statement into an existing table in the database?
simplest answer which works for any SELECT including UNPIVOT would be...
INSERT INTO MyTable
SELECT statement
However, this does require that your destination tables columns match your SELECT statement columns.
Although you can get around this limitation with...
INSERT INTO MyTable (column1, column2....)
SELECT statement