• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • Last Modified:

Calling a User Defined Function with several parameters as a DEFAULT for a column

I have created the table below.  The field, DOM, is supposed to hold a cumulative value, i.e., the sum of all the values of  "DaysInThisStatus" meeting the condition [Status] in (5, 30) And MLSNUM = @MLSNUM And ChangeDate <= @ChangeDate

I have created a function to return DOM. However, I am not sure how to pass the current record's MLSNUM, ListingDate, ChangeDate, Status, etc. as parameters to the function (see below).

Thank you in advance.


CREATE TABLE [dbo].[tblListingPropertyDOM_CDOM](
      [idListingPropertyDOM_CDOM] [int] IDENTITY(1,1) NOT NULL,
      [MLSNUM] [varchar](20) NOT NULL,
      [ListDate] [datetime] NOT NULL,
      [ChangeType] [varchar](20) NULL,
      [ChangeDate] [datetime] NULL,
      [Status] [tinyint] NULL,
      [DateStatusStart] [datetime] NULL,
      [DateStatusEnd] [datetime] NULL,
      [DaysInThisStatus]  AS (case when [DateStatusEnd] IS NULL then datediff(day,[DateStatusStart],getdate()) else datediff(day,[DateStatusStart],[DateStatusEnd]) end),
      [DOM] [int] NULL,
 CONSTRAINT [PK_tblListingPropertyDOM_CDOM] PRIMARY KEY CLUSTERED
(
      [idListingPropertyDOM_CDOM] ASC
) ON [PRIMARY]

GO
******************

-- =============================================
CREATE FUNCTION fnCalculateDOM
(
      -- Add the parameters for the function here
      @MLSNUM varchar(20), @ListingDate datetime, @Status tinyint, @ChangeDate datetime
)
RETURNS int
AS
BEGIN
      -- Declare the return variable here
--MLSNUM    ListDate  ChangeType   ChangeDate   Status DateStatusStart   DateStatusEnd   DaysInThisStatus DOM
      DECLARE @DOM int

      -- Add the T-SQL statements to compute the return value here
      SELECT @DOM = Sum(DaysInThisStatus)
      From dbo.tblListingPropertyDOM_CDOM
      Where [Status] in (5, 30) And MLSNUM = @MLSNUM And ChangeDate <= @ChangeDate
--  print @DOM
      -- Return the result of the function
      RETURN @DOM

END
GO
0
dteshome
Asked:
dteshome
  • 5
  • 2
  • 2
  • +1
1 Solution
 
indianguru2Commented:
Is the function already created?
If yes, try this statement for a sample.

select *, dbo.fnCalculateDOM(MLSNUM, ListDate, Status, ChangeDate)   from tblListingPropertyDOM_CDOM
0
 
dteshomeAuthor Commented:
The function is already created.
The stmt works when exec fom query window and the function returns what I want.
What I need is to create a default that calls the function and use the returned int as the value of DOM field for each record.

0
 
SharathData EngineerCommented:
>> What I need is to create a default that calls the function and use the returned int as the value of DOM field for each record
What do you mean by a default call? Are you looking for a trigger?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
dteshomeAuthor Commented:
In the create table statement, the DOM field will be defined as follow ...

CREATE TABLE [dbo].[tblListingPropertyDOM_CDOM](
      [idListingPropertyDOM_CDOM] [int] IDENTITY(1,1) NOT NULL,
      ...
      ...
      [DOM] INT NULL Default( dbo.fnCalculateDOM()),
      ...
)

The problem here is that the function has these (MLSNUM, ListDate, Status, ChangeDate) parameters the values of which comes from the current record.

I hope I am clear.

Thanks.

0
 
SharathData EngineerCommented:
While creating the table, you can pass the other columns to the function.
CREATE TABLE [dbo].[tblListingPropertyDOM_CDOM](
      [idListingPropertyDOM_CDOM] [int] IDENTITY(1,1) NOT NULL,
      ...
      ...
      [DOM] INT NULL Default( dbo.fnCalculateDOM(MLSNUM, ListDate, Status, ChangeDate)),
      ...
)

Open in new window

When you are inserting the records into the table, DOM will be automatically calculated based on the values.
0
 
dteshomeAuthor Commented:
Does not work, returns error:

Msg 128, Level 15, State 1, Line 11
The name "MLSNUM" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.


0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
please clarify the compatibility version of the database
0
 
dteshomeAuthor Commented:
I am running
Microsoft SQL Server 2005 - 9.00.4053.00 (X64)   May 26 2009 14:13:01  
 Standard Edition (64-bit) on Windows NT 5.2 (Build 3790: Service Pack 2)
Win 2003 Server , SP2

The current compatibility level is 90.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
visibly, you cannot create a "DEFAULT" with a function having arguments.
so, either, you manage to get your function to work without arguments,
or you actually want the column to be a computed column (there you can use a function with arguments)
or you have to migrate the "default" to the insert trigger.
0
 
dteshomeAuthor Commented:
Thank U!

I have gone the computed column sol'n and implemented the function.  It works.

Much appreciated.
Dan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now