1

I am struggling to get my query to work in c# usng the select * into statement which will copy the data of the table and create a new table.

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         
        }

Can anyone point me the right way?. thanks

Androidz
  • 413
  • 1
  • 6
  • 19

3 Answers3

3

Your query, as it is now, is going to look something like:

... sp.qty_onhand as Qtyinto TempProductProfile from product_profile ppinner join ...

You need to insert a space at the beginning of each line you're concatenating:

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))
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
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         
        }

Write the query like this.With @ you can write string on more than one line ! The problem is explain in Grant Winney answer about wrong concatenation of strings.

mybirthname
  • 17,949
  • 3
  • 31
  • 55
0

Put a space at the start of each line of text, excluding the first one.

As you don't have any spaces between your lines of text, your SQL is

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
DeanOC
  • 7,142
  • 6
  • 42
  • 56