Solved

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

Posted on 2011-03-15
10
374 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

710 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