Johnny_C
Johnny_C

Reputation: 1

Syntax error in CREATE TABLE statement When using Create Table AS

For a project i need to copy a table from a template, but when executing the SQL code the response is Syntax error in CREATE TABLE statement.

   OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["LoginDataB"].ToString();
        OleDbCommand cmd = new OleDbCommand();
        cmd.Connection = conn;


    cmd.CommandText = "CREATE TABLE 'TEST' AS SELECT * INTO 'TEST' FROM 
    buttonstemplate";

This is the code i have used. Yet when I use an online SQL editor like W3schools It works just fine. I am using MS Access 2016 and programming in ASP.net

I am just starting to learn to code so I hope someone could help me out.

Upvotes: 0

Views: 792

Answers (2)

maddy23285
maddy23285

Reputation: 786

Try this:

SELECT * INTO TEST FROM BUTTONSTEMPLATE

To copy table into another database for eg: externaldb.mdb

SELECT * INTO TEST IN 'externaldb.mdb' FROM BUTTONSTEMPLATE

Upvotes: 1

ashleedawg
ashleedawg

Reputation: 21619

...I don't think that SQL runs fine in W3 Editor (like your question said). :-)

w3notworking

I'm sure you're aware that the first step is to make sure the SQL will run on it's own.

You're combining two different types of SQL statements for creating a table:

CREATE TABLE is for defining a new table. For example:

CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

If you want to create a table based on the results from another table:

INSERT INTO table2
SELECT * FROM table1
WHERE condition; 

...so perhaps you could use:

INSERT INTO TEST
SELECT * FROM buttonstemplate

Upvotes: 0

Related Questions