0

I thought this would be a simple task but i cannot figure out why this gives me an error in my syntax. Any help is appreciated.

DECLARE @usernameid VARCHAR(20);
declare @UserIDParam VARCHAR(20);
SET @usernameid = 'myid';
SET @UserIDParam =
   (SELECT userid
      FROM tblusers
     WHERE unid = usernameid);

SELECT *
 FROM tblusers
 WHERE tblusers.userID = @useridparam
2
  • how about you show us your error Commented May 9, 2012 at 18:36
  • As an aside, if that is really your entire code, why don't you just reduce the whole thing to a single SELECT query with a SQL join? Commented May 9, 2012 at 18:40

1 Answer 1

1

One doesn't DECLARE user variables: one just uses them. (You DECLARE local variables e.g. in a procedure).

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

6 Comments

can i get an example how to change this?
@Mike: Just entirely remove the two DECLARE statements.
@Mike: In your comment to Gratzy's answer, you said the error was on the DECLARE statement - how can you obtain the same error if the DECALRE statements aren't there?
eggyal oh you mean remove the entire line, okay i got past that not i its at Unknown system variable 'usernameid'
@Mike: You need to prefix your reference to usernameid with @ in your first SELECT statement.
|

Your Answer

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