我有以下选择语句:
string query = @"Select Sudent, " + Guid.NewGuid() + " as Id, Name, DOB from Student where Name = Ken";
然后我在 bulkcopy 命令中使用此语句在表中插入。但问题是它只为所有 5 行生成 1 个 guid 并引发错误。如何为列 ID 的所有 5 行(或任意数量的行选择获取)获取不同的 guid。
string query = @"Select Student, newid() as Id, Name, DOB from Student where Name = Ken";
对于 SQL Server:
SELECT Student, NEWID() AS Id, Name, DOB
FROM Student
WHERE Name = 'Ken'
对于 MySQL:
SELECT Student, UUID() AS Id, Name, DOB
FROM Student
WHERE Name = 'Ken'
对于甲骨文:
SELECT Student, SYS_GUID() AS Id, Name, DOB
FROM Student
WHERE Name = 'Ken'
由于您使用的是 C#,我将假设 SQL Server。
SQL Server 有一个内置函数——NEWID
它将创建一个 GUID。
string query = @"Select Sudent, NEWID() as Id, Name, DOB
from Student where Name = Ken";