Results 1 to 5 of 5

Thread: [RESOLVED] Sql Server 2000 Function, set variable to column value

Threaded View

  1. #1

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Resolved [RESOLVED] Sql Server 2000 Function, set variable to column value

    I've got a table that holds a rate value and an effective date:
    Code:
    ID | EffectiveDate | AdminCost
    1  | 2000-01-01    | 200.00
    2  | 2010-01-01    | 225.00
    3  | 2012-01-01    | 250.00
    I'm trying to make a Function that will return the value of AdminCost column for a single record, I'm able to get that record with this query just fine:
    Code:
    Select Top 1 AdminCost, max(ID)
    from tbl_pwe_tcr_DefaultAmounts
    where EffectiveDate < @EntryDate
    Group By ID, AdminCost
    Which means when the date passed in is between "Jan 1 2010" and "Jan 1 2012" it returns the '225.00' and I'm trying to set that value to a function variable to return it to the Stored Proc that calls this, but when I try set it to a variable, I get an error, here's the function I'm trying to create:
    Code:
    Create Function [dbo].[fn_GetAdminFee] (@EntryDate DateTime)
    Returns numeric(6, 2)
    As
    Begin
    	Declare @Amount numeric(6, 2)
    
    	Select Top 1 @Amount = AdminCost, max(ID)
    	from tbl_pwe_tcr_DefaultAmounts
    	where EffectiveDate < @EntryDate
    	Group By ID, AdminCost
    
    	Return @Amount
    End
    The error I'm getting is:
    Code:
    A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.
    So my question is, how can I assign the the AdminCost column to the @Amount variable?

    I've also tried:
    Code:
    Declare @Amount numeric(6, 2)
    
    Select @Amount = AdminCost From (Select AdminCost, max(ID)
        from tbl_pwe_tcr_DefaultAmounts
        where EffectiveDate < @EntryDate
        Group By ID, AdminCost)
    but that doesn't work either.
    Last edited by JuggaloBrotha; May 16th, 2012 at 03:44 PM.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

Posting Permissions

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



Click Here to Expand Forum to Full Width