Updating fields using functions

Posts   
 
    
Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 05-Nov-2004 16:55:43   

Hi All,

How would I update a field with the sql server date/time?

Currently, I use something like:

 DECLARE @varUpdatedID  varchar(30),
    @varUpdatedDate datetime

SET @varUpdatedID = USER_NAME()
SET @varUpdatedDate = GETDATE()

UPDATE Assignment
SET     SectionNo = @parSectionNo, 
    Name = @parName, 
    Points = @parPoints, 
    Weight = @parWeight, 
    CategoryID = @parCategoryId, 
    DateDue = @parDateDue, 
    CimType = @parCimType,
    AssignmentDesc = @parAssignmentDesc, 
    Sequence = @parSequence,
    RequiredFlag = @parRequiredFlag, 
    Note = @parNote,
    Publish = @parPublish,
    UpdatedID= @varUpdatedID,
    UpdatedDate = @varUpdatedDate
WHERE 
        AssignmentId = @parAssignmentId 

Which use the GETDATE and USER_NAME functions.

So, how would I accomplish this without a sproc?

Thanks,

Fishy.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 05-Nov-2004 17:31:01   

This is not yet supported. You also can't fake it with some low level code at the moment.

Frans Bouma | Lead developer LLBLGen Pro
Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 05-Nov-2004 18:05:24   

Since these fields are on every table I take it that I can only use llblgen to select data. Maybe I can use it to Insert/Delete.

Where is this enhancement in the queue?

Thanks,

Fishy.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 05-Nov-2004 18:29:26   

Fishy wrote:

Since these fields are on every table I take it that I can only use llblgen to select data. Maybe I can use it to Insert/Delete.

Where is this enhancement in the queue?

In the next upgrade of the designer, which release is expected early 2005.

You could work around the requirement of your system of course, by passing in DateTime.Now for the date and for the username you can either use the windows user the app is under if you use integrated security, or read the userid from the connection string, as that's the same value.

Frans Bouma | Lead developer LLBLGen Pro
Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 05-Nov-2004 19:03:02   

Otis wrote:

You could work around the requirement of your system of course, by passing in DateTime.Now for the date and for the username you can either use the windows user the app is under if you use integrated security, or read the userid from the connection string, as that's the same value.

I suppose I could get a little elaborate and get the time off of sql server when I start up the app, then take the difference from DateTime.Now and use that to adjust the value of the DateTime.Now when assigning that value to the field. cry

or if the enhancement is early enough I'll just wait.

Thanks again,

Fishy.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 05-Nov-2004 21:05:42   

I can't give an estimate release date, as the final functionality for the designer hasn't been finalized so I'm afraid it's a workaround for now disappointed , sorry.

Frans Bouma | Lead developer LLBLGen Pro
ctadlock avatar
ctadlock
User
Posts: 60
Joined: 12-Feb-2004
# Posted on: 07-Nov-2004 03:06:35   

You could use a sql server trigger to solve the problem.

-CT

wayne avatar
wayne
User
Posts: 611
Joined: 07-Apr-2004
# Posted on: 07-Nov-2004 07:16:30   

I have altered my DBUtils template a few weeks ago - so that i could call UDF's. It is for selfservicing but i am sure you guys can Hack it a bit. - Maybe it could help you.

Here is a example of how i have used it:

return DBUtils.CallUDFunction("Decrypt", CreditCardPaymentEntity.CCNumber)
CreditCardPaymentEntity.CCNumber =  DBUtils.CallUDFunction("Encrypt",CCNum,DateTime.Now)

I will ask Frans to put it on the 3rdParty section on Monday.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 07-Nov-2004 10:26:49   

Wayne, how do you call the function in ado.net? As an sqlserver function (not a proc) is not callable from a command, just from SQL, or am I mistaken?

Frans Bouma | Lead developer LLBLGen Pro
wayne avatar
wayne
User
Posts: 611
Joined: 07-Apr-2004
# Posted on: 07-Nov-2004 11:13:28   

Hi Frans,

I don't have the exact code with me at the momment - it is at work but this is what i do.

I wrap it in a SQL Select statement using DB_Command.CommandType = CommandType.Text - this will return a normal result set that you can put in a datatable - or you can try ExecuteScalar.

select dbo. FunctionName (param1, Param2)

So far i have not had any problems and it works fine. simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 07-Nov-2004 11:29:46   

Sounds the way to do it indeed, after I wrote the post I thought of something similar, however perhaps there was a more cleaner way wink )

Frans Bouma | Lead developer LLBLGen Pro
wayne avatar
wayne
User
Posts: 611
Joined: 07-Apr-2004
# Posted on: 08-Nov-2004 11:33:48   

Ok guys - Add-on for User Defined Functions is available from 3rd Party section! simple_smile

Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 08-Nov-2004 17:45:58   

ctadlock wrote:

You could use a sql server trigger to solve the problem.

-CT

That works out best for my situation. The following code is what I'll use

 CREATE TRIGGER UpdateTableNameTrigger ON TableName
 FOR Update
AS 
Update TableName
  SET LastUpdateDateTime = GETDATE
WHERE TableName.PK = Inserted.PK

Just in case someone else needs to do something like this.

Thanks,

Fishy