Solved

sql server 2008 query

Posted on 2013-06-26
1
394 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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.
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now