插入选择查询

时间:2013-03-16 16:39:01

标签: c# sql sql-server sql-insert

大家好我希望有人可以帮助我,我正在学习c#到目前为止我已经写了几个有用的程序,但现在我从一个特定的问题难题我用Google搜索我认为我需要的查询。这是我的代码

        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);

        }

当程序执行时,它会捕获我的错误并在'附近'说错误的同义词,我很难弄清楚这个逗号的位置。

感谢任何输入

3 个答案:

答案 0 :(得分:9)

您唯一需要做的就是删除包含select语句中列的括号,例如

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

答案 1 :(得分:1)

从您的选择查询中删除'('和')'


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");

答案 2 :(得分:-1)

我相信你错过了cmd.CommandType = CommandType.Text