Solved

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

Posted on 2011-03-15
10
375 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 11

Expert Comment

by:indianguru2
ID: 35140111
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
ID: 35140972
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 41

Expert Comment

by:Sharath
ID: 35141548
>> 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dteshome
ID: 35142026
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 41

Expert Comment

by:Sharath
ID: 35142268
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
 

Author Comment

by:dteshome
ID: 35142747
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 143

Expert Comment

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

Author Comment

by:dteshome
ID: 35148335
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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 35148794
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
ID: 35149454
Thank U!

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

Much appreciated.
Dan
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

631 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