Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 HELP Creating New Table

Author  Topic 

zhel04
Starting Member

38 Posts

Posted - 2011-03-02 : 03:50:16
Hi guys. I need help. I would like to create a new table. I have this query:

CREATE TABLE TblMark
AS SELECT CM, Code, SUM ([YTD1 (Value)]), SUM ([YTD2 (Value)])
FROM TblRep

However this query gives an error Incorrect syntax near the keyword 'AS'.

can someone help me what should I do? or teach me the right query?

Thanks. :) God Bless.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-03-02 : 03:54:22
[code]
select <column list>
into table_name
from <table>
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

zhel04
Starting Member

38 Posts

Posted - 2011-03-02 : 03:56:03
Thanks for the reply but i need to get the SUM of the values from the existing table to new table. How will i do that?
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2011-03-02 : 03:56:21
CREATE TABLE TblMark
AS SELECT CM, Code, SUM ([YTD1 (Value)]), SUM ([YTD2 (Value)])
FROM TblRep
it will work in oracle
but u can use simple into clause in sql server for creation of table.
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2011-03-02 : 03:59:07
quote:
Originally posted by zhel04

Thanks for the reply but i need to get the SUM of the values from the existing table to new table. How will i do that?



SELECT CM, Code, SUM ([YTD1 (Value)]) ytd1value, SUM ([YTD2 (Value)])ytd2value into newtablename
FROM TblRep
group by cm,code

it will create the table with columns cm,code,ytd1value,ytd2value
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-03-02 : 03:59:09
quote:
Originally posted by zhel04

Thanks for the reply but i need to get the SUM of the values from the existing table to new table. How will i do that?




Can be done via KH method.


Cheers
MIK
Go to Top of Page

zhel04
Starting Member

38 Posts

Posted - 2011-03-02 : 04:00:49
I want to create a new table copying the fields from the existing table however there will be a computation from it like SUM of the values. How will I do that if I'm going to use the Into statement?
Go to Top of Page

zhel04
Starting Member

38 Posts

Posted - 2011-03-02 : 04:05:50
quote:
Originally posted by bklr

quote:
Originally posted by zhel04

Thanks for the reply but i need to get the SUM of the values from the existing table to new table. How will i do that?



SELECT CM, Code, SUM ([YTD1 (Value)]) ytd1value, SUM ([YTD2 (Value)])ytd2value into newtablename
FROM TblRep
group by cm,code

it will create the table with columns cm,code,ytd1value,ytd2value



No it didn't work. It gives me an error.
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2011-03-02 : 04:06:58
can u post the error message
Go to Top of Page

zhel04
Starting Member

38 Posts

Posted - 2011-03-02 : 04:10:21
quote:
Originally posted by bklr

can u post the error message




Msg 1038, Level 15, State 5, Line 1
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. Add a name or single space as the alias name.
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-03-02 : 05:28:18
there is a column having no name/alias ... provide alias to each and every column and confirm it by executing only the Select Statement.

Cheers
MIK
Go to Top of Page

kazi
Starting Member

8 Posts

Posted - 2011-03-02 : 06:08:31
SELECT CM,Code, sum(YTD1) Value1,sum(YTD2) Value2 into TblMark
FROM TblRep group by CM,Code

Kazi
Go to Top of Page
   

- Advertisement -