Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 select records within a range

Author  Topic 

tariq2
Posting Yak Master

125 Posts

Posted - 2011-01-26 : 08:10:02
Objective:

I am attempting to select all records from a table f0902_aot with a jobkey greater than the maximum jobkey less 5.

I am trying:
DECLARE @maxjobkey varchar(6);
set @maxjobkey = 'select max (jobkey)from f0902_aot'
select * from F0902_AOT where Jobkey > cast(@maxjobkey - 5)

And I receive the following error message:
Msg 1035, Level 15, State 10, Line 3
Incorrect syntax near 'cast', expected 'AS'.

Thank you



MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-01-26 : 08:12:48
DECLARE @maxjobkey int;
set @maxjobkey = select max (jobkey)from f0902_aot;
select * from F0902_AOT where Jobkey > (@maxjobkey - 5);
Go to Top of Page

tariq2
Posting Yak Master

125 Posts

Posted - 2011-01-26 : 08:29:03

Now I receive the following:

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'select'.
Go to Top of Page

tariq2
Posting Yak Master

125 Posts

Posted - 2011-01-26 : 09:28:12
Actually, have worked this out now.
Thank you for your help
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-01-27 : 05:26:18
Did you use this?

DECLARE @maxjobkey int;
set @maxjobkey = (select max (jobkey)from f0902_aot;
select * from F0902_AOT where Jobkey > (@maxjobkey - 5));

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -