0

I hate asking about little stupid bugs like this, but I can't figure it out for the life of me...

This query:

INSERT INTO user_authentication(init_password_setup) VALUES( substr((md5(random()::TEXT)), 0, 10) ) WHERE (init_password_setup = NULL OR init_password_setup ='')

Is throwing this error:

 16:27:11 Kernel error: ERROR:  syntax error at or near "WHERE"

I tried running it as an insert select as well. Sincere thanks for any help it is greatly appreciated

1
  • sorry, guys. very dumb. not sure what I was thinking. late in the day. Thanks for the quick help. Commented Aug 19, 2015 at 23:34

4 Answers 4

4

INSERT statement doesn't have WHERE clause. But you can use INSERT ... SELECT statement:

INSERT INTO user_authentication (user_id, init_password_setup) 
    SELECT id, substr((md5(random()::TEXT)), 0, 10)
    FROM users 
    WHERE <some condition here>;

or just update existed records:

UPDATE user_authentication
SET init_password_setup = substr((md5(random()::TEXT)), 0, 10)
WHERE init_password_setup IS NULL OR init_password_setup ='';
Sign up to request clarification or add additional context in comments.

Comments

3

If you want to modify an already existing row, you need an UPDATE statement not an INSERT statement.

Update will modify the existing row(s), Insert will add a new row in the table.

UPDATE user_authentication
  SET init_password_setup = substr((md5(random()::TEXT)), 0, 10)
WHERE init_password_setup IS NULL 
   OR init_password_setup =''

Comments

1

An insert statement has no where clause. Either insert new data or update existing data with the update clause (which can have a where clause).

1 Comment

INSERT INTO ... SELECT ... WHERE is perfectly valid. The problem is that you cannot use a WHERE clause in a VALUES set-expression. That said, I agree that the user appears confused and seems to be trying to use INSERT to UPDATE a row.
1

You can't do INSERT with a WHERE clause. If you are trying to INSERT, then just use VALUES. If you want to use where then use UPDATE.

1 Comment

This is mistaken for the same reasons as @jurgend's answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.