I am developing an attendance management program, used to maintain the absence record of a student. Users of this software will need to enter various dates, updated once in a month: for instance, a list of dates on which a student was absent for that particular month would be entered, and my program must then store them into a database with each date added as a new row in the appropriate table.
I have the dates stored using arrays internally, how might I transfer these into the database? How should I proceed?
You have not mentioned the database system being used, so my reply is general in nature. The usual way to do this is to run multiple insert statements one after another:
INSERT INTO Table1 (FirstColumn, SecondColumn)
VALUES ('a', 'b');
INSERT INTO Table1 (FirstColumn, SecondColumn)
VALUES ('c', 'd');
INSERT INTO Table1 (FirstColumn, SecondColumn)
VALUES ('e', 'f');
GO
The trick way to do this is to use the UNION ALL statement:
INSERT INTO Table1 (FirstColumn, SecondColumn)
SELECT 'a', 'b'
UNION ALL
SELECT 'c', 'd'
UNION ALL
SELECT 'e', 'f'
GO
Versions of SQL Server prior to 2008 support only these methods. But SQL 2008 and MySQL 3.22 and above support the Row construction method as well:
INSERT INTO Table1 (FirstColumn, SecondColumn)
VALUES ('a', 'b'),
VALUES ('c', 'd'),
VALUES ('e', 'f')
GO
Now you can use any of the above methods to iterate through your array and add individual attendance rows to the database.
foreach($arrayName as $arrayValue) {
// run your query here!
}
for example:
$myArray = array('apple','orange','grape');
foreach($myArray as $arrayFruit) {
$query = "INSERT INTO `Fruits` (`FruitName`) VALUES ('" . $arrayFruit . "')";
mysql_query($query, $connection);
}
does that makes sense / fit what you were thinking?
Do you want to store the dates seperately so you can juggle with them, query them, etc.?
Or do you just want to store the array as is?
If you want to store the dates separately you may want to create a table with an FK to students, a column for date and a column for the nature of the date, like absence, late, ...
Then you would indeed store the single dates into that table. If you must, by iterating but if you can with one of Cerbrus' solutions!. It is not recommended to have db-queries within loops.
If you just need to store that array somewhere, you can serialize it and store the serialized string in a text or varchar column.
What language and what type of database are you using? Is this a web application? A desktop application? We can't help you without more information.
Depending on your situation, any of the above solutions could work. Or you could even load all of the attendance records at once as a CSV or XML document. Perhaps a little more research on your part will help you ask a more useful question?
Iterate over the array and execute insert SQL for each date.
Related
USE DATABASE
insert into #Table -- Previously created temp table
(
Name
Number
Date
)
select 'Joe', 5, 'January 9th'
union all select 'Sam', 3, 'January 4th'
union all select 'Eleanor', 4, 'January 5th'
union all select 'Joseph', 1, 'January 6th'
My question is what is the scope of the USE statement when insert into select is not specifying a 'from' statement that clearly denotes what table in the database the information is coming from?
I've encountered an insert into select statement similar to this one and what I am struggling to understand is if the data is being created in the four select statements or if it is being searched for and found in the DATABASE. If it is being searched for how does Microsoft SQL Server Management studio know what table is being referred to?
Everything following USE database will be run in the context of the database chosen.
In this case, the USE accomplishes nothing, because your #temp table is stored in tempdb regardless of which database you're using, and your select is not accessing any tables, so it will return the hardcoded values regardless of database context as well.
If there is no FROM clause, then the data isn't coming from any table in any database. It is hard-coded into the INSERT query. If you're asking how does it know what table to insert the data into, that is specified in the INSERT INTO clause, and is in the context of the USE statement.
I'm suspicious of your question, because you insert into a temp table, which you comment as "previously created" and the scope of temp tables is an entirely different matter.
I would like to write PL/SQL generator of dummy data (e.g. names as combination of first and last most popular names) for Oracle 12c.
So I need to populate names and surnames tables with source data first.
I cannot use sqlldr and all I have access to is SQL*Plus and SQLDeveloper.
I can populate my source tables with lots of individual insert statements like:
INSERT INTO names(id, name) VALUES(1, 'Oliver');
INSERT INTO names(id, name) VALUES(2, 'Jack');
⋮
INSERT INTO names(id, name) VALUES(50, 'Aaron');
I wonder whether there are any other (perhaps more elegant) options.
Edit:
I was hitting another issue coming from a fact that value of generated identity column is not incremented during INSERT ALL. There is a nice workaround described in this related answer at SO.
You can do this in oracle:
INSERT INTO names(id, name)
select 1, 'Oliver' from dual union all
select 2, 'Jack' from dual union all
. . .
Then there is INSERT ALL:
INSERT ALL
INTO names (id, name) VALUES (1, 'Oliver')
INTO names (id, name) VALUES (2, 'Jack')
select * from dual;
I like the first one better.
You might also want to check out http://plsql.ninja/npg/package/random_ninja. Morten Egan has been building a very interesting library of utilities and his random_ninja package will generate all sorts of data for you.
I have a table into which i will insert and it has 4 columns.
While inserting 3 columns will be same and the other one column will be different for each and it will be taken from another table.
How could i do that?
For exmaple;
INSERT INTO sendMsg (Type,Name,SenderName,Message) values(4, 'john','Mike','Hi, blabla')
i will insert same message also for Bob, instead of john.
and the names which i will send are contained in Names table.
Thank you.
Use a select statement to build up your insert. Something like this could work (as you didn't provide more details):
INSERT INTO sendMsg (Type,Name,SenderName,Message)
SELECT 4, "name" ,'Mike','Hi, blabla' FROM anothertable
-- WHERE ....
Column names are in ", so don't be confused. It's to ensure difference between string and database object.
Inside optinal WHERE you could do maybe something like
WHERE name in ('Bob', 'John', ...)
or whichever algorithm you need to determine the names.
Afternoon all!
I'm working on a large database at work, and they've specifically asked me to implement triggers into the database.
Basically, we have a set of tables all containing different information, but if, in one particular table, a certain range of values are entered into the 'fieldID' column, I need it to trigger.
In this case, if a user INSERT data, with the fieldID >= 4 AND fieldID <=9, I need the SQL Server to trigger, with a command of inserting the data to a different table.
For example,
INSERT INTO myTestTable(state, process, fieldID) VALUES ('ACTIVE', 13,9);
(and the database actually performing)
INSERT INTO myFieldTable(state, process, fieldID) VALUES ('ACTIVE', 13,9);
INSERT INTO myTestTable(state, process, fieldID) VALUES ('ACTIVE', 13,42);
(and the database actually performing)
INSERT INTO myTestTable(state, process, fieldID) VALUES ('ACTIVE', 13,42);
I've found loads of stuff online about triggers and how they work in when a new record is entered in general, but nothing for specific data
In the simplest form, you can write a INSTEAD OF INSERT trigger to achieve what you are trying. something like below
CREATE TRIGGER trg_testoninsert on myTestTable
INSTEAD OF INSERT
AS
BEGIN
IF inserted.fieldID >= 4 AND inserted.fieldID <=9
INSERT INTO myFieldTable(state, process, fieldID)
select state, process, fieldID
from inserted;
END;
GO
I'm new to Sql but what is the best way to insert more than 1000 rows from an excel document into my database(Sql server 2008.)
For example I'm using the below query:
INSERT INTO mytable(companyid, category, sub, catalogueref)
VALUES
('10197', 'cat', 'sub', '123'),
('10197', 'cat2', 'sub2', '124')
This is working fine but there is a limit of inserting 1000 records and I have 19000 records and I don't really want to do 19 separate insert statements and another question, is that the company id is always the same is there a better way then writing it 19000 times?
Just edit the data in Excel or another program to create N amount of insert statements with a single insert for each statement, you'll have an unlimited number of inserts. For example...
INSERT INTO table1 VALUES (6696480,'McMurdo Station',-77.846,166.676,'Antarctica','McMurdo')
INSERT INTO table1 VALUES (3833367,'Ushuaia',-54.8,-68.3,'America','Argentina')
...19,000 later
INSERT INTO table1 VALUES (3838854,'Rio Grande',-53.78769,-67.70946,'America','Argentina')
Microsoft provides an import wizard with SQL Server. I've used it to migrate data from other databases and from spreadsheets. It is pretty robust and easy to use.
There are several options, the Import Wizard which Erik suggests, or SSIS is another good one.
Read here:
Import Excel spreadsheet columns into SQL Server database
Ok, it's a late answer but I ran into this very same problem and found a solution that worked twice as fast as #Nur.B's solution for a 7K-row insertion.
Note: whenever possible prefer to use TRANSACTIONS when dealing with large amounts of data.
INSERT INTO mytable(companyid, category, sub, catalogueref)
SELECT '10197', 'cat', 'sub', '123' UNION ALL
SELECT '10197', 'cat2', 'sub2', '124' UNION ALL
-- ... other N-thousand rows
SELECT '10197', 'catN-1', 'subN-1', '12312' UNION ALL
SELECT '10197', 'catN', 'subN', '12313'; -- don't add "UNION ALL" statement on the last line
You should be able to insert using multiple transactions -
BEGIN TRANSACTION
Insert into mytable(companyid,category,sub,catalogueref)
values
('10197','cat', sub','123'),
('10197','cat2', sub2','124')
...998 more rows...
COMMIT TRANSACTION
go
BEGIN TRANSACTION
Insert into mytable(companyid,category,sub,catalogueref)
values
('10197','cat', sub','123'),
('10197','cat2', sub2','124')
...998 more rows...
COMMIT TRANSACTION
INSERT INTO mytable(companyid, category, sub, catalogueref)
SELECT companyid, category, sub, catalogueref from (VALUES
('10197', 'cat', 'sub', '123'),
('10197', 'cat2', 'sub2', '124')
//more 1000 rows...
as sub (companyid, category, sub, catalogueref)