Quantcast
Channel: West Wind Message Board Messages
Viewing all articles
Browse latest Browse all 10393

Re: wwBusiness and SQL Identity column

$
0
0
Re: wwBusiness and SQL Identity column
Web Connection User Discussions
Re: wwBusiness and SQL Identity column
02/17/2012
07:38:56 AM
3FO1FFTU1 Show this entire thread in new window
Gratar Image based on email address
From:
To:
Attachments:
None
Bob,

Thanks. I was not sure if Rick had an ADO call to update an identity column on an insert, but @@IDENTITY was my back-up plan. I have used @@IDENTITY going back to SQL 6.5 and I am only adding one row at a time.

Question: Why do you reload the new row, after getting the identity value? Is that to return any default values? I am just wondering because it does not seem like it will help me as far as the identity column goes, since it is a stateless app.

Since Web apps are stateless, the next button click will have to reload the row. However, I want to make sure that I don't reload a row or insert a new row, based on a URL parameter. I was thinking of using a session variable to store the identity value and another session variable to store the operation. So, I add a contact. Now, I want to add an order. When I click the button to add the order, look at the session variable, rather than the URL parameter.

Thanks,

David

Hi David;

I have been using SQL Server with a number of applications.
I have subclassed the wwsql class and modified a few of the methods.
Specifically saving data (I have columns that can have default null values, or have a default value like GETDATE() and so my code specifically will not include those fields
on an insert if the value is null. Also, getting back the primary key is valuable too!)
Anyway, here is the sub-classed code of my save method.
Note that after the save of data, I do a specific call to the load method with the primary key to
load the oData object with the values just saved, including the primary key.
The buildrecordinfo is an extra method I added for cases where I wanted to display the date and time of the last edit
(pretty well all my tables have columns for who created the record, when it was created, who modified and when modified.
These date fields are updated by a triggers)

I should be using Scope_Identity() instead of @@Identity but as long as inserts don't add records in other tables I am okay.
But I should update this. (This code has been in place for years!)

LOCAL lcPKField, llRetVal, loRecord, lcSqlCommand, loSQL, loTSql llRetVal = .T. THIS.SetError() *** Optional auto Validation IF THIS.lValidateOnSave AND ; !THIS.VALIDATE() RETURN .F. ENDIF loRecord = THIS.oData IF !THIS.OPEN() RETURN .F. ENDIF LOCAL loSQL as wwsql, llUseNamedParameters loSQL = THIS.oSQL loSQL.lError = .F. llUseNamedParameters = WWVFPVERSION > 7 *--llUseNamedParameters = .f. DO CASE CASE THIS.nupdatemode = 2 && New loSQL.cSQL = loSQL.SQLBuildInsertStatementFromObject(loRecord,THIS.cTableName,,llUseNamedParameters) loSQL.Execute("", this.csqlcursor) IF loSQL.lError THIS.SetError(loSQL.cErrorMsg) RETURN .F. ENDIF THIS.nupdatemode = 1 *-- since we have just inserted a record, let's get the IDENTITY Value *-- that we can pass back loSQL.cSQL = [SELECT @@IDENTITY] loSQL.Execute([SELECT ident = @@IDENTITY], [c_ident]) IF loSQL.lError THIS.SetError(loSQL.cErrorMsg) RETURN .F. ENDIF pkfield = [this.odata.] + this.cpkfield &pkfield = c_ident.ident *-- reload the data into the data object this.load(c_ident.ident) USE IN c_ident this.buildrecordinfo() CASE THIS.nupdatemode = 1 && Edit *** Check if exists first lcPKField = THIS.cPKField loSQL.Execute("select " + lcPKField +" from " + THIS.cTableName+ " where " + THIS.cPKField + "=" + TRANS(loRecord.&lcPKField) ) IF loSQL.lError THIS.SetError(loSQL.cSQL) RETURN .F. ENDIF IF RECCOUNT() < 1 lcSqlCommand = loSQL.SQLBuildInsertStatementFromObject(loRecord,THIS.cTableName,,llUseNamedParameters) ELSE lcSqlCommand = loSQL.SqlBuildUpdateStatementFromObject(loRecord,THIS.cTableName,lcPKField,; IIF(THIS.lcompareupdates,THIS.oOrigData,.F.),,llUseNamedParameters ) ENDIF IF !EMPTY(lcSqlCommand) loSQL.Execute( lcSqlCommand, this.csqlcursor) ENDIF IF loSQL.lError THIS.SetError(loSQL.cErrorMsg) RETURN .F. ENDIF this.load(loRecord.&lcPKField) this.buildrecordinfo() ENDCASE *-- clear any parameters in the parameter object loSQL.oParameters = NULL RETURN llRetVal

Bob


Hi,

I am going to use a wwBusiness subclass to connect to an MS SQL table, which has an identity (auto increment) primary key column. I am using wwSQL to connect to the SQL server. I assume that I have to specify the PK column for cSkipFieldsForUpdates. When I insert a new row, the PK column will not have a value. When I save the row, will the wwBusiness class refresh the identity value or do I have to make a call to @@IDENTITY?

Thanks,

David


Viewing all articles
Browse latest Browse all 10393

Trending Articles