1

我有以下选择语句:

string query = @"Select Sudent, " + Guid.NewGuid() + " as  Id, Name, DOB from Student where Name = Ken";

然后我在 bulkcopy 命令中使用此语句在表中插入。但问题是它只为所有 5 行生成 1 个 guid 并引发错误。如何为列 ID 的所有 5 行(或任意数量的行选择获取)获取不同的 guid。

4

3 回答 3

5
string query = @"Select Student, newid() as  Id, Name, DOB from Student where Name = Ken";
于 2012-09-07T20:05:07.783 回答
5

对于 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'
于 2012-09-07T20:06:03.213 回答
2

由于您使用的是 C#,我将假设 SQL Server。

SQL Server 有一个内置函数——NEWID它将创建一个 GUID。

string query = @"Select Sudent, NEWID() as  Id, Name, DOB 
                 from Student where Name = Ken";
于 2012-09-07T20:04:36.270 回答