user2177451
user2177451

Reputation: 23

Insert into select query

HI everyone i hope someone can help me out i am learning c# so far i have written a couple of useful programs, but now i a stumped from a particular problem i have googled the query i thought i would need. this is my code

        try
        {
            con.Open();
            cmd = new SqlCommand("SELECT * from LMNormal", con);
            cmd.CommandText = ("INSERT INTO LMNormal (upc,cert_code,description,cost,normal_price,pricemethod,groupprice,quantity,special_price,specialcost,specialpricemethod,specialgroupprice,specialquantity,start_date,end_date,pack,size,unitofmeasure) SELECT (upc,cert_code,description,cost,normal_price,pricemethod,groupprice,quantity,special_price,specialcost,specialpricemethod,specialgroupprice,specialquantity,start_date,end_date,pack,size,unitofmeasure) from products where (modified >= @now) and (advertised=@false)");
            cmd.Parameters.AddWithValue("@now", now);
            cmd.Parameters.AddWithValue("@false", selected);
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
            timer1.Start();
            con.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);

        }

When the program executes it catch my error and says incorrect syn tact near ',' i am having a hard time trying to figure out where this comma is.

Thanks for any input

Upvotes: 2

Views: 9753

Answers (3)

Tim
Tim

Reputation: 8919

I believe you are missing cmd.CommandType = CommandType.Text.

Upvotes: -1

ljh
ljh

Reputation: 2604

Remove '(' and ')' from your select query


cmd.CommandText = ("INSERT INTO LMNormal (upc,cert_code,description,cost,normal_price,pricemethod,groupprice,quantity,special_price,specialcost,specialpricemethod,specialgroupprice,specialquantity,start_date,end_date,pack,size,unitofmeasure)
SELECT upc,cert_code,description,cost,normal_price,pricemethod,groupprice,quantity,special_price,specialcost,specialpricemethod,specialgroupprice,specialquantity,start_date,end_date,pack,size,unitofmeasure from products where modified >= @now and advertised=@false");

Upvotes: 1

John Woo
John Woo

Reputation: 263933

the only thing you need to do is to remove the parenthesis that wraps the columns in your select statement, eg

INSERT INTO LMNormal (upc, cert_code, description, ..., unitofmeasure)
SELECT upc, cert_code, description, ..., unitofmeasure
FROM   products 
WHERE  (modified >= @now) and (advertised = @false)

Upvotes: 9

Related Questions