SQL insert variables in query

Hey

I have this table:

Table:

Name, ID
BOB, 12345
SARA, 67890

I would like to return:
Select * from table

BOB, IT
SARA, HR

(12345 and 67890 as variables in query - not in a table)
LVL 1
mikeydkAsked:
Who is Participating?
 
Ryan ChongCommented:
can you share your full script?

in general, you may apply case ... when clause to compare your "variables", and then display the output accordingly.

refer to examples showing in:

CASE (Transact-SQL)
https://technet.microsoft.com/en-us/library/ms181765(v=sql.105).aspx
0
 
Ryan ChongCommented:
to make your query more efficient, try to create another mapping table to map your values. like:

AnotherTable
ID, MappedValue
12345, IT
67890, HR

then:

Select a.Name, b.MappedValue
from table a left join AnotherTable b
on a.ID = b.ID

Open in new window

0
 
mikeydkAuthor Commented:
I'm not able to create an extra table. So It have to variables
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Ryan ChongCommented:
try like this:

declare @tmp table
(
	Name varchar(50),
	ID int
)
insert into @tmp
values
('BOB', 12345),
('SARA', 67890);


select Name,
case
	when ID = 12345 then 'IT'
	when ID = 67890 then 'HR'
end MappedValue
from @tmp

Open in new window

0
 
Ryan ChongCommented:
is that last comment useful to you?
0
 
Ryan ChongCommented:
as suggested.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.