+ Reply to Thread
Page 2 of 2 FirstFirst 1 2
Results 11 to 12 of 12

Thread: Dynamic Stored Procedure without Building Strings

  1. #11
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Seems to work fine for me in SQL Server with NULL values. You'll probably just have to do a conditional in the procedure or use inline SQL.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  2. #12
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    If anyone is interested, this is how I worked it in MySQL. This version allows for NULL values in fields.
    Code:
    CREATE PROC [dbo].[usp_SearchCandidates]
    candidateID int,
    firstName varchar(25),
    lastName varchar(25),
    email varchar(100)
     
    BEGIN
     
    SET @candidateID=candidateID;
    SET @firstName = firstName;
    SET @lastName = lastName;
    SET @email = email;
     
    SELECT
        [candidateID],
        [firstName],
        [lastName],
        [email]
    FROM
        [candidates]
    WHERE (isActive = 1 AND
    candidateID = IF(@candidateID=0,candidateID, @candidateID) AND
    IF(ISNULL(@firstName),NOT ISNULL(firstName),firstName=@firstName) AND
    IF(ISNULL(@lastName),NOT ISNULL(lastName),lastName=@lastName) AND
    IF(ISNULL(@email),NOT ISNULL(email),email=@email)
    )
    
    This works where NULL values are used in field values. Obviously you can toggle the NOT ISNULL to just ISNULL, depending on what you want the Stored Procedure to return if you pass NULL as one of the parameters.

    If you don't use NULL values in your fields, you could try this instead:-
    Code:
    candidateID = IF(@candidateID=0,candidateID, @candidateID) AND
    firstName=IFNULL(@firstName,firstName) AND
    lastName=IFNULL(@lastName),lastName) AND
    email=IFNULL(@email,email)
    
    Hope that helps someone.

+ Reply to Thread
Page 2 of 2 FirstFirst 1 2

Similar Threads

  1. write a procedure.
    By guddu in forum Microsoft SQL Server
    Replies: 7
    Last Post: July 11th, 2008, 11:19 AM
  2. Using Parameterized Stored Procedure
    By richyrich in forum MySQL
    Replies: 14
    Last Post: June 12th, 2008, 08:00 AM
  3. Stored Procedures on MySQL
    By richyrich in forum MySQL
    Replies: 7
    Last Post: June 9th, 2008, 12:15 PM
  4. If statement stored procedure
    By peebman2000 in forum SQL Development
    Replies: 32
    Last Post: May 23rd, 2008, 04:54 PM
  5. Dynamic Stored Procedure
    By jmurrayhead in forum Microsoft SQL Server
    Replies: 16
    Last Post: March 26th, 2008, 12:19 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO