Home > Software design >  Select all rows but park on specific
Select all rows but park on specific

Time:02-11

I need to show all rows in a table but park on a specific one.

Select * from t_table 

will show me all but park on the top.

select * from t_table where id=1000 

will only show me id=1000.

what I need is

select * from t_table locate id=1000

is this possible ?

Example : enter image description here

If the storedprocedure showing this select has a parameter @PartCode. This can be NULL (show all) of a certain partcode. f.e. 2000443

The select in the stored procedure should park on this partcode but not filter enter image description here

CodePudding user response:

To have your required "partcode" appear at the top of the result set you will need to pass its value as a parameter to your procedure, so assuming this is @PartCode

<your select query>
order by
  case when t.Partcode = @PartCode then 0 else 1 end, <other criteria>

CodePudding user response:

use top 1

select top 1 * from t_table order by id desc

CodePudding user response:

You could achieve this with a case statement in an order by. It would always return your specific record as row 1 but would still order by other IDs below.

Something like the following should work:

ORDER BY
   CASE Partcode
      WHEN 2000443 THEN 1 --2000443 being the ID to return first.
      ELSE 2
   END, id
  • Related