Solved

Stored Procedure with parameters in ASP.Net

Posted on 2014-04-27
1
414 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
[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 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…

739 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