C# .NET - sql server simple query - Asked By sara asa on 04-May-12 03:28 PM

hi
i have a table as users with this field(id ,name,family,age)mt id and age  are int and name and family nvarchar(50)
i tried with design query in editory select name and family with parameter age..under window it show me as this

@age int

SELECT


name, family, age

FROM users

WHERE (age = @age)


ofcourse i set @age int  with my hand not editor but it show me syntax error
like this

Msg 137, Level 15, State 2, Line 3

Must declare the scalar variable "@age".


ofcourse i decalre id as int and auto number age as int and name and family as nvarchar(50)

how can i solve thissimple query error..i guess ididnt make any mistake...plz help me..i guess it should be more simple in editor but it was n't..

ty

Robbe Morris replied to sara asa on 04-May-12 06:01 PM
declare @age int

set @age = 18

select *
  from tableA
 where age = @age
Venkat K replied to sara asa on 04-May-12 11:15 PM
You need to use the DECLARE Keyword when declaring variables:
Modify the code as below:

DECLARE @age INT
SELECT Name, Family, Age FROM Users WITH(NOLOCK) WHERE age=@age

Thanks

sara asa replied to Robbe Morris on 05-May-12 06:40 AM
ty
sara asa replied to Venkat K on 05-May-12 06:41 AM
ty