I am trying to insert a column of data from one table to another. The table I am trying to insert into is called MU.PROVIDERS. The table I am inserting from is called Sheet1$. (Imported from an excel sheet). The columns they have in common is called "NPI", a key that is common for all providers, so I am trying to insert based on this key. Not every NPI value in the Sheet1$ will put a corresponding RELAY_ID value into the 'MU.PROVIDERS' table. (There are more NPI's in the 'MU.PROVIDERS' than in Sheet1$) My query is as follows:
INSERT INTO [MU.PROVIDERS] (RELAY_ID)
SELECT h.RELAY_ID
FROM Sheet1$ as h JOIN
[MU.PROVIDERS] as i ON h.NPI = i.NPI;
I am getting the error:
Cannot insert the value NULL into column 'NPI', table 'MU.PROVIDERS'; column does not allow nulls. INSERT fails.
I do have the NPI column set as the primary key on the 'MU.PROVIDERS' table, but I am not inserting anything into this column so I do not understand the problem.
I think you want an update. Insert adds new rows. Update changes the values of columns:
UPDATE i
SET relay_id = h.relay_id
FROM mu.Providers i JOIN
Sheet1$ h
ON h.NPI = i.NPI;
You may have defined the NPI column as the primary key.
But the primary key needs a unique value to identify a row.
If you don't provide the value with your INSERT statement you should define the NPI column as IDENTITY(1,1) to automagically create a new identity value.
Related
This is a muti-tenant app. All records have a client id to separate client data. Customers can insert their own data in this table and set their own field nullable or not null. Therefore, setting the whole field not null will not work. I need to set a field null for a specific client id.
I am currently querying the database to check if the value is null. On INSERT I check if the inserting value is null if so I throw an error. I would like the database to do all these checks. is this possible in a multi tenant database like this?
Also, I need suggestions for SQL Server, oracle and postgresql. Thanks
With Postgresql at least you could do this with table inheritance.
You could define an inherited table for this specific client which included the required constraint.
Consider the following example:
psql=> CREATE TABLE a(client INT NOT NULL, id SERIAL, foo TEXT);
CREATE TABLE
psql=> CREATE TABLE b(foo TEXT NOT NULL, CHECK (CLIENT=1) ) INHERITS (a);
NOTICE: moving and merging column "foo" with inherited definition
DETAIL: User-specified column moved to the position of the inherited column.
CREATE TABLE
psql=> INSERT INTO b(client,foo) VALUES (1,'a');
INSERT 0 1
psql=> INSERT INTO b(client,foo) VALUES (1,NULL);
ERROR: null value in column "foo" violates not-null constraint
DETAIL: Failing row contains (1, 2, null).
The table 'b' in this case inherits from 'a' but has a different definition for column 'foo' including a not-null constraint. Also note that I have used a check constraint to ensure that only records for client 1 can go into this table.
For this to work, either your application would have to be updated to insert client records into the correct table, or you would need to write a trigger that does that automatically. Examples of how to do that are given in the manual section on partitioning.
Your application can still make queries against the parent table ('a' from my example) and get the records for all clients, including any in child tables.
You won't be able to do this with a column constraint. Think you're going to have to write a trigger.
I need to insert a field in a that references an id field in another table.
The id field it is to going is next to the field 'test' (column - codedescription, table typecategory) and coming from an id field next to the word 'assessment' (column categorydescription, table typecategory)
INSERT INTO codetype
(typecategoryid)
Where codedescription='test'
SELECT id FROM typecategory WHERE categorydescription='Assessment Types'
There are plenty of examples of inserting entire columns but nobody has written how to insert a single field from another table.
table - codetype
id bigserial primary key
codedescription varchar
typecategoryid bigint foreign key to typecatogory on the ID column
Table - typecategory
ID big serial primary key
categorydescription varchar
If the column already exists and there are are already records in the rest of the columns in the table, then you need an UPDATE statement, not an INSERT.
Looks like this post might help you: Update a column of a table with a column of another table in PostgreSQL
maybe
UPDATE codetype c
SET c.typecategoryid = t.id
FROM typecategory t
WHERE c.codedescription = 'test' and t.categorydescription='Assessment Types'
I have two tables A and B and need to insert records (few columns not all) from A into B.
Of course I guess I can do:
INSERT INTO B (col2)
SELECT DISTINCT col2
FROM A
However, Col1 in table B (named ID) has a type of INT so it is causing this error:
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'ID', table 'MyDB.dbo.Visitor'; column does not allow nulls. INSERT fails.
How can I make SQL Server ignore this column and just insert the data I need?
Thanks.
A primary key must fulfill two conditions:
It must be unique in the table (that is, any row in the table can be identified by its primary key), and
The fields that are part of the primary key cannot be NULL. That's because allowing NULL values in the primary key will make the uniqueness of the primary key impossible to hold, because a NULL value is non-equal to any other value, including another NULL.
Quoting from here:
A unique key constraint does not imply the NOT NULL constraint in practice. Because NULL is not an actual value (it represents the lack of a value), when two rows are compared, and both rows have NULL in a column, the column values are not considered to be equal. Thus, in order for a unique key to uniquely identify each row in a table, NULL values must not be used.
This should work assuming you don't insert duplicates into B:
INSERT INTO B (col2)
SELECT DISTINCT col2
FROM A
WHERE col2 IS NOT NULL
Set ID column in table B to "auto-increment".
SQL Server will provide automatically unique values for ID column if you define it as IDENTITY
In your case you can calculate the maximum value of ID column and start IDENTITY from the value that exceeds that maximum.
See the accepted answer for SQL Server, How to set auto increment after creating a table without data loss? for such code.
You need to create a relationship between the two tables and do an update statement.
Update table b set valueb = valuea from table a where a.id = b.id
You also need to rethink your design a little bit it sounds like.
I have the following two sql tables:
The GlassesID Column in Glasses table is the primery key defined as auto Increaseble.
The GlassesColor table has GlassesID(not auto Increaseble) colomn that defined as Foreign Key.
When Glasses table get a record (from stored procedure)GlassesID automatecly get value.
The GlassesColor.GlassesID column must be set with value from Glasses.GlassesID Column.
My question is how can i implement this? i,e... How can i set column field with related column field in another table?
Immediately after you insert a record into Glasses table, get the ID from Glasses table
declare #GlassesID as int
select #GlassesID = scope_identity();
Then you can use #GlassesID to insert into GlassesColor table.
insert GlassesColor(GlassesID, .....)
values(#GlassesID, .....);
My table has only a single column calld id. This column is an autoincrementing key (I am using it for sequencing). I want to do something like: insert into sequencer; but this gives me SQL errors because I guess I need to have a values portion. But there are no other columns in the table and I want the id column to be autoincremented. I'd also rather not hack this by just adding another dummy column. Thanks.
INSERT INTO table SET id = NULL;
or
INSERT INTO table VALUES (NULL);
When inserting NULL into an auto-increment column, mysql will generate a new number instead.