Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HI friends,
I stuck on a problem...I have a table EMployeeId and within this I had created 3 rows columns (EmpName, EmpId, EmpADD) and i had inserted 10 columns rows into it having EmpId from 1 to 10. Now my question is i want to select the table EmployeeId in the sequence 5,8,2,3,4,6,7,9,10...
Thank you in advance.
Posted
Updated 11-Oct-14 0:44am
v2
Comments
Estys 11-Oct-14 6:46am    
You need to have some column to sort on. Why that sequence : name? birthdate? What?

1 solution

One way is to get the user preference for the order into a table somehow ... let's say you have a table
SQL
CREATE TABLE UserOrder
(
OrderId INT IDENTITY(1,1),
EmpId INT
);

The EmpId is going to be a foreign key to your EmployeeId table.

How you get those values in there is up to you. Say you pass a comma separated list to a Stored Procedure then see this thread[^] for a way of getting it into the UserOrder table.

Here is an example:

SQL
DECLARE @TheOrder VARCHAR (150)

SET @TheOrder = '5,8,2,3,4,6,7,9'

DECLARE @EmpId VARCHAR

INSERT INTO UserOrder (EmpId)
    SELECT items
    FROM [dbo].[Split] (@TheOrder, ',')

SELECT E.*
FROM EmployeeId E
INNER JOIN UserOrder O ON E.EmpId = O.EmpId
ORDER BY O.OrderId


(N.B. Note the implicit cast from the varchar items returned from the function and the int that is stored on the table.)
 
Share this answer
 
Comments
Rahul Kumar kcnit 11-Oct-14 12:11pm    
As I am new in sql, I am not able to understand this query so please briefly explain the code that how it will work...
CHill60 12-Oct-14 6:31am    
Well if you follow the link I gave you will find a function call Split - all this does is take a string containing a list and return a table containing the individual bits of text as rows in column [items].
Use the results to populate a working table [UserOrder] and join to the original data.
You're not really interested in the contents of UserOrder - you only want to use it to sort on.
Not sure I can say anything else about the code to be honest

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900