Solved

sql server 2008 query

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article I will describe the Detach & Attach 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.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

815 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

11 Experts available now in Live!

Get 1:1 Help Now