Link to home
Start Free TrialLog in
Avatar of casit
casitFlag for United States of America

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('cmuench') 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.
ASKER CERTIFIED SOLUTION
Avatar of m1tk4
m1tk4
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of casit

ASKER

Still nothing.  Does it matter how my username is stored in the users table?  The weird thing is if I do
SELECT user_id from users where username = 'cmuench'  it returns with a 1.
Avatar of casit

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