Solved

passing parameter to update correct fileds using Stored Procedure

Posted on 2009-07-03
4
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MongoDB Through a MySQL Lens

This article looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

632 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