Solved

sql server 2008 query

Posted on 2013-06-26
1
432 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
[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
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 SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

732 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