Dan
Dan

Reputation: 47

Inserting data into a SQL Server table

I getting an error when attempting to insert data into my table for I don't know how to insert a primary key. The error I get is:

Operand type clash: int is incompatible with uniqueidentifier

My code:

USE BeachTennis7

INSERT INTO dbo.player (playerfname, playerlname, gender, age, [prior rank], player_id)
VALUES ('Dan', 'McClure', 'M', 21, 1, 0);

I understand the the int value cannot be converted into a key, I just don't know how to declare a primary key. Thanks!

Upvotes: 0

Views: 55

Answers (2)

M Prabhu
M Prabhu

Reputation: 58

Use this

INSERT INTO dbo.player (playerfname, playerlname, gender, age, [prior rank], player_id) VALUES ('Dan', 'McClure', 'M', 21, 1, NEWID());

Upvotes: 0

CBredlow
CBredlow

Reputation: 2840

You need to declare a variable as a unique identifier, then add it to the insert statement.

DECLARE @playerId uniqueidentifier
SET @playerId = NEWID()

INSERT INTO dbo.player (playerfname,playerlname,gender,age,[prior rank],player_id)
VALUES ('Dan','McClure','M',21,1, @playerId);

Upvotes: 2

Related Questions