Solved

Stored Procedure with parameters in ASP.Net

Posted on 2014-04-27
1
411 Views
Last Modified: 2014-04-27
I have a stored procedure in SQL Server I am using to search records and return them for display in an asp.net page (c# code behind). I have 3 parameters I need to use. Owner, Year and Type. By default the page opens to all owners for the current year of both types. Using drop down lists, users can select which owner, what year and which type to return. There are 12 owners, with data from each year for 2009-2014. There are two types to select from. I'm trying to create the stored procedure to select 1 owner OR all owners for a particular year, with either type 1 or type 2. I think I need an if statement or case statement in the stored procedure, but I don't know where to look to find examples. Am I approaching this right, and is there a good example to work with?
0
Comment
Question by:dodgerfan
1 Comment
 
LVL 8

Accepted Solution

by:
Ganapathi earned 500 total points
ID: 40026495
CREATE TABLE TMP(OWNER VARCHAR(10),YEAR_VAL DATETIME, OWNER_TYPE INT)
INSERT INTO TMP SELECT 'OWNR1',GETDATE(),8
INSERT INTO TMP SELECT 'OWNR2',GETDATE(),6
INSERT INTO TMP SELECT 'OWNR3',GETDATE(),4
INSERT INTO TMP SELECT 'OWNR4',GETDATE(),3

DECLARE @TYPE_PARAM INT /* Consider this as the Paramater */
, @OWNER_PARAM CHAR(5)
SELECT @TYPE_PARAM = 2
SELECT @OWNER_PARAM = 'OWNR3'
SELECT
	*
FROM
	TMP
WHERE
   (2 = CASE
   		   WHEN @TYPE_PARAM = 2
		      THEN 2
		   ELSE 1
		END)
OR (@TYPE_PARAM = 1 AND OWNER = @OWNER_PARAM)

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

809 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