SQL: Insert multiple sets of values in one statement? - sql

Is it possible to insert multiple sets of values to a SQLite table in one statement?
I was trying:
INSERT INTO the_table VALUES (1,2,'hi'),(2,0,'foo');
with the different ()s representing different insert sets, but I get an error.

Are there only three columns in your table? If not, you could try defining the column names you are setting like so:
INSERT INTO the_table
(column1 ,column2 ,column3)
VALUES (1 ,2 ,'hi' )
,(2 ,0 ,'foo' )
This convention was introduced in SQL Server 2008 known as the Table Value Constructor. See MSDN's INSERT page for a look at the overall syntax. Also, the INSERT statement can be easily formatted for better readability.

You can do
INSERT INTO the_table
SELECT 1,2,'hi'
UNION
SELECT 2,0,'foo';

I was found that syntax in MSDN but after trying I can't do that too, than I note that in the bottom of the page was written that there is an error in the page :) where is link http://msdn.microsoft.com/en-us/library/ms174335.aspx see the bottom How to insert multiple rows

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

Selecting rows where cells contain multiple values

Using Oracle SQL Developer 1.5.5.
I have a data table and I'm trying to select rows from the table (TABLENAME) where a column (COLUMN5) in the table contains multiple values. For example using the following command:
select * from TABLENAME where COLUMN5=''101','221','429''
I'm doing this because I want to select all rows in the table where the cell values in column 5 are '101','221',229'
An added complication is that in the table each of the values is within single quotation marks, separated by a comma. When run the above command I get an error message which says " SQL command not ended properly" I think this is something to do with the single quote marks and the multiple values because when i run the following command to select rows when column 5 is '443', I get a result:
select * from TABLENAME where COLUMN5='''443'''
Note that these values of '443' have single quotation marks around them in the table.
My question is what command will allow me to select rows with the multiple values in column 5? Help!
Let me know if you require any clarification
I think you can use FIND_IN_SET like below:
select * from accounts where FIND_IN_SET('101','101,102')
FIND_IN_SET ignore the single quotes
you just need to escape the quotes
create table so_col5 (a integer, col5 varchar2(25));
insert into so_col5 values (1, 'abcd');
insert into so_col5 values (2, 'abcd');
insert into so_col5 values (3, 'abcd');
Insert into SO_COL5 (A,COL5) values (4,'''101'',''221'',''229''');
commit;
select * from so_col5 where col5 = '''101'',''221'',''229''';
It took me a few tries, testing selecting the string from dual, but then I remembered I could just cheat - and export the result from the GRID as an INSERT Script and copy/paste the quoted string for that value and use that in my SELECT.
You're using v1.5, which is ANCIENT. Highly suggest you upgrade for an optimal experience.
You can also use the Q function (docs) which you can pass a string and Oracle handles the quote escaping for you. Here's a scenario you can play with on LiveSQL.

SELECT * FROM NEW TABLE equivalent in Postgres

In DB2 I can do a command that looks like this to retrieve information from the inserted row:
SELECT *
FROM NEW TABLE (
INSERT INTO phone_book
VALUES ( 'Peter Doe','555-2323' )
) AS t
How do I do that in Postgres?
There are way to retrieve a sequence, but I need to retrieve arbitrary columns.
My desire to merge a select with the insert is for performance reasons. This way I only need to execute one statement to insert values and select values from the insert. The values that are inserted come from a subselect rather than a values clause. I only need to insert 1 row.
That sample code was lifted from Wikipedia Insert Article
A plain INSERT ... RETURNING ... does the job and delivers best performance.
A CTE is not necessary.
INSERT INTO phone_book (name, number)
VALUES ( 'Peter Doe','555-2323' )
RETURNING * -- or just phonebook_id, if that's all you need
Aside: In most cases it's advisable to add a target list.
The Wikipedia page you quoted already has the same advice:
Using an INSERT statement with RETURNING clause for PostgreSQL (since
8.2). The returned list is identical to the result of a SELECT.
PostgreSQL supports this kind of behavior through a returning clause in a common table expression. You generally shouldn't assume that something like this will improve performance simply because you're executing one statement instead of two. Use EXPLAIN to measure performance.
create table test (
test_id serial primary key,
col1 integer
);
with inserted_rows as (
insert into test (c1) values (3)
returning *
)
select * from inserted_rows;
test_id col1
--
1 3
Docs

SQLite : insert multiple value (select from) with insert specified value

i'm trying to make sqlite query that can insert multiple values , here's my query that i try :
insert into table1(idy_table1,idx_table1)
values ('1', //specified value insert to idy_table1
(select id_table2 from table2)) //insert value from select id_table2
i'm having some trouble, it just only insert one value,
and my question is how to make a proper query? so i can make it work.
The VALUES clause always adds one row.
(Except when you're using multiple tuples, but this does not work with queries.)
The easiest way to add multiple rows from a query is to use the SELECT form of the INSERT statement:
INSERT INTO Table1(idy_table1, idx_table1)
SELECT '1', id_table2 FROM table2;

Use INSERT-OUTPUT to provide values for another INSERT

Good day,
I was wondering if it is possible to use an INSERT-OUTPUT statement in such a way as to provide the value(s) for another, outer, INSERT statement. That way values can be added to an entity table and an intersection table in a single statement - I hope I'm wording this effectively. For example:
INSERT INTO [#tblIntersect] ([Entity1ID], [Entity2ID])
VALUES
(
INSERT INTO [#tblEntity1] ([Value])
OUTPUT [inserted].[ID] AS [entity1ID], #entity2ID AS [entity2ID]
VALUES ('One')
)
So the inner INSERT-OUTPUT statement will add a new entity to table #tblEntity1. The new entity's ID (which is set as IDENTITY(1, 1) will then be returned through the OUTPUT statement, along with a static value (which I already have in my code), to provide the two values for the outer INSERT statement.
The reason I think it might be possible is because execution of the inner INSERT-OUTPUT statement on its own returns a table anyway, and such output can usually be used to provide values for INSERT statements.
Obviously this example doesn't work; I was hoping it's just a simple syntax problem.
Thank you in advance for any comments and advice.
Your requirement is possible according to the documentation.
Assuming #tblIntersect has two matching id columns this should work
INSERT INTO [#tblEntity1] ([Value])
OUTPUT [inserted].[ID] AS [entity1ID], #entity2ID AS [entity2ID]
INTO #tblIntersect
VALUES ('One')