Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

stored procedure with optional parameters

Posted on 1998-09-20
1
Medium Priority
?
223 Views
Last Modified: 2010-03-19
i want to build a stored procedure with some parameters, if one ore more of the parameters isn't given then it should not be included in the procedure ( 'and <column name> = 'value' should be ignored ), I know it is possible but i don't know how.
0
Comment
Question by:skysoft
1 Comment
 
LVL 7

Accepted Solution

by:
Victor Spiridonov earned 400 total points
ID: 1090204
You do not need to specify parameter if you specify default when you define your stored procedure:
Simple example:
CREATE PROCEDURE test @optional_param varchar(10)="none"
as
IF @optional_param="none"
 SELECT * from t1
ELSE
 SELECT @ from t1
  WHERE col1=@optional_param
IF you have multiple optional parameters you will have to first build your SQL string and then use EXEC to execucte it.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

916 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