Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

stored procedure with optional parameters

Posted on 1998-09-20
1
Medium Priority
?
220 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
[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 7

Accepted Solution

by:
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

715 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