NoviceMe
NoviceMe

Reputation: 3256

How to insert new guid on sql select statement for a column

I have following select statement:

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

then i am using this statement in bulkcopy command to do an insert in table. But problem is it is generating only 1 guid for all 5 rows and throws an error. How can i get different guid for all 5 (or any number of rows select gets) rows for column Id.

Upvotes: 2

Views: 6505

Answers (3)

CandiedCode
CandiedCode

Reputation: 604

string query = @"Select Student, newid() as  Id, Name, DOB from Student where Name = Ken";

Upvotes: 6

Joe Stefanelli
Joe Stefanelli

Reputation: 135789

For SQL Server:

SELECT Student, NEWID() AS Id, Name, DOB
    FROM Student
    WHERE Name = 'Ken'

For MySQL:

SELECT Student, UUID() AS Id, Name, DOB
    FROM Student
    WHERE Name = 'Ken'

For Oracle:

SELECT Student, SYS_GUID() AS Id, Name, DOB
    FROM Student
    WHERE Name = 'Ken'

Upvotes: 5

Oded
Oded

Reputation: 498942

Since you are using C#, I will assume SQL Server.

SQL Server has a built in function - NEWID - it will create a GUID.

string query = @"Select Sudent, NEWID() as  Id, Name, DOB 
                 from Student where Name = Ken";

Upvotes: 2

Related Questions