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

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

Posted on 2011-03-15
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,
      [idListingPropertyDOM_CDOM] ASC


-- =============================================
      -- Add the parameters for the function here
      @MLSNUM varchar(20), @ListingDate datetime, @Status tinyint, @ChangeDate datetime
      -- 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

Question by:dteshome
  • 5
  • 2
  • 2
  • +1
LVL 11

Expert Comment

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

Author Comment

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.

LVL 40

Expert Comment

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?
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.


Author Comment

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.


LVL 40

Expert Comment

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.

Author Comment

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.

LVL 143

Expert Comment

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

Author Comment

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.
LVL 143

Accepted Solution

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.

Author Closing Comment

ID: 35149454
Thank U!

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

Much appreciated.

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

791 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