March 10, 2010 at 10:55 am
I have a list of ids in a particular order. I am passing this as a comma-delimited string to a stored procedure. I need to update a table and the sort field according to the order in which the ids came in.
for eg.
SortTable
Id SortColumn
100 0
105 1
102 2
104 3
200 4
Now passing in the list of ids to stored proc
ids = '100,102,104,200,105'
Result after stored proc runs
SortTable
Id SortColumn
100 0
105 4
102 1
104 2
200 3
Can you please help me with the SQL?
March 10, 2010 at 4:41 pm
preetshari (3/10/2010)
I have a list of ids in a particular order. I am passing this as a comma-delimited string to a stored procedure. I need to update a table and the sort field according to the order in which the ids came in.for eg.
SortTable
Id SortColumn
100 0
105 1
102 2
104 3
200 4
Now passing in the list of ids to stored proc
ids = '100,102,104,200,105'
Result after stored proc runs
SortTable
Id SortColumn
100 0
105 4
102 1
104 2
200 3
Can you please help me with the SQL?
Sure... can you post your stored procedure, please?
--Jeff Moden
Change is inevitable... Change for the better is not.
March 10, 2010 at 10:48 pm
No need for a Tally table here:
Setup
DECLARE @SortTable
TABLE (
id INTEGER NOT NULL,
sort_column INTEGER NOT NULL
);
INSERT @SortTable (id, sort_column) VALUES (100, 0);
INSERT @SortTable (id, sort_column) VALUES (105, 1);
INSERT @SortTable (id, sort_column) VALUES (102, 2);
INSERT @SortTable (id, sort_column) VALUES (104, 3);
INSERT @SortTable (id, sort_column) VALUES (200, 4);
Solution:
DECLARE @SortString VARCHAR(8000);
SET @SortString = '100,102,104,200,105';
UPDATE ReIndexed
SET sort_column = new_sort_column - 1
FROM (
SELECT ST.id,
ST.sort_column,
new_sort_column =
ROW_NUMBER()
OVER(
ORDER BY
CHARINDEX(
CONVERT(VARCHAR(12), ST.id)
COLLATE LATIN1_GENERAL_BIN,
@SortString))
FROM @SortTable ST
) Reindexed;
-- Show the results
SELECT id, sort_column
FROM @SortTable;
Output
id sort_column
100 0
105 4
102 1
104 2
200 3
Paul
March 10, 2010 at 11:21 pm
That kind of presupposes that you know what order the delimited parameter is going to be in, doesn't it? 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
March 10, 2010 at 11:26 pm
Jeff Moden (3/10/2010)
That kind of presupposes that you know what order the delimited parameter is going to be in, doesn't it? 😀
Nope. My reading of the requirement is that the order of keys in the string defines the ordering in the table. That's what my code does. Have I misunderstood?
March 10, 2010 at 11:52 pm
Paul White (3/10/2010)
Jeff Moden (3/10/2010)
That kind of presupposes that you know what order the delimited parameter is going to be in, doesn't it? 😀Nope. My reading of the requirement is that the order of keys in the string defines the ordering in the table. That's what my code does. Have I misunderstood?
Not from what you said above. I guess I don't understand the purpose of @SortTable in your code. What if the passed in parameter has a value that's not in @SortTable? Maybe I'm the one misunderstanding.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 10, 2010 at 11:54 pm
Heh... never mind. I didn't read the original post carefully enough.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 11, 2010 at 12:03 am
Jeff Moden (3/10/2010)
Heh... never mind. I didn't read the original post carefully enough.
No worries. It is a bit off an odd requirement... kinda makes me doubt that the system in question is a shining example of correct design.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply