Solved

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

Posted on 2011-03-15
10
369 Views
Last Modified: 2012-05-11
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
Comment
Question by:dteshome
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 11

Expert Comment

by:indianguru2
Comment Utility
Is the function already created?
If yes, try this statement for a sample.

select *, dbo.fnCalculateDOM(MLSNUM, ListDate, Status, ChangeDate)   from tblListingPropertyDOM_CDOM
0
 

Author Comment

by:dteshome
Comment Utility
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
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
>> 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
 

Author Comment

by:dteshome
Comment Utility
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
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:dteshome
Comment Utility
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
please clarify the compatibility version of the database
0
 

Author Comment

by:dteshome
Comment Utility
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
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:dteshome
Comment Utility
Thank U!

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

Much appreciated.
Dan
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now