user990016
user990016

Reputation: 3368

sql create table

I cannot find the syntax error in the following statement:

CREATE TABLE dbo.statslogsummary as
  (SELECT COUNT(logID) AS userid, logUserID,MAX(logDateTime)
FROM statsLog
GROUP BY logUserID);

Tells me "invalid syntax near AS"

Upvotes: 0

Views: 374

Answers (2)

David
David

Reputation: 73554

A CREATE TABLE statement shouldn't have a SELECT statement in it. A CREATE TABLE statement should only be defining the table structure.

If you're trying to create a table by selecting data from another table, you need to use the Select Into syntax.

SELECT COUNT(logID) AS userid, logUserID,MAX(logDateTime)  AS logDateTime
INTO dbo.statslogsummary  
FROM statsLog
GROUP BY logUserID

Upvotes: 3

Yahia
Yahia

Reputation: 70369

UPDATE - after is is clear that this is SQL Server:

SELECT COUNT(logID) AS userid, logUserID,MAX(logDateTime) AS maxlogtm
INTO dbo.statslogsummary  
FROM statsLog
GROUP BY logUserID

Upvotes: 2

Related Questions