0
CREATE OR REPLACE FUNCTION public.create_user(_name TEXT, _pass TEXT, _iconid INTEGER)
RETURNS user_info
LANGUAGE sql
AS $$
DECLARE
    _newid INTEGER;
BEGIN
    INSERT INTO users(name, password, iconid) VALUES (_name, crypt(_pass, gen_salt('bf')), _iconid);
    SELECT _newid = currval(pg_get_serial_sequence('users', 'id'));


    SELECT u.id, u.name, u.rating, t.name, i.path, i.name FROM users u
    LEFT OUTER JOIN usertypes t ON t.id = u.typeid
    LEFT OUTER JOIN usericons i ON i.id = u.iconid
    WHERE u.id = _newid;
END
$$;

I get:

ERROR: syntax error at or near "INTEGER"
Line 6:    _newid INTEGER;
                  ^

I am sure there's something I'm missing but it's been really frustrating so far because the tools are substandard compared to almost EVERY other major db - mysql, sql server, oracle, etc.

1 Answer 1

3

Why isn't this Postgres function correct?

Because it doesn't contain valid SQL. It appears to contain plpgsql code. Try changing the language:

LANGUAGE plpgsql
Sign up to request clarification or add additional context in comments.

4 Comments

Now the function creates, but I get this error when executing: select * from public.create_user('DerpDerp5678', 'password', 1); ERROR: query has no destination for result data HINT: If you want to discard the results of a SELECT, use PERFORM instead. CONTEXT: PL/pgSQL function create_user(text,text,integer) line 6 at SQL statement
Marking this as the answer, but - doesn't matter at this point. I am sick of wrestling with crappy tools. Back to mySQL where mysterious, corner-case tasks like EDITING A FUNCTION aren't hidden away in dusty corners.
You need a separate question for that error. Although it will probably be a duplicate.
The error message of PostgreSQL is correct, informative and exhausting. You have done a query using SELECT, but not said what to do with the result. What else but an error message can you expect in this situation? But feel free to go back to some crappy database system.

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.