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.
Author |
Topic |
RiyaSen845
Starting Member
3 Posts |
Posted - 2013-03-21 : 01:13:52
|
Hi,I have 5 columns contained in the same table1: C1CODE2: C2CODE3: NAME4: DATE5: STATUSand passing the input parameter @DatabaseName in sp.- Admin- ClientIf I pass 'Admin' , then query should return the following columns1: C1CODE3: NAME4: DATE5: STATUSIf I pass 'ClientJob' , then query should return the following columns2: C2CODE3: NAME4: DATE5: STATUSCan 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 TableNameELSE IF @DatabaseName = 'ClientJob' SELECT C2CODE, NAME, DATE, STATUS FROM TableName[/code]--Chandu |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-21 : 02:07:55
|
no need of IF. you could just doSELECT 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 MVPhttp://visakhm.blogspot.com/ |
|
|
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 doSELECT 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 |
|
|
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? |
|
|
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 |
|
|
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 MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|