Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to return column based on the input parameter

Author  Topic 

RiyaSen845
Starting Member

3 Posts

Posted - 2013-03-21 : 01:13:52
Hi,

I have 5 columns contained in the same table

1: C1CODE
2: C2CODE
3: NAME
4: DATE
5: STATUS

and passing the input parameter @DatabaseName in sp.

- Admin
- Client

If I pass 'Admin' , then query should return the following columns

1: C1CODE
3: NAME
4: DATE
5: STATUS

If I pass 'ClientJob' , then query should return the following columns

2: C2CODE
3: NAME
4: DATE
5: STATUS

Can anyone help me out on this?

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-03-21 : 01:34:05
[code]
DECLARE @DatabaseName VARCHAR(20) = 'Admin'

SELECT CASE @DatabaseName WHEN 'Admin' THEN C1CODE WHEN 'ClientJob' THEN C2CODE END As CodeByInputParam , NAME, DATE, STATUS
FROM TableName

--Alternate
IF @DatabaseName = 'Admin'
SELECT C1CODE, NAME, DATE, STATUS
FROM TableName
ELSE IF @DatabaseName = 'ClientJob'
SELECT C2CODE, NAME, DATE, STATUS
FROM TableName
[/code]

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-03-21 : 02:07:55
no need of IF. you could just do

SELECT CASE @DatabaseName WHEN 'Admin' THEN C1CODE WHEN 'ClientJob' THEN C2CODE END, NAME, DATE, STATUS
FROM TableName


I hope C1CODE AND C2CODE are of compatible datatypes.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-03-21 : 02:12:21
quote:
Originally posted by visakh16

no need of IF. you could just do

SELECT CASE @DatabaseName WHEN 'Admin' THEN C1CODE WHEN 'ClientJob' THEN C2CODE END, NAME, DATE, STATUS
FROM TableName

I hope C1CODE AND C2CODE are of compatible datatypes.


Yes visakh.. thats why i have given two possible solutions for OP

--
Chandu
Go to Top of Page

RiyaSen845
Starting Member

3 Posts

Posted - 2013-03-21 : 18:55:12
Thanks guys, It worked!!

I also want to hide the column if value of C1CODE is NULL and display only NAME,DATE and STATUS.

Any idea on this?
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-03-22 : 00:57:27
Means all rows for C1CODE is NULL then you want to hide that column from display....?

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-03-22 : 01:00:27
quote:
Originally posted by RiyaSen845

Thanks guys, It worked!!

I also want to hide the column if value of C1CODE is NULL and display only NAME,DATE and STATUS.

Any idea on this?


Thats a presentation requirement and has to be dealt at your front end application.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -