CLOSE DATA ALL CREATE DATABASE 'TESTPK.DBC' CREATE TABLE 'IDS.DBF' NAME 'IDS' (TABLE C(15) NOT NULL, ; NEXTID I NOT NULL) INDEX ON TABLE TAG TABLE CREATE TABLE 'DUMMYTEST.DBF' NAME 'DUMMYTEST' ; (PKID I NOT NULL DEFAULT newid("DUMMYTEST"), MYFIELD C(10) NOT NULL) INDEX ON PKID TAG PKID CREATE SQL VIEW "VIEW1" ; AS SELECT * FROM testpk!dummytest DBSetProp('VIEW1', 'View', 'UpdateType', 1) DBSetProp('VIEW1', 'View', 'WhereType', 3) DBSetProp('VIEW1', 'View', 'FetchMemo', .T.) DBSetProp('VIEW1', 'View', 'SendUpdates', .T.) DBSetProp('VIEW1', 'View', 'UseMemoSize', 255) DBSetProp('VIEW1', 'View', 'FetchSize', 100) DBSetProp('VIEW1', 'View', 'MaxRecords', -1) DBSetProp('VIEW1', 'View', 'Tables', 'testpk!dummytest') DBSetProp('VIEW1', 'View', 'Prepared', .F.) DBSetProp('VIEW1', 'View', 'CompareMemo', .T.) DBSetProp('VIEW1', 'View', 'FetchAsNeeded', .F.) DBSetProp('VIEW1', 'View', 'FetchSize', 100) DBSetProp('VIEW1', 'View', 'Comment', "") DBSetProp('VIEW1', 'View', 'BatchUpdateCount', 1) DBSetProp('VIEW1', 'View', 'ShareConnection', .F.) *!* Field Level Properties for VIEW1 * Props for the VIEW1.pkid field. DBSetProp('VIEW1.pkid', 'Field', 'KeyField', .T.) DBSetProp('VIEW1.pkid', 'Field', 'Updatable', .T.) DBSetProp('VIEW1.pkid', 'Field', 'UpdateName', 'testpk!dummytest.pkid') DBSetProp('VIEW1.pkid', 'Field', 'DataType', "I") DBSetProp('VIEW1.pkid', 'Field', 'DefaultValue', "NewId('DUMMYTEST')") * Props for the VIEW1.myfield field. DBSetProp('VIEW1.myfield', 'Field', 'KeyField', .F.) DBSetProp('VIEW1.myfield', 'Field', 'Updatable', .T.) DBSetProp('VIEW1.myfield', 'Field', 'UpdateName', 'testpk!dummytest.myfield') DBSetProp('VIEW1.myfield', 'Field', 'DataType', "C(10)")And this goes to stored procedures :
function NewId LPARAMETERS tcAlias LOCAL lcAlias * Uppercase Alias name lcAlias = upper(iif(parameters() = 0, alias(), tcAlias)) * Lock reprocess - try once SET REPROCESS TO 1 IF !USED("IDS") USE ids IN 0 ENDIF * If no entry yet create and return 1 IF !SEEK(lcAlias, "Ids", "table") INSERT into ids (Table, NextId) values (lcAlias,1) RETURN ids.NextId ENDIF * Lock, increment id, unlock, return nextid value DO while !RLOCK('ids') * Delay before next lock trial lnStart = seconds() DO while seconds()-lnStart < 0.01 ENDDO ENDDO * Increment REPLACE ids.NextId WITH ids.NextId + 1 in 'ids' UNLOCK in 'ids' RETURN ids.NextIdDBC and view ready. Now test code :
open data TestPk use view1 && Not really necessary - to mimic opening in DE insert into View1 (myField) values ('A') insert into View1 (myField) values ('B') insert into View1 (myField) values ('C') insert into View1 (myField) values ('D') insert into View1 (myField) values ('') tableupdate() close all clear all use DummyTest browseCetin