Solved

passing parameter to update correct fileds using Stored Procedure

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is needed to become a DBA? 7 56
SQL Simple Query Taking a Very Long Time 11 37
Datatable / Dates ? 4 32
learning MS SSIS 13 21
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

738 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