Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

sql server - How can I assign a name to the SUM column?

how can I assign a column name to the SUM column ?

i.e.

select OwnerUserId, SUM(PostScore)
INTO Experts
from ...

I get this error:

An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.

I guess because the column containing the results of SUM has not name.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First of all there's no SQL-Server 2003. Only 2000, 2005, 2008 (and then 2008R2, 2012 and the latest 2014).

As for the name - called alias - you can use AS. This is the standard SQL syntax:

SELECT OwnerUserId, SUM(PostScore) AS PostScoreSum
INTO Experts 
FROM ... 

But AS is optional, so you can also alias a column without it:

SELECT OwnerUserId, SUM(PostScore)  PostScoreSum
INTO Experts 
FROM ... 

You can also use the (proprietary, only in SQL-Server) alias = column syntax:

SELECT OwnerUserId, PostScoreSum = SUM(PostScore)  
INTO Experts 
FROM ... 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...