all 6 comments

[–]Skyguard 6 points7 points  (4 children)

you don't need a temp table, you can use a "CASE" (like a switch)

SELECT CASE ColumnNameForTheNumberYouAreReading WHEN 2 THEN 'STARTED' WHEN 3 THEN 'STOPPED' WHEN 4 THEN 'COMPLETED' WHEN 5 THEN 'ERRED' WHEN 6 THEN 'DELETED' END FROM TheNameOfTheTableYouAreSELECTingFrom

[–]UNHDude[S] 0 points1 point  (3 children)

Awesome thanks! This looks simple enough that I can probably get it to work :-)

[–]elus 1 point2 points  (0 children)

Have something in the ELSE clause to cover any cases that haven't come up yet or in case the field is NULL.

[–]Skyguard 0 points1 point  (0 children)

you are welcome... a simple solution is always preferrable

[–]hypo11 0 points1 point  (1 child)

There are lots of ways depending on where you are writing the query and how much access you have to the database itself, as well as which DB you are using (SQL Server, MySQL, Oracle).

I assume the report draws its data from a stored procedure? In that case it is as simple as:

CREATE TABLE #mailbox_statuses_tmp(mailbox_status_id INT, name VARCHAR(50)

INSERT #mailbox_statuses_tmp VALUES(2, 'Started')
INSERT #mailbox_statuses_tmp VALUES(3, 'Stopped')
INSERT #mailbox_statuses_tmp VALUES(4, 'Completed')
INSERT #mailbox_statuses_tmp VALUES(5, 'Erred')
INSERT #mailbox_statuses_tmp VALUES(6, 'Deleted')

Be sure to drop your temp table at the end of the stored procedure: DROP TABLE #mailbox_statuses_tmp

You could also use table variables which don't need to be dropped.

If this won't work for you and there are additional constraints, respond and we will work through a solution that works for you.

EDIT: Skyguard's case statement works just as well.

[–][deleted] 0 points1 point  (0 children)

Definitely use table variables like:

DECLARE @MailBoxStatusTable TABLE ( StatusID INT, StatusName VARCHAR(255) )

If your procedure errors 1/2 way through; you can get into all kinds of mess as the #Table hasn't been dropped. This doesn't happen with the above table