Dear friends, I need to generate one unique id for a group of newly inserted records (ie. to mark a group of record with a unique id). But I need it for SQL where I can't use explicit locking to assure id uniqueness.
Any ideas?
EMG
Dear friends, I need to generate one unique id for a group of newly inserted records (ie. to mark a group of record with a unique id). But I need it for SQL where I can't use explicit locking to assure id uniqueness.
Any ideas?
EMG
CREATE TABLE `groupid` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;BEGININSERT INTO groupid (id) VALUES(NULL)@LASTID=LAST_INSERT_ID()INSERT INTO myTable (groupid, field1, field2) VALUES (@LASTID, 'VALUE1', 'VALUE2'), (@LASTID, 'VALUE3', 'VALUE4'), (@LASTID, 'VALUE5', 'VALUE6')COMMITDaniel Garcia-Gil wrote:Hello
create a new table with group insert ids
CREATE TABLE `groupid` ( `id` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=InnoDB;
start transaction
BEGIN
insert in groupid
INSERT INTO groupid (id) VALUES(NULL)
retrieve last id
@LASTID=LAST_INSERT_ID()
use the id to group insert
INSERT INTO myTable (groupid, field1, field2) VALUES (@LASTID, 'VALUE1', 'VALUE2'), (@LASTID, 'VALUE3', 'VALUE4'), (@LASTID, 'VALUE5', 'VALUE6')
close transaction
COMMIT
Daniel Garcia-Gil wrote:in Mysql you can start a transaction with "BEGIN", close transaction with "COMMIT" (save the changes) or ROLLBACK to not save changes
http://dev.mysql.com/doc/refman/5.0/en/commit.html
<%
Function GetGuid()
Set TypeLib = CreateObject( "Scriptlet.TypeLib" )
GetGuid = Replace( Mid( TypeLib.Guid, 2, 36 ), "-", "" )
Set TypeLib = Nothing
End Function
Response.Write GetGuid()
%>Enrico
yes, i guess
Daniel Garcia-Gil wrote:Enrico
yes, i guess
Dear friends, I need to generate one unique id for a group of newly inserted records (ie. to mark a group of record with a unique id). But I need it for SQL where I can't use explicit locking to assure id uniqueness.
Any ideas?
EMG
//-------------------
Static Func _GenEid()
LOCAL nRAND, cRAND
LOCAL oRs, cSQL, oERR
oRs:= TOleAuto():New( "ADODB.Recordset" )
oRs:CursorType := 1 // opendkeyset
oRs:CursorLocation := 3 // local cache
oRs:LockType := 3 // lockoportunistic
cSQL := "SELECT Distinct [GroupId] from [YourTable]" // groupid is char(18)
TRY
oRs:Open( cSQL,xCONNECT )
CATCH oErr
MsgInfo( "Error in Opening YourTable to Create Unique EID" )
RETURN("BOGUS")
END TRY
cRAND := 'BOGUS'
DO WHILE .T.
nRAND := nRANDOM(10000000000000000)
// 1 is reserved and 0 is a null key //
IF nRAND = 1 .or. nRAND = 0 .or. nRAND = NIL
LOOP
ENDIF
cRAND := STRzero(nRAND,18)
IF oRs:eof
ELSE
oRs:MoveFirst()
oRs:Find("GroupId = '"+cRAND+"'" )
ENDIF
IF oRs:eof
EXIT
ELSE
LOOP
ENDIF
EXIT
ENDDO
oRs:Close()
RETURN( cRAND )Rick,
I'm testing the guid (see above) right now...
EMG
Enrico
Interested to see how the guid turns out .. How are you going to apply that unique ID to your group of records ?
Rick
Rick Lipkin wrote:Enrico
Interested to see how the guid turns out ..
Rick Lipkin wrote:How are you going to apply that unique ID to your group of records ?