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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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 ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
is that last comment useful to you?
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
as suggested.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.