0

I want to create a function that loops through a table and checks if a specific word exists in a text field. If it does, the function should set a Boolean field true. This is what I've come up with. I guess my execute command is wrong?

CREATE OR REPLACE FUNCTION insertCarBooleans()
Returns void
AS $$
DECLARE
    location RECORD;
BEGIN
    FOR location IN SELECT * FROM Ownlocations ORDER BY id LOOP
    EXECUTE
        'UPDATE Ownlocations SET Ford = true
        WHERE 
        cars LIKE 'Ford';       
    END LOOP;
RETURN;
END;
$$
LANGUAGE plpgsql;

SELECT insertCarBooleans()
3

Maybe I'm missing something, but why do you need a function for this? And why the loop?

UPDATE own_locations SET ford='t' WHERE cars LIKE '%Ford%';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.