I just want to know if it is possible to insert data into a table that has columns for example ID, FIRST_NAME, AGE, SEX, SALARY but I want to insert into all columns except the column id.
Normally as I know I need to set this code
INSERT_INTO TABLE_NAME (FIRST_NAME, AGE, SEX, SALARY)
VALUES (....);
but it will take a long time if there is a lot of columns...
Is there any code that will grant me time?
you can pass null value for the columns you don't want to set.
Entry will be inserted with the default value if provided in the schema, or null is field is nullable
Related
I'm trying to create a table in Oracle as follows:
create table person (
person_id number(5) generated always as identity
minvalue 1
maxvalue 99999
increment by 1 start with 1
cycle
cache 10,
firstname varchar(10),
lastname varchar(10));
This works all fine, but when I try something like this:
insert into person
values ('firstname', 'lastname');
I'm getting an error. When I try putting in a number where the ID should be, I get a different error.
I'm confused because I originally thought that formatting the person_id in this way automatically generated unique ID values, so that I wouldn't be able to put them in.
I'm also a bit confused on how this all fits into JDBC. If I'm working with Java and I insert a tuple into such a table, how am I able to get the generated ID into my Java program? I can't search by first and last name, because there could be two people with the same name.
You need to list the columns for an insert:
insert into person(firstname, lastname)
values ('firstname', 'lastname');
The fact that the column is generated doesn't affect the syntax of the insert. So, with no columns, the insert is the same as:
insert into person(person_id, firstname, lastname)
values ('firstname', 'lastname');
And with two values and three columns, you are getting an error.
I have two columns in lets say employees table.
1) emp_id which is a uuid autogenerated.
2) emp_name which is a string.
Now i want to put some default values in emp_name column at the time of insert and i want lets say first 6 characters of the corresponding emp_id added after 'emp_' string.
And i want all of it as a single insert query statement.
Is there a way to do such a thing in postgres.
Try this:
WITH emp as (SELECT public.gen_random_uuid() AS uuid)
insert into employees(emp_id, emp_name) (select
emp.uuid,'emp_'||substring(emp.uuid::varchar from 0 for 6) from emp);
You can generate the uuid, save it in a variable and use it in the insert statement.
To understand how you can create it and the different type of uuid, you can read this page:
http://www.postgresqltutorial.com/postgresql-uuid/
Have an issue. I have a database called DATA. Within it are multiple tables. One called MASTER and others called temp1 .
MASTER has columns called first, middle, last, dob, address, city, state, zip, phone, cell
Temp1 has essentially the same columns more or less, but in different orders, different column names, more columns than exist in MASTER, etc...
I'd like to be able to write a TSQL script that I can execute to move data from temp1 to MASTER, but map which column gets what data.
Using something like:
INSERT INTO MASTER
SELECT * from temp1
just blows up and name is in wrong field and its a mess due to the columns within temp1 which are, and in a jumbled order:
dateofbirth, lastname, firstname, middlename, telephone, cell, address, city, state, zip
What'd I'd like to do is be able to map the columns while they are transferred... like if I was using the import GUI.
so firstname to first, lastname to last, cell to cell, address to address, dateofbirth to DOB... etc. and some columns totally skipped.. but you see where its going :-)
Is this possible?? Or am I stuck using the GUI??
All the GUI does is generate SQL for you, so rest assured, this is possible.
Here's what you want:
INSERT INTO [MASTER] (
first, middle, last, dob, address, city, state, zip, phone, cell, ...
)
SELECT
firstname, middlename, lastname, dateofbirth, address, city, state, zip, telephone, cell, ...
FROM
[temp1]
SQL Server will use the indexes of the named columns to map data, disregarding names and types. If there is a type mismatch it will try to perform an implicit conversion or fail with a runtime error.
You can use a select statement with an insert statement which allows you to order which columns go where.
i.e.
INSERT INTO [MASTER] (FIRSTNAME,LASTNAME)
SELECT FIRST,LAST FROM TEMP1
You can have each column list (in both the insert and the select statements) in any order - regardless of the column structure of the table.
In my view, we need to map columns programatically or manually to insert data accurately.
I have one simple options, not sure if its best practices:
Can we generate insert statement for each row of data with proper column mappings?
For Example:
Export all the rows from TEMP1 table to Excel
Write an Insert Statement for all values of Row1 on Excel using CONCATENATE.
=CONCATENATE("INSERT INTO MASTER(Lname,Fname,Address) Values('",A1,"','",B1,"','",C1,"')
Use Drag options to Generate Insert Statement for all the rows.
Copy all the Insert Statements with values and Prepare for Inserting into Master table.
I am using Oracle databases. I have an sql table PS_Z_STAGE_TEST_JE that has three fields (EMPLID, LAST_NAME, FIRST_NAME).
I am trying to do a select statement that will pull many EMPLIDs from sql table:ps_vc_plan_mem and insert them into the EMPLID column while leaving the other two fields (LAST_NAME and FIRST_NAME) null.
Below is my SQL but it will say
Cannot insert null Value into LAST_NAME
when I try to run it.
INSERT INTO sysadm.PS_Z_STAGE_TEST_JE (EMPLID)
SELECT DISTINCT(emplid)
FROM ps_vc_plan_mem
WHERE vc_plan_id IN ('PNC-RS','PNC-SO','PNC-ESPP');
The only obvious problem that I see with your query is the asterisk at the end:
INSERT INTO sysadm.PS_Z_STAGE_TEST_JE(EMPLID)
SELECT DISTINCT emplid
FROM ps_vc_plan_mem
WHERE vc_plan_id IN ('PNC-RS', 'PNC-SO', 'PNC-ESPP');
Note that distinct is not a function. It is a modifier on select, so the parentheses don't mean anything.
The error message is exactly what it says it is. Your last_name column must be defined as not null. Therefore, you can't insert a null into it. Since you didn't define what to insert into the column in your insert, it tries to insert null by default and fails.
You must insert something into last name. I would suggest either a default string or an empty string if you can't get an actual last name to insert.
INSERT INTO sysadm.PS_Z_STAGE_TEST_JE (EMPLID, LAST_NAME)
SELECT DISTINCT(emplid), 'N/A'
FROM ps_vc_plan_mem
WHERE vc_plan_id IN ('PNC-RS','PNC-SO','PNC-ESPP');
Alternatively, you could alter your table so that last_name is nullable.
There is a asterisk at the end of your SQL statement. Please remove and retry.
If [sysadm].[PS_Z_STAGE_TEST_JE] table has a PK, you might want truncate the table before running the statement.
I have an application that is ready to go live, once we take data from a MS Access DB and import it into SQL Server 2005. I have used the Migration - Access tool to get the Access db into SQL Server, but now I need to take the data from that table and put it into the tables that our app is going to use. Is there a T-Sql way to Insert multiple rows, while at the same time 're-mapping' the data?
For example
SELECT ID, FIRST_NAME, LAST_NAME
INTO prod_users (user_id, first_name, last_name)
FROM test_users
I know that select * into works when the column names are the same, but the
prod_users (column names, ..., ...)
part is what I really need to get to work.
Any ideas?
I believe the SELECT INTO syntax is used to create new tables. If you want to map data from the tables you just imported to some other existing tables, try a plain INSERT. For example:
INSERT INTO prod_users (user_id, first_name, last_name)
SELECT ID, FIRST_NAME, LAST_NAME
FROM test_users
The mapping of columns from test_users to prod_users is based on the order that they are listed, i.e. the first column in "prod_users (column_names, ...)" matches the first column in the "SELECT other_col_names, ...", the second matches the second, etc. Therefore, in the code sample above, ID is mapped to user_id, and LAST_NAME is mapped to last_name, and so on.
Just make sure you have the same number of columns in each list and that the column types match (or can be converted to match). Note that you don't have to specify all the columns in either table (as long as the destination table has valid defaults for the unspecified columns).
See the INSERT syntax for details; the part relevant to your question is the "execute_statement" expression.
INSERT and SELECT are the magic keywords:
insert into new_table (list of columns) select columnA, functionB(),
(correlated_subquery_C) from table_or_join where critera_expression_is_true
Maybe you can be more specific about what you mean by re-mapping?
Base on your comment, a more specific query is:
insert into new_table (user_id, firstname, lastname)
select id, first_name, last_name from old_database..old_table