I can't insert multirow in firebirdsql - sql

I know in sql server to
Insert into table ( id, name) values ('1', 'John'), ('2','Peter');
but in firebid 1.5 i don't know how to insert the values.
I tried with
Insert into table ( id, name)
select '1', 'John' from rdb$database
union all select '2', 'Peter' from rdb$database;
but does it not work

The documentation says:
UNION allowed in feeding SELECT
Changed in: 2.0
Description: A SELECT query used in an INSERT statement may now be a
UNION.
Since you're using version 1.5, it looks like you'll have to use multiple insert statements.

Related

bulky insert record to database

I need to insert approximatlly 10 records.I am thinking foreach loop,do u think is it the best way to do that.whenn searching what is the best method to do that,I find something like this,
GO
INSERT INTO MyTable (FirstCol, SecondCol)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
UNION ALL
SELECT 'Third' ,3
UNION ALL
SELECT 'Fourth' ,4
UNION ALL
SELECT 'Fifth' ,5
GO
what is the differences between the this and foreach loop.which one is the better.
thank you all
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES ('First',1),
('Second',2),
('Third',3),
('Fourth',4),
('Fifth',5)
GO
Both the foreach loop and the union all methods would be what is called "Row by Agonizing Row" approach
If you have MSSQL 2008 or later, you can use multi-valued insert:
INSERT INTO mytable (id, name)
VALUES (1, 'One'),
(2, 'Two'),
(3, 'Three')
Unfortunately, this syntax is not supported by MSSQL 2005 and earlier.
Just Create a single for inserting all the record..
Because foreach loop will hit the database 10 times and u will take more time and resources..
So better to make single query for all the 10..
INSERT INTO Table ( FirstCol, SecondCol) VALUES
( Value1, Value2 ), ( Value1, Value2 )...

Insert SQL for multiple record

I get the error message when I run the following query in MSSQL Server 2005.
Error Message is Incorrect syntax near ','. I think query is ok. But I don't know why I get error.
INSERT INTO PERSON (ID, EMP_NAME) VALUES ('E001', 'AAA'), ('E002', 'BBB');
SQL Server does not support?
If your DB is lower than SQL Server 2008
INSERT INTO PERSON (ID, EMP_NAME) VALUES ('E001', 'AAA');
INSERT INTO PERSON (ID, EMP_NAME) VALUES ('E002', 'BBB');
Try to use UNION ALL -
INSERT INTO Person (id, EMP_NAME)
SELECT id = 'E001', EMP_NAME = 'AAA'
UNION ALL
SELECT 'E002', 'BBB'
Try this:
INSERT INTO Person (id, EMP_NAME)
SELECT 'E001', 'AAA'
UNION ALL
SELECT 'E002', 'BBB'

inserting multiple values into a single cell

i am trying to insert multiple values into a single cell column in my db. i am using a sql statment like this:
insert into table_name (mycol) values (1A,2B,3G,4,5,6F)
this table has only one field called "mycol"
the above query doesn't run and gives me an error. however, if i run it single entry like this:
insert into table_name (mycol) values (1A)
it works!
is there a way to feed sql with multiple values?
You have to break up the values portion like so (if you're using SQL Server)
insert into table_name (mycol) values (1A),(2B),(3G),(4),(5),(6F)
Based on this article, you can do something like this:
USE YourDB
GO
INSERT INTO MyTable (FirstCol, SecondCol)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
UNION ALL
SELECT 'Third' ,3
UNION ALL
SELECT 'Fourth' ,4
UNION ALL
SELECT 'Fifth' ,5
GO
I highly recommend against storing multiple values in a single field. You're better off, normally, with having seperate fields or multiple rows...
If student X is on Courses A, B, C;
INSERT INTO mapping_table (student_id, course_id)
SELECT 123, 1
UNION ALL SELECT 123, 2
UNION ALL SELECT 123, 3
If student X has 3 lines in his address;
INSERT INTO student_address (student_id, line1, line2, line3)
SELECT 123, '10 The Street', 'The Town', 'The City'
You need to pivot the selected values or do something like this:
INSERT dbo.Table_1(mycol)
SELECT * FROM
(
SELECT '1a' AS mycol UNION
SELECT '2b' UNION
SELECT '3g' UNION
SELECT '4' UNION
SELECT '5'
) T1

Insert multiple values using INSERT INTO (SQL Server 2005)

In SQL Server 2005, I'm trying to figure out why I'm not able to insert multiple fields into a table. The following query, which inserts one record, works fine:
INSERT INTO [MyDB].[dbo].[MyTable]
([FieldID]
,[Description])
VALUES
(1000,N'test')
However, the following query, which specifies more than one value, fails:
INSERT INTO [MyDB].[dbo].[MyTable]
([FieldID]
,[Description])
VALUES
(1000,N'test'),(1001,N'test2')
I get this message:
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ','.
When I looked up the help for INSERT in SQL Sever Management Studio, one of their examples showed using the "Values" syntax that I used (with groups of values in parentheses and separated by commas). The help documentation I found in SQL Server Management Studio looks like it's for SQL Server 2008, so perhaps that's the reason that the insert doesn't work. Either way, I can't figure out why it won't work.
The syntax you are using is new to SQL Server 2008:
INSERT INTO [MyDB].[dbo].[MyTable]
([FieldID]
,[Description])
VALUES
(1000,N'test'),(1001,N'test2')
For SQL Server 2005, you will have to use multiple INSERT statements:
INSERT INTO [MyDB].[dbo].[MyTable]
([FieldID]
,[Description])
VALUES
(1000,N'test')
INSERT INTO [MyDB].[dbo].[MyTable]
([FieldID]
,[Description])
VALUES
(1001,N'test2')
One other option is to use UNION ALL:
INSERT INTO [MyDB].[dbo].[MyTable]
([FieldID]
,[Description])
SELECT 1000, N'test' UNION ALL
SELECT 1001, N'test2'
You can also use the following syntax:-
INSERT INTO MyTable (FirstCol, SecondCol)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
UNION ALL
SELECT 'Third' ,3
UNION ALL
SELECT 'Fourth' ,4
UNION ALL
SELECT 'Fifth' ,5
GO
From here
In SQL Server 2008,2012,2014 you can insert multiple rows using a single SQL INSERT statement.
INSERT INTO TableName ( Column1, Column2 ) VALUES
( Value1, Value2 ), ( Value1, Value2 )
Another way
INSERT INTO TableName (Column1, Column2 )
SELECT Value1 ,Value2
UNION ALL
SELECT Value1 ,Value2
UNION ALL
SELECT Value1 ,Value2
UNION ALL
SELECT Value1 ,Value2
UNION ALL
SELECT Value1 ,Value2

Inserting multiple rows in a single SQL query? [duplicate]

This question already has answers here:
Insert multiple rows WITHOUT repeating the "INSERT INTO ..." part of the statement?
(17 answers)
Closed 9 years ago.
I have multiple set of data to insert at once, say 4 rows. My table has three columns: Person, Id and Office.
INSERT INTO MyTable VALUES ("John", 123, "Lloyds Office");
INSERT INTO MyTable VALUES ("Jane", 124, "Lloyds Office");
INSERT INTO MyTable VALUES ("Billy", 125, "London Office");
INSERT INTO MyTable VALUES ("Miranda", 126, "Bristol Office");
Can I insert all 4 rows in a single SQL statement?
In SQL Server 2008 you can insert multiple rows using a single SQL INSERT statement.
INSERT INTO MyTable ( Column1, Column2 ) VALUES
( Value1, Value2 ), ( Value1, Value2 )
For reference to this have a look at MOC Course 2778A - Writing SQL Queries in SQL Server 2008.
For example:
INSERT INTO MyTable
( Column1, Column2, Column3 )
VALUES
('John', 123, 'Lloyds Office'),
('Jane', 124, 'Lloyds Office'),
('Billy', 125, 'London Office'),
('Miranda', 126, 'Bristol Office');
If you are inserting into a single table, you can write your query like this (maybe only in MySQL):
INSERT INTO table1 (First, Last)
VALUES
('Fred', 'Smith'),
('John', 'Smith'),
('Michael', 'Smith'),
('Robert', 'Smith');
NOTE: This answer is for SQL Server 2005. For SQL Server 2008 and later, there are much better methods as seen in the other answers.
You can use INSERT with SELECT UNION ALL:
INSERT INTO MyTable (FirstCol, SecondCol)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
UNION ALL
SELECT 'Third' ,3
...
Only for small datasets though, which should be fine for your 4 records.
INSERT statements that use VALUES syntax can insert multiple rows. To do this, include multiple lists of column values, each enclosed within parentheses and separated by commas.
Example:
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);