Sql Insert with dynamic values - sql

I am using following query on postgres database:
insert into user_settings (google_access_token, google_refresh_token)
select 'google_access_token', 'google_refresh_token' from user
where id = user_id
table user_settings has a field user_id which corresponds to field named id in user table. but this gives the error:
column "user_id" does not exist
Hint: There is a column named "user_id" in table "user_settings",
but it cannot be referenced from this part of the query

you are taking a column in user_settings and inserting into that table with condition.
I think you have to use update statement because your condition is
user .id = user_settings .user_id

I guess you need to do like this :
insert into user_settings (google_access_token, google_refresh_token)
select 'google_access_token',
'google_refresh_token'
from user
where id in (select user_id from user_settings )

You can update a table with out NULLs even if you don't have matching values
update user_settings
set
user_settings.google_access_token='google_access_token',
user_settings.google_refresh_token='google_refresh_token'
from user_settings
inner join user
on user_settings.user_id=user.id

Related

SQL: Update registries generates Constraint violation

I have a table like this:
What i want to do is delete the first letter in Username column if this is an 'U'.
I have a problem if i do:
UPDATE table
SET USERNAME= SUBSTR(USERNAME, 2, LENGTH(USERNAME))
WHERE USERNAME LIKE 'U%';
ORA-00001: unique constraint (SPE.UQ_P_USUARIO_NOMBRE) violated.
As you can see in the example, in the table exist the registry 1111-A, and when U1111-A is modifying the error appears.
I want to generate a query which modify just the registries that dont exist in the table.What can i do?
If what you want is to update just the rows in which your update won't violate unique constraint you can use this
UPDATE `table`
SET USERNAME= SUBSTR(USERNAME, 2, LENGTH(USERNAME))
WHERE USERNAME LIKE 'U%'
AND NOT EXISTS(SELECT 1 FROM `table` t WHERE t.USERNAME = SUBSTR(`table`.USERNAME, 2, LENGTH(`table`.USERNAME)) AND t.NAME = `table`.NAME);
This line checks if any rows with username-name pair you are trying to create already exist and if they do this row is not updated
NOT EXISTS(SELECT 1 FROM `table` t WHERE t.USERNAME = SUBSTR(`table`.USERNAME, 2, LENGTH(`table`.USERNAME)) AND t.NAME = `table`.NAME)
You can use the below
UPDATE table a
SET USERNAME= SUBSTR(USERNAME, 2, LENGTH(USERNAME))
WHERE USERNAME LIKE 'U%'
AND NOT EXISTS (SELECT 1 FROM table b
where b.username=SUBSTR(a.USERNAME, 2, LENGTH(a.USERNAME)
AND a.name=b.name)

Merge/Update records from the same table

I have two recordsets from a single table:
SELECT * FROM userconfig WHERE userid = 'user1'
AND
SELECT * FROM userconfig WHERE userid = 'user2'
I would like to update or merge (whatever is easier) one column from a specific user's recordset into the recordset of the other user. IE: user1 has a column configvalue whose values I want to insert into user2's configvalue column. I need both columns to have the same value. What is the simplest way to achieve this?
Update
user2.userkey = user1.userkey,
user2.uservalue = user1.uservalue
FROM
MyTable user2 INNER JOIN MyTable user1 ON user1.YourKey = user2.YourKey
The same table, MyTable (replace with your table name). Join this table back to itself based on the YourKey column (replace YourKey with the column where the join matches).
Then simply assign the alias values of user1 into user2. Aliases and joining back to the same table is the key.

SQL Server : updating one column and inserting if not exist

I have a table TableKats that looks like this:
ID - int
Name - varchar
KatID - int
What I want to do is to update the column Name from another table, and if there is a name in the other table that doesn't exist in TableKats, it should insert it and give KatID a 0
Does anybody know a way to do that? Thanks
you can do it using MERGE, as your other table schema is not known assuming Name as the column in other table too
MERGE TableKats T
USING ( SELECT * from TableB) AS S
ON T.Name = S.Name
WHEN NOT MATCHED THEN
INSERT ( Name, KatID)
VALUES ( S.Name, 0)
WHEN MATCHED THEN
UDPATE -- Not clear what needs to be updated.

SQL insert data into a table from another table

I'm having a problem trying to insert some values into a table. I made an empty table with the fields
id(primary key)
association_id
resource_id
I have another table with
resource_id
association_id
and another one with
id(coresponding to the association_id in the former one)
image
I want to insert the resource_id and association_id from the first populated table, where the image field of the coresponding id from the last table is not empty.
I tried this:
INSERT IGNORE INTO `logo_associations` (``,`association_id`,`resource_id`)
SELECT
``,
`a`.`association_id`,
`a`.`resource_id`
FROM doc24_associations_have_resources a
Join doc24_associations An on a.association_id = An.id
WHERE An.image<>''
but it does not work
Try this:
INSERT INTO logo_associations (association_id, resource_id)
SELECT a.association_id
,a.resource_id
FROM doc24_associations_have_resources a
LEFT JOIN doc24_associations an ON a.association_id = an.id
WHERE an.image IS NULL -- check for null with left join
This is valid for SQL Server. You do not need to select and insert the first column as it is an identity as you mention.
My experience is based on SQL Server but the SQL may be very similar
INSERT INTO DestinationTable
(association_id, resource_id)
SELECT LNK.assocication_id,
LNK.resource_id
FROM LinkTable AS LNK
INNER JOIN ImageTable AS IMG ON IMG.id = LNK.association_id
AND IMG.image IS NOT NULL
Above I assume the following:
Tables are named DestinationTable, LinkTable, and ImageTable respectively
In DestinationTable the primary key (id) is auto generated

how to insert data in table using select statement in sql server

I have two tables Login table and userinfo table. UserInfo table is primary key(UserId) table and login table is foreign key(UserId) table. So my problem is when i insert data in login table value of userid column should come from Userinfo table and value of other columns of log in table like username and password should be inserted directly . Is it possible in single insert statement.
i did the following but it didnt work
insert into login(Userid,username,password)
values(select max(userid) from userinfo,sumit,sumit123)
insert into login(Userid,username,password)
values((select max(userid) from userinfo),'sumit','sumit123');
insert into login (Userid, username, password)
select max(userid), 'sumit', 'sumit123'
from userinfo
[Please note: while that is syntactically correct, I probably wouldn't do it that way.]
Have you tried using a inner JOIN?
INSERT INTO Insurance (Name)
SELECT Employee.Username FROM Employee
INNER JOIN Project ON Employee.EmployeeID = Project.EmployeeID
WHERE Project.ProjectName = 'Hardwork';