2

In my project I want data from same table in 2 different manner. 1) Only 5 fields 2) Only 10 fields And I am getting the data through Stored Procedure for each one. But I want that if possible I should make one procedure.

So, can I put if condition for selecting column?

For example, if I pass parameter as "Less", it will get data of only 5 columns and if I pass parameter as "More", it will get data of 10 columns. I can have two SELECT statement in procedure based on condition(that I have already done) but I want to make it One SELECT statement. Is it possible?

Dhwani
  • 7,484
  • 17
  • 78
  • 139
  • Okie... Actually I refered this post. So I thought might be there is a way. http://stackoverflow.com/questions/4688782/use-the-if-else-condition-for-selecting-the-column-in-mysql – Dhwani Feb 12 '13 at 09:59

2 Answers2

1

This isn't possible.

Your current solution of having an IF statement in the stored procedure is the best approach.

Curtis
  • 101,612
  • 66
  • 270
  • 352
1

No, is it not possible to have a single SELECT statement.

But then, you can do this:

if @ColumnList = 'less'
begin
    call storedProc_returning5Columns
end
else
begin
    call storedProc_returning10Columns
end

[Another option is to use Dynamic TSQL]

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • Actually I have other parameters also for deciding SELECT Query and My Store procedure has almost 8 select statement so I though if I can have better approach. – Dhwani Feb 12 '13 at 10:03
  • even better this way then. Code will be more understandable and more maintainable. – Mitch Wheat Feb 12 '13 at 10:04