?
Solved

How to make parameter value optional in stored procedure

Posted on 2008-11-12
2
Medium Priority
?
345 Views
Last Modified: 2012-05-05
I would like this sp to return all rows where the surname = @name and allow the user to enter specific first name(@Gname or not. if user enters a first name then return all rows that match firstname surname else return all rows where surname matches name.


create proc spHPI

@Gname varchar(30),
@Name varchar(30)
as
      select
      n.[given name],
      n.surname,
      n.[professional ID],
      p.[Common Person Number],
      a.street_address,
      a.city,
      q.qualification
      from name n
      left    outer join address a  on n.[professional id]= a.[professional id]      
            join Professional p   on n.[Professional ID] = p.[Professional ID]
            join qualification q   on q.[Professional ID]= p.[Professional ID]
      where n.surname = @Name
      and n.[given name] = @Gname
      order by n.[given name]
0
Comment
Question by:deNZity
2 Comments
 
LVL 12

Accepted Solution

by:
Dimitris earned 2000 total points
ID: 22946043
here u r
create proc spHPI
@Name varchar(30),
@Gname varchar(30) = null,
as
      select
      n.[given name],
      n.surname,
      n.[professional ID],
      p.[Common Person Number],
      a.street_address,
      a.city,
      q.qualification
      from name n
      left    outer join address a  on n.[professional id]= a.[professional id]      
            join Professional p   on n.[Professional ID] = p.[Professional ID]
            join qualification q   on q.[Professional ID]= p.[Professional ID]
      where n.surname = @Name
      and n.[given name] = isnull(@Gname, n.[given name])
      order by n.[given name]
 
 
 
/* You can call it like 
exec spHPI 'dimitris' or exec spHPI @Name ='dimitris' 
or
exec spHPI 'dimitris','FromGreece' or exec spHPI @Name ='dimitris', @gname ='FromGreece'
 
The second parameter (@gname has a default value of null 
so ig you don't pass  it (first exec method) it uses the null 
 
*/

Open in new window

0
 

Author Closing Comment

by:deNZity
ID: 31516229
Thanks I changed it a bit but just what I wanted.

create proc spHPI
@Name varchar(30),
@Gname varchar(30) = null
as
      select
      n.[given name],
      n.surname,
      n.[professional ID],
      p.[Common Person Number],
      a.street_address,
      a.city,
      q.qualification
      from name n
      left    outer join address a  on n.[professional id]= a.[professional id]      
            join Professional p   on n.[Professional ID] = p.[Professional ID]
            join qualification q   on q.[Professional ID]= p.[Professional ID]
      where n.surname like @Name + '%'
      and n.[given name] like isnull(@Gname + '%', n.[given name])
      order by n.[given name]
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

807 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