Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I have to execute various sql queries but my table name is stored in a variable. I am having problem in create table command.

I tried 2 different methods,but both are showing syntax error.

Here, con is oledbconnection. ninp_tab_name and nout_tab_name are variable names, Input and Output are tables whose schema I want to copy.

code1:
C#
string new_intb_comm = "CREATE TABLE"+ninp_tab_name+""+"AS SELECT * FROM Input WHERE 1=2";
OleDbCommand cmn = new OleDbCommand(new_intb_comm, con);
cmn.ExecuteNonQuery();

code 2:
C#
string new_outb_comm = "CREATE TABLE @out_table AS SELECT * FROM Output WHERE 1=2";
OleDbCommand cmo = new OleDbCommand(new_outb_comm, con);
cmo.Parameters.AddWithValue("@out_table", nout_tab_name);
cmo.ExecuteNonQuery();

Please help asap.
Posted
Updated 28-Mar-12 13:25pm
v2
Comments
[no name] 28-Mar-12 19:55pm    
And what was wrong with the answer that you already got?
Member 8722300 28-Mar-12 21:07pm    
both of these queries shows syntax error :(
If even one of them runs successfully, I can complete my work
[no name] 28-Mar-12 21:08pm    
You are getting the syntax errors because you made the exact same spacing error that you made in your previous post.
Member 8722300 29-Mar-12 4:52am    
hello, can you please put the corrected query here, I may be making some mistake, but I am still not able to run this.

1 solution

Issue with lack of spaces after create table and before as:

string new_intb_comm = "CREATE TABLE "+ninp_tab_name+ " AS (SELECT * FROM Input WHERE 1=2)";
OleDbCommand cmn = new OleDbCommand(new_intb_comm, con);
cmn.ExecuteNonQuery();

I have put the parenthesis that contain the select statement.
 
Share this answer
 
v2
Comments
Member 8722300 29-Mar-12 2:43am    
I have put the spaces as suggested, but it is still showing syntax error.
can you please write the correct string(with required spaces), which I can copy -paste, may be I am still doing something wrong
Clifford Nelson 29-Mar-12 3:16am    
What is the purpose of WHERE 1=2. That will probably mean nothing will be selected. Also try and put the "SELECT * FROM Input WHERE 1=2" inside parenthesis.
Member 8722300 29-Mar-12 4:43am    
yes, 1=2 is used to only get the schema of the table. I even tried to follow your suggestion, but still no success.
If you get success executing the command, please put the corrected string here.
thanks for trying.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900