4

I am trying to create a new table from a query result in SSMS

I have tried:

CREATE TABLE table2 AS
(SELECT * FROM table1
 WHERE code = 'x'
 ORDER BY code;)

However I am getting an error telling me:

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'AS'.

Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'ORDER'.

Can anyone help?

7
  • MySQL or MS SQL Server? Commented Nov 27, 2018 at 15:40
  • 3
    You're looking for INTO ... Select * Into table2 FROM table1 WHERE code = 'x' ORDER BY code Commented Nov 27, 2018 at 15:41
  • MS SQL server, sorry I'm new to this did not know they were different Commented Nov 27, 2018 at 15:41
  • Remove the parentheses? Commented Nov 27, 2018 at 15:44
  • 1
    @JohnCappelletti thank you that worked! Commented Nov 27, 2018 at 15:45

1 Answer 1

4

Try this:

SELECT * 
INTO NEW_TABLE
FROM table1
WHERE code = 'x'
ORDER BY code;
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this is what I applied as provided by John Capelleti earlier above! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.