Doug Ancil
Doug Ancil

Reputation: 549

Adding a primary key with an "insert into" statement

I have the following query and need to add a Primary Key to the Column of Employeenumber:

SELECT [Exceptions].Employeenumber,[Exceptions].exceptiondate, [Exceptions].starttime, [exceptions].endtime, [Exceptions].code, datediff(minute, starttime, endtime)  as minutes INTO scratchpad3,
FROM Employees INNER JOIN Exceptions ON [Exceptions].EmployeeNumber = [Exceptions].Employeenumber
where [Exceptions].exceptiondate between '5/1/2011' and '5/8/2011'
GROUP BY [Exceptions].Employeenumber, [Exceptions].Exceptiondate, [Exceptions].starttime, [exceptions].endtime,
[Exceptions].code, [Exceptions].exceptiondate

but don't know the proper syntax when you're doing a "create" this way. What's the propery syntax to add a primary key this way?

Thank you.

Upvotes: 0

Views: 257

Answers (1)

user724198
user724198

Reputation:

You can't add a Primary Key to a SELECT statement. Primary Keys are identifying columns of tables. You'd need to ALTER TABLE and ADD PRIMARY KEY. The syntax is different, but it looks like you're using SQL Server. Statements can be found Here.

If you're looking to just add a number for each record, try using ROW_NUMBER (might be different depending on the database you're using).

Hope that helps, Jason

Upvotes: 1

Related Questions