SQL Programming
Can web connection work with identity fields as primary keys
Gravatar is a globally recognized avatar based on your email address. Can web connection work with identity fields as primary keys
  Alejandro A Sosa
  All
  Apr 23, 2021 @ 08:06pm

Newbie question.

I am using Web connection and wwBusinessObject with data in sql server.

Is web connection able to work with primary keys that are autoincremented identity fields or is it required that the value of the pk be calculated with the provided stored procedure sp_ww_newid?

If web connection is able to use an auto-incremented pk, how do you find out the just generated pk?

Thank you very much,

Alex

Gravatar is a globally recognized avatar based on your email address. re: Can web connection work with identity fields as primary keys
  Rick Strahl
  Alejandro A Sosa
  Apr 23, 2021 @ 11:56pm

Yes - we covered how you can override NewId() and just have it do nothing.

You can retrieve the identity key with the @@identity value from SQL Server which returns the last identity value that was created.

if (!loBus.Save())
   ? loBus.cErrorMsg
   RETURN
endif

loBus.Execute("select @@identity as Id","TQuery")

loBus.oData.id = TQuery.Id
use in TQuery

You can override the Save() method of your business object - call DoDefault() and then run that and assign the value to the entity.

FUNCTION Save()

IF (!DoDefault())
   RETURN .F.
ENDIF

lcAlias = Alias()

*** If Id is not set yet (ie. save) try to retrieve it
IF THIS.oData.id < 1 AND ;   
   THIS.Execute("select @@identity AS id","TQuery") = 1
   this.oData.id = TQuery.Id
ENDIF

use in TQuery
IF !EMPTY(lcAlias)
   select (lcAlias)
ENDIF
ENDFUNC

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Can web connection work with identity fields as primary keys
  Alejandro A Sosa
  Rick Strahl
  Apr 24, 2021 @ 04:48am

Thank you very much Rick.

Alex

© 1996-2024