CREATE TABLE `groupid` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
BEGIN
INSERT 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')
COMMIT
Daniel Garcia-Gil wrote:Hello
create a new table with group insert ids
- Code: Select all Expand view
CREATE TABLE `groupid` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
start transaction
- Code: Select all Expand view
BEGIN
insert in groupid
- Code: Select all Expand view
INSERT INTO groupid (id) VALUES(NULL)
retrieve last id
- Code: Select all Expand view
@LASTID=LAST_INSERT_ID()
use the id to group insert
- Code: Select all Expand view
INSERT INTO myTable (groupid, field1, field2) VALUES (@LASTID, 'VALUE1', 'VALUE2'), (@LASTID, 'VALUE3', 'VALUE4'), (@LASTID, 'VALUE5', 'VALUE6')
close transaction
- Code: Select all Expand view
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()
%>
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 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 ?
Return to Off Topic / Otros temas
Users browsing this forum: Google [Bot] and 6 guests