Solved

sql server 2008 query

Posted on 2013-06-26
1
427 Views
Last Modified: 2013-06-26
I'm using Sql server 2008.
This example uses the Employees table from the Northwind database.
I have a asp.net page where I have a RadioButtonList.

The RadioButtonList has 3 possible selections.

One is called Query 1 which will have a value of 1
the other is called Query 2 which will have a value of 2
and the other is called Query 3 which will have a value of 3



How would I write a query to handle the following:

If the Query 1 radio button is selected run this query

SELECT [EmployeeID],[LastName],[FirstName],[Title],[TitleOfCourtesy],[BirthDate],[HireDate],[Address],[City],[PostalCode],[Country],[HomePhone]
FROM [Northwind].[dbo].[Employees] WHERE [EmployeeID] < 4
 

If the Query 2  radio button is selected run this query

SELECT [EmployeeID],[LastName],[FirstName],[Title],[TitleOfCourtesy],[BirthDate],[HireDate],[Address],[City],[PostalCode],[Country],[HomePhone]
FROM [Northwind].[dbo].[Employees] WHERE [EmployeeID] > 4


If the Query 3  radio button is selected run this query

SELECT [EmployeeID],[LastName],[FirstName],[Title],[TitleOfCourtesy],[BirthDate],[HireDate],[Address],[City],[PostalCode],[Country],[HomePhone]
FROM [Northwind].[dbo].[Employees] WHERE [EmployeeID] = 6

Can I just use a case statement?

This query is going to go inside a stored procedure that I am just going to pass in the parameter selected from the radion button to the stored procedure.

So the stored procedure will only run one of these queries depending on what parameter I passed it.
So if I passed it a value of 1 then query 1 should run
     if I passed it a value of 2 then query 2 should run
     if I passed it a value of 3 then query 3 should run

Any ideas?

Thanks.
0
Comment
Question by:maqskywalker
1 Comment
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 500 total points
ID: 39280026
The following should do that I think, I've written for testing, you just change the var to be the SP parameter.

declare @InputValue int

set @InputValue = 1

SELECT [EmployeeID],[LastName],[FirstName],[Title],[TitleOfCourtesy],[BirthDate],[HireDate],[Address],[City],[PostalCode],[Country],[HomePhone]
FROM [Northwind].[dbo].[Employees]
WHERE (@InputValue = 1 and [EmployeeID] < 4)
or (@InputValue = 2 and [EmployeeID] > 4)
or (@InputValue = 3 and [EmployeeID] = 6)

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

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