I am new to SQL, I am trying to add a column to my table from another table with certain condition
I have Employees2013 table that has StaffNumber column, and I have an Employees table that has StaffNumber and Title columns.
What I am trying to is create a new column called Title in Employees2013 and select title from Employees where Employees2013.StaffNumber = Employees.StaffNumber.
I tried this but it didn't work:
insert into Employees2013(Title)
select e.Title
from LandornetSQL.dbo.Employees e, Employees2013 f
where e.StaffNumber = f.StaffNumber
I get this error:
Cannot insert the value NULL into column 'StaffNumber', table 'xDevProjects.NA\OnderO.Employees2013'; column does not allow nulls. INSERT fails.
Anyone has any idea?
Insert only inserts records, if you want a new column, you must add the column to the table.
ALTER TABLE Employees2013 ADD Title VARCHAR(100)
Then you can update the table, setting the Title column
UPDATE Employees2013 SET Title = Employees.Title
FROM Employees
WHERE Employees.StaffNumber = Employees2013.StaffNumber;
Related
enter image description hereI created a table and inserted 8 rows. Then I added a new column called Childname using alter. How can I add or insert different values into the new column at once without using update and Set for each row within the new column? I mean the Childname column showing the null values. I highlighted it with a red arrow.
As far as I understand your question;
You created table then add new column:
CREATE TABLE foo(id int, name varchar(255), city varchar(255));
INSERT INTO foo(id, name, city) VALUES (1, "John", "NYC");
INSERT INTO foo(id, name, city) VALUES (2, "Bill", "Boston");
....
# Alter table
ALTER TABLE foo ADD uid int;
After ALTER operation, surnames were null
If you want to add values to surname column of rows that already existed in the table, you need to use UPDATE like in below sql:
UPDATE foo SET uid = 1 WHERE id = 1
WHERE parameter can change by the case.
UPDATE:
After adding a new column to table which has rows, you could set that column with auto-increment property in SQL Server:
ALTER TABLE foo ADD uid INT IDENTITY(1, 1)
I have a petl table obtained from a database in which I transform and join with data from other databases. The thing is that I need to insert or update data in a new table depending of the fields but the petl table columns doesn't always match with table columns.
Is any method that can create these columns if there are not already in the table? I tried this but returns a FieldSelectionError
table = etl.cut(table, 'Column1', 'Column2', 'Column3', 'Column4')
FieldSelectionError: selection is not a field or valid field index: 'Column4'
Context
I got a temporary table which is filled/adjusted by users. Let's call the table tmp, with columns ID, updated_at, price, foreign_ID. Every time the user enters a new price, the price column is filled and the updated_at and ID are created automatically. foreign_ID is NULL until the record is processed to another table, when my foreign_ID should contain the ID of the other table.
Periodically I update a table with prices, let's call it prices. Here are all prices stored from different sources, among them from the tmp table. The prices table has the columns ID, updated_at, price.
question
I want to insert the data from my tmp table into prices table, and update the column foreign_ID with the corresponding ID from my prices table. How can I insert new rows in a table and update/set a IDs in another table?
My desired result after the periodic update is a new entry in the prices table with the new prices which were not yet processed, and a foreign_ID in my tmp table which corresponds with the ID in my prices table.
I know I can output the inserted IDs using the following query:
insert into prices
output inserted.ID
select price
from tmp;
I'm struggling to see how I can use the inserted.ID to update my tmp.foreign_ID column with the output above.
Help is appreciated!
You can also INSERT the values from the OUTPUT clause into another table as well, so if you need those you can still reference them.
Without proper sample data and behaviour, this is just an example but should, hopefully, set you on the right path as it shows you how to get the values from inserted into another object. You can then use that object to do the additional task(s) you need:
CREATE TABLE dbo.SomeTable (ID int IDENTITY(1,1),
SomeString varchar(10));
GO
DECLARE #IDs table (ID int);
INSERT INTO dbo.SomeTable (SomeString)
OUTPUT inserted.ID
INTO #IDs (ID)
VALUES('asdjgkde'),('sdflhdglf');
SELECT *
FROM #IDs;
GO
DROP TABLE dbo.SomeTable;
Within the context of SQLite.
I have an existing table which is currently populated with numerous rows of data.
I am trying to add a new primary key column to this table whilst persisting the original data.
As demonstrated below, I have tried the following
Add a new column to the existing table (Id INTEGER).
Change the name of the existing table.
Create a new table which includes the new primary key (Id INTEGER PRIMARY KEY).
Insert all data from the renamed table into the newly created table.
Drop the renamed table.
The reason I thought this would work is because according to SQlite documentation,
A column declared INTEGER PRIMARY KEY will autoincrement.
However I am receiving the following error.
ErrorCode : 19
Message : constraint failed
UNIQUE constraint failed: Person.Id
Result : Constraint
Here is my code.
--Add a new column to the existing table(Id INT).
ALTER TABLE [Person]
ADD Id INTEGER;
--Change the name of the existing table.
ALTER TABLE [Person] RENAME TO Person_temp;
--Create a new table which includes the new PK.
CREATE TABLE Person(
Id INTEGER PRIMARY KEY,
FirstName nvarchar(100) NULL,
LastName nvarchar(100) NULL
);
--Insert all data from the renamed table into the new table.
INSERT INTO Person SELECT * FROM Person_temp;
--Drop the renamed table.
DROP TABLE Person_temp;
Could anyone be kind enough to shed some light?
Since you do not declare column names in your insert query, the column order depends on the order in witch they where created / added. Try to specify the column names. This is usually a good practice anyway
--Insert all data from the renamed table into the new table.
INSERT INTO Person(Id, FirstName, LastName) SELECT Id, FirstName, LastName FROM Person_temp;
By the way, you probably don't need to add the Id column in the first table :
--Insert all data from the renamed table into the new table.
INSERT INTO Person(FirstName, LastName) SELECT FirstName, LastName FROM Person_temp;
the implicit null value for Id will be replaced by the autoincrement
It sure seems like your Id column does not contain unique value in each row. Since you just added the column, each row will have the same value.
The auto increment is there to help when you insert new rows. (You don't have to select max(id), and insert the new row with id = max+1). It won't auto-populate an existing table of data.
SQLite already has a column that could work for what you want. It's called ROWID. Try using that instead of duplicating it with your Id column.
I am having one table with 3 f_Key and 1 P_Key with 6054 records.
One record is lost from that table. I am trying to insert record into that table.
The record id is 2352 and last record id is 9560 so,if i insert the record then it is taking 9561 id which is next id of before id.If try to delete the others records then because of F_Key it is not allowing to delete also.If i try to update the 9561 id then it also not allowing to update.
You can use the SET IDENTITY INSERT construct to explicitly insert the PK value in a table with auto-numbering, like so:
set identity_insert #your_table on
insert into your_table (PK_COL_IDENTITY, ...) values (2352, ...)
set identity_insert #your_table off
As per my knowledge , if your ID is auto incremented then you cannot update that ID(key) .The only way to do in your case is TRUNCATE.If you will truncate the table then it will allow to generate new sequence.
You can create a temporary table and migrate the data to temporary table and truncate that parent table and again migrate the data from temporary table to parent table.
Hope it will help you.