0

I am trying to convert excel file rows into columns using C# code

string command=";WITH CTE AS(SELECT * FROM (SELECT IPAddress, Slot1,Slot2,Slot3,Slot4 FROM ["+ sheet1 +"] ) T UNPIVOT ( SlotPort FOR N IN (Slot1 ,Slot2 ,Slot3 , Slot4))P )SELECT SlotPort, IPAddress FROM CTE";

Am using above code to achieve the mentioned process. But I am getting the below error.

Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'.

Please anyone help to resolve this issue. Thanks in advance

4
  • Which connection do you use? Is excel linked to your sql server? Commented Jan 19, 2015 at 14:40
  • OleDbConnection am using Commented Jan 19, 2015 at 14:42
  • You are querying Excel directly using this SQL? Commented Jan 19, 2015 at 14:42
  • Yes. I am connecting excel with oledb connection using this sql query. Commented Jan 19, 2015 at 14:49

1 Answer 1

1

You should specify unpivoted column.

;WITH CTE AS
(
    SELECT IPAddress, N, SlotPort
    FROM
    (SELECT IPAddress, Slot1,Slot2,Slot3,Slot4 FROM ["+ sheet1 +"]) T
    UNPIVOT (SlotPort FOR N IN (Slot1 ,Slot2 ,Slot3 , Slot4)) P
)
SELECT SlotPort, IPAddress FROM CTE
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.