Solved

passing parameter to update correct fileds using Stored Procedure

Posted on 2009-07-03
4
188 Views
Last Modified: 2012-05-07
Hello,

I want to pass parameters as number  1   or 2  to 3
to update or select certain field in a table with fields names f1, f2,f3

so that when the value is 2 I want my select statement to be equal to
select f2 from mytable

Best Regards

Zaki
0
Comment
Question by:zaki100
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 24771589
this could do:
select case @parameter WHEN 1 then f1 WHEN 2 THEN f2 WHEN 3 THEN f3 END as field from mytable 

however, the data types must be equivalent (or be made equivalent with CONVERT() .. )

Open in new window

0
 
LVL 9

Assisted Solution

by:Rahul Goel ITIL
Rahul Goel ITIL earned 250 total points
ID: 24771615

declare @parameter as int
set @parameter = 2
select 
    case @parameter 
               WHEN 1 then col1
               WHEN 2 THEN col2
               WHEN 3 THEN col3
    END as field 
from 
    table
 

Open in new window

0
 

Author Comment

by:zaki100
ID: 24771647
Hello,

Both answers are good but I want to make comparison for the selected field such us checking if it is null  or >0 within the same select statement

Thanks

Zaju
0
 
LVL 14

Expert Comment

by:shru_0409
ID: 24771928
select decode(@parameter,1,f1,2,f2,3,f3,'your_comment') as fiels from your_table
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question