casit
asked on
Postgresql function syntax help
I have the following function. I want to call it with a username and then it return a user_id for that user.
CREATE OR REPLACE FUNCTION public.get_user_id (username char) RETURNS int4 AS
$$ SELECT user_id from users where username = '$1' $$
LANGUAGE 'sql'
GO
That doesn't work. when I execute the function.
select public.get_user_id('cmuenc h') as user_id I get NULL back. However when I just do a straight select with the username in the where clause it reuturns a 1 like its supposed to.
CREATE OR REPLACE FUNCTION public.get_user_id (username char) RETURNS int4 AS
$$ SELECT user_id from users where username = '$1' $$
LANGUAGE 'sql'
GO
That doesn't work. when I execute the function.
select public.get_user_id('cmuenc
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Got it to work. I thought I had removed the single quotes around the $1 but it hadn't the first time. I thne reopened the query and deleted them. Thanks for the suggestion. It was driving me crazy
ASKER
SELECT user_id from users where username = 'cmuench' it returns with a 1.