Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

Incorrect syntax near @keyword

$
0
0

Hello,

I am trying to create a table in an existing database but when I run the code I receive the error incorrect syntax near @keyword

using (SqlConnection Connection = new SqlConnection(ConfigurationManager.AppSettings["SqlConn"]))
                {
                    string sql = "DECLARE @table_name varchar(MAX) SET @table_name = '" + tableName + "' CREATE TABLE dbo.@table_name (Id int NOT NULL, Name varchar NULL, Date datetime NULL) CREATE CLUSTERED INDEX [CI_@table_name_Id] ON [dbo].[@table_name] ( [Id] ASC) EXEC (@table_name)";
                    SqlCommand command = new SqlCommand(sql, Connection);

                    Connection.Open();
                    command.ExecuteNonQuery();
                    Connection.Close();
                }

Do I need to use an execute such as sp_execute to convert the #table_name variable to an actual value, how do I use the declared variable @table_name to use tableName as the tablename? 


Viewing all articles
Browse latest Browse all 7129

Trending Articles