Question:
How to insert a column into sql table with unique data on each row?
Sykikal
2012-07-25 14:07:41 UTC
Hello, I have an sql database, and a column of data with several hundred rows on excel that I want to insert. Each row is unique, and has an ID.

This ID corresponds to an ID in the sql table. The goal is to add the information in the column into the already made rows in the sql table, not create entirely new rows. I also would like to avoid manually inserting each row. What's a good way to accomplish this?
Three answers:
godfatherofsoul
2012-07-25 19:33:23 UTC
You want to create something called a sequence, which is a special table that automatically creates UIDs that aren't duplicated.



You're not saying what DBMS you're using, but it usually looks something like this:



CREATE SEQUENCE table_name_seq;

INSERT INTO table_name(id,person_name) VALUES (table_name_seq.nextval(),'Dave');



You can get the last created unique ID by doing table_name_seq.currval.
?
2016-07-24 03:25:10 UTC
We can't insert the more than one rows at a time. We can insert one row at a time. For inserting a couple of rows at a time. You have to write the PL/SQL language or write the code with font-finish instrument like java, vb, vc++ etc. That's it.
TheMadProfessor
2012-07-26 06:22:30 UTC
Here's one possible method:



1) Export the excel sheet as a csv or xml file.

2) Create a temporary table such as #ages (ID, age) and import the data from your flat file

3) Execute ALTER TABLE to add a new column to your existing table named age

3) Execute an update query like

UPDATE existingTable a SET age =

(SELECT age FROM ages b WHERE b.ID = a.ID)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...