Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - SQL Server Stored Proc Help Needed
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

SQL Server Stored Proc Help Needed

 Post Reply Post Reply
Author
Gary View Drop Down
Senior Member
Senior Member
Avatar

Joined: 20 March 2002
Location: United Kingdom
Status: Offline
Points: 326
Post Options Post Options   Thanks (0) Thanks(0)   Quote Gary Quote  Post ReplyReply Direct Link To This Post Topic: SQL Server Stored Proc Help Needed
    Posted: 20 January 2004 at 4:57am

Hi,

Working with asp and SQL Server, I want to have a dynamic stored proucedure that can accept 2 values, but will also work with 1 value...

CREATE PROCEDURE [mySP]
    @value1 nvarchar(7),
    @value2 nvarchar(7)
AS
    SELECT *
    FROM myTable
    WHERE [col1] = @value1 AND [col2] = value2

Easy enough if both values are provided (eg mySP 'x', 'y'). However, I may not be passing the second value (eg mySP 'x'), but then the sp throughs an error as it expects 2 values.

How can this be achieved ???
Thx

Back to Top
gavinc View Drop Down
Newbie
Newbie


Joined: 20 November 2003
Location: Wales
Status: Offline
Points: 14
Post Options Post Options   Thanks (0) Thanks(0)   Quote gavinc Quote  Post ReplyReply Direct Link To This Post Posted: 20 January 2004 at 9:21am
Originally posted by Gary Gary wrote:

CREATE PROCEDURE [mySP]
    @value1 nvarchar(7),
    @value2 nvarchar(7)
AS
    SELECT *
    FROM myTable
    WHERE [col1] = @value1 AND [col2] = value2

What you can do is to assign a default value to the optional parameter:

CREATE Procedure sp_MyProc @Val1 varchar(5), @Val2 varchar(5) = NULL AS etc....

but you will probably have to change the code of the procedure so that it runs one of two select queries depenind on the value of the option parameters since i'm not 100% sure if you can say:

WHERE Col1 = @Val1 AND Col2 = @Val2 if @Val2 is NULL

I think you will have to say:

WHERE Col1 = @Val1 AND Col2 IS NULL

 

Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post Posted: 20 January 2004 at 12:53pm
Try

CREATE PROCEDURE [mySP]
    @value1 nvarchar(7),
    @value2 nvarchar(7) = NULL
AS
    SELECT *
    FROM myTable
    WHERE [col1] = @value1 AND [col2] = value2

Back to Top
Flamewave View Drop Down
Senior Member
Senior Member
Avatar

Joined: 19 June 2002
Location: United States
Status: Offline
Points: 376
Post Options Post Options   Thanks (0) Thanks(0)   Quote Flamewave Quote  Post ReplyReply Direct Link To This Post Posted: 21 January 2004 at 12:18am


CREATE PROCEDURE dbo.mySP
    @value1 nvarchar(7),
    @value2 nvarchar(7)
AS
If @value2 is null or @value2=''
    Select * From myTable Where col1 = @value1
Else
    Select * From myTable Where col1 = @value1 and col2 = @value2

Then if you dont want to use the second value, pass it as NULL
exec dbo.mysp 'value1', null



Edited by Flamewave
- Flamewave

They say the grass is greener on the other side, but if you really think about it, the grass is greener on both sides.
Back to Top
psycotik View Drop Down
Groupie
Groupie


Joined: 27 November 2003
Status: Offline
Points: 73
Post Options Post Options   Thanks (0) Thanks(0)   Quote psycotik Quote  Post ReplyReply Direct Link To This Post Posted: 21 January 2004 at 1:55am

CREATE PROCEDURE [mySP]
    @value1 nvarchar(7),
    @value2 nvarchar(7) = Null
AS
    SELECT *
    FROM myTable
    WHERE [col1] = @value1 AND ([col2] = value2 OR @value2 is Null)

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.