Solved

passing parameter to update correct fileds using Stored Procedure

Posted on 2009-07-03
4
167 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 142

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
Rahul Goel 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now