在c#中使用select * into语句时出错

时间:2014-09-18 23:15:01

标签: c#

我正在努力让我的查询在c#usng中使用select * into语句,该语句将复制表的数据并创建一个新表。

using (SqlConnection con = new SqlConnection(connectionstring))
        {
            con.Open();
            using (SqlCommand cmd = new SqlCommand("select  pp.upc as upc , pp.description as                     Description,sp.qty_onhand as Qty" +
                          "into TempProductProfile from product_profile pp" +
                          "inner join store_products sp on" +
                          "pp.upc = sp.upc" +
                          "order by pp.description", con))

                   cmd.executenonquery(): <-- got error here         
        }

有人能指出正确的方法吗? 感谢

3 个答案:

答案 0 :(得分:3)

您的查询现在看起来像是:

  

... sp.qty_onhand作为Qtyinto TempProductProfile from product_profile ppinner join ...

您需要在每行连接处插入一个空格:

using (SqlCommand cmd = new SqlCommand(
    "select  pp.upc as upc , pp.description as Description,sp.qty_onhand as Qty" +
    " into TempProductProfile from product_profile pp" +
    " inner join store_products sp on" +
    " pp.upc = sp.upc" +
    " order by pp.description", con))

答案 1 :(得分:3)

using (SqlConnection con = new SqlConnection(connectionstring))
        {
            con.Open();
            using (SqlCommand cmd = new SqlCommand(@"select  pp.upc as upc , pp.description as                     Description,sp.qty_onhand as Qty
                          into TempProductProfile from product_profile pp
                          inner join store_products sp on
                          pp.upc = sp.upc
                          order by pp.description", con))

                   cmd.executenonquery(): <-- got error here         
        }

像这样编写查询。使用@,您可以在多行上写字符串!问题在Grant Winney答案中解释了字符串的错误连接。

答案 2 :(得分:0)

在每行文本的开头添加一个空格,不包括第一行。

由于您的文本行之间没有任何空格,因此您的SQL

select  pp.upc as upc , pp.description as Description,sp.qty_onhand as Qtyinto TempProductProfile from product_profile ppinner join store_products sp onpp.upc = sp.upcorder by pp.description