简体   繁体   中英

Insert GUID into sql table uniqueidentifier field - Groovy

With the following table how would I insert a Guid value into the guidField of the table. Using the following groovy code results in the following error "because: The name "B551F2C8" is not permitted in this context." Guid generated is B551F2C8-8380-491B-A51F-436E51CDD08F. Maybe the '-' in the Guid are the problem. Any ideas?

SQL table

create table tblTest  
(
  guidField  uniqueidentifier,
  strField  varchar(50),
  dateField smalldatetime
)

Code block

UUID uuid = UUID.randomUUID()
long lTimeStamp = d.getTime();
String tableInsert = "INSERT INTO tblTest (guidField,strField,dateField)" +
                                  " VALUES ("   + uuid +
                                            "," + 'test' +
                                            "," + lTimeStamp +
                                            ")";
sql.execute(tableInsert);
UUID uuid = UUID.randomUUID()
//long lTimeStamp = d.getTime();
String tableInsert = "INSERT INTO tblTest (guidField,strField,dateField)" +
                              " VALUES ('" + uuid + "'"
                                        ",'test'" +
                                        ", getdate()"
                                        ")";
sql.execute(tableInsert);

Do you need your guid into your application? If not, let SQL server generate one for you :

String tableInsert = "INSERT INTO tblTest (guidField,strField,dateField)" +
                              " VALUES (newid()"
                                        ",'test'" +
                                        ", getdate()"
                                        ")";
sql.execute(tableInsert);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM