sql - Set sort-order field based on alphabetically ordering of another field -
i've added couple of fields tables in database (sql server 2005) allow users customize sort order of rows. i've followed pattern of tables:
-- alter invoicestatus table alter table [dbo].[invoicestatus] add [disabled] bit not null default 0 go alter table [dbo].[invoicestatus] add [sortorder] int not null default 0 go -- use primary key default sort order update [dbo].[invoicestatus] set [sortorder] = [invoicestatusid] go
normally, can see, i've used primary key default sort order. in situation use alphabetical ordering of text field in table default sort order.
using above table example (which has text field [invoicestatusname]
), there similar nice , short query write use alphabetical ordering of [invoicestatusname]
default sort order?
update:
question answered, has pointed out solution might not ideal want add context future references. old system (not legacy-old, has been around quite years) in use handful of different places.
there several lists/drop-downs in application typical "status" type (such invoice status, order status, customer type etc.). when system first written these standard values in use every place (not meant changed in way), users have started request ability add new statuses, remove no longer in use , specify custom sort order (one status might more used, , nice have @ top of list).
the easiest way found (without having mess around of old code) add 2 new fields, disabled
, sortorder
, relevant tables. disabled
field used "hide" un-used types (cannot delete them because of referential integrity, , value hold need kept), , sortorder
field there users can specify own custom sort order. since relevant tables share these same 2 columns, easy make simple interface handle sorting (and disabling) in generic way.
;with ( select sortorder, row_number() on (order invoicestatusname) rn dbo.invoicestatus ) update set sortorder = rn
Comments
Post a Comment