Solved

passing parameter to update correct fileds using Stored Procedure

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Show RTF format in an SSRS report 3 37
Star schema daily updates 2 33
MS SQL Server Management Studio R2 4 24
Not listening to where 1 13
I have a large data set and a SSIS package. How can I load this file in multi threading?
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

685 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