Results 1 to 4 of 4
  1. #1
    RayMilhon is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,066

    Sql Server Function


    I need to create a SQL Server Function that will query a table based on 2 parameters. If no Data is found it needs to requery with a single parameter. Something like
    Code:
    Select apd from ipr where ipr.provid = @param1 and ipr.lob = @param2
    
    If apd is null Then Select apd from ipr where ipr.lob = @param2 and ipr.provid is null
    
    return apd
    I'm just not sure of the correct syntax to be able to do this.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    You need it to be a function rather than a stored procedure? You can select a variable equal to the first SQL, then test it for Null. If Null set the variable to the second SQL and then return the variable.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    I got bored and tried it. Here are the key bits from my test:

    Code:
    Begin
      Declare @CoDesc varchar(20)
    
      SELECT @CoDesc = (SELECT ...) 
    
      IF @CoDesc Is Null
        SELECT @CoDesc = (SELECT ...)
    
      Return @CoDesc
    
    End
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,035
    You can do it simply in a query without function or procedure see next example where I did it for my cities table;

    Code:
    declare @param1 varchar(max)='Gant'
    declare @param2 varchar(max)='Gent';
    
    With qry1(ID1,Zip1)as (select citID, citPC from [dbo].[tblCities] where citName = @param1),
    qry2 (ID2,Zip2)as (select citID, citPC from [dbo].[tblCities] where citName = @param2)
    Select coalesce(Zip1, Zip2)as Result from qry1 full outer join qry2 on qry1.ID1 = qry2.ID2 
    
     

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Upgrade from SQL Server 2008 to SQL Server 2014
    By RayMilhon in forum SQL Server
    Replies: 4
    Last Post: 07-12-2017, 04:54 PM
  2. Mailing works for PRD server but not for DEV server
    By violeta_barajas in forum Access
    Replies: 0
    Last Post: 11-09-2016, 12:34 PM
  3. SQL Server Function
    By RayMilhon in forum SQL Server
    Replies: 6
    Last Post: 07-02-2015, 03:46 PM
  4. SQL Server function needs permissions
    By EuniceH in forum SQL Server
    Replies: 2
    Last Post: 07-03-2014, 10:43 AM
  5. Date function in Terminal Server
    By eww in forum Access
    Replies: 8
    Last Post: 11-16-2010, 04:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums