web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / PAD - reference a spec...
Power Automate
Answered

PAD - reference a specific column name of output from SQL Query to export to Excel

(0) ShareShare
ReportReport
Posted on by 64

I am creating a flow in PAD where I want to reference a specific column from an output of a sql query, and add that column to an excel spreadsheet. The SQL query outputs three columns, and I want the data from the 3rd column included in the Excel spreadsheet.

I have the same question (0)
  • VishnuReddy1997 Profile Picture
    2,666 Super User 2026 Season 1 on at

    Hi @MCubedMama ,

     

    Please find the below solution,

     

    1. Open SQL Connection:
    ------------------------------
    Use Excel as DB and use below expression Provider=Microsoft.ACE.OLEDB.12.0;Data Source=%Excel_File_Path%;Extended Properties="Excel 12.0 Xml;HDR=YES";

    2. Execute SQL Query:
    ---------------------------------------
    SELECT Column1, Column2, Column3 FROM Sheet1

    change column names as per yours

    3.For Each Loop
    --------------------------
    After the "Execute SQL Query" action, add a "For Each" loop iterating through each row returned by the query.
    Write only 3rd Column Data.

     

    Note: if it worked mark it as a solution

  • Verified answer
    Deenuji_Loganathan_ Profile Picture
    6,255 Moderator on at

    @MCubedMama 

    Please follow the below suggestion.

    1. Connect to Database:

      • Use the provided ConnectionString to connect to your database located on the server 
    2. Execute SQL Statement:

      • Execute the SQL statement: "SELECT [Package],[Command],[Hint] FROM [Action]".
      • Store the result of the SQL query in a variable named QueryResult.
    3. Launch Excel and Open Workbook:

      • Launch Microsoft Excel and open the workbook located at "C:\Users\Documents\Results.xlsx".
    4. Find the First Free Column and Row in the Excel Sheet:

      • Determine the first free column and store its index in a variable named FirstFreeColumn.
      • Determine the first free row and store its index in a variable named FirstFreeRow.
    5. Set Counter to 0:

      • Initialize a variable named Counter with a value of 0.
    6. Check if Query Result has Rows:

      • If the QueryResult contains rows (i.e., QueryResult.RowsCount > 0), proceed with the following steps; otherwise, skip to Closing the Excel file.
    7. Loop through Each Row in the Query Result:

      • For each row in the QueryResult: Write the value of the third column (index 2) to the Excel sheet           

     

     

    Index based syntax: %CurrentItem[2]%
    Column based syntax: %CurrentItem['columnName']%
    Example: %CurrentItem['Hint']

     

     

    • Write the value to the cell in the column identified by FirstFreeColumn and in the row identified by FirstFreeRow plus the value of the Counter variable.
    • Increment the Counter variable by 1.

          8. Save the Excel Workbook:

                     Save the changes made to the Excel workbook.

                     Close the Excel instance.

    Deenuji_0-1709956344048.png

    Code:

     

     

    Database.Connect ConnectionString: $'''Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BotConverter;Data Source=SQLEXPRESS''' Connection=> SQLConnection
    @@copilotGeneratedAction: 'False'
    Database.ExecuteSqlStatement.ConnectAndExecute ConnectionString: $'''Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BotConverter;Data Source=SQLEXPRESS''' Statement: $'''SELECT [Package],[Command],[Hint] FROM [Action]
    ''' Timeout: 30 Result=> QueryResult
    Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: $'''C:\\Users\\Documents\\Results.xlsx''' Visible: True ReadOnly: False Instance=> ExcelInstance
    Excel.GetFirstFreeColumnRow Instance: ExcelInstance FirstFreeColumn=> FirstFreeColumn FirstFreeRow=> FirstFreeRow
    SET Counter TO 0
    IF QueryResult.RowsCount > 0 THEN
    LOOP FOREACH CurrentItem IN QueryResult
    Excel.WriteToExcel.WriteCell Instance: ExcelInstance Value: CurrentItem[2] Column: FirstFreeColumn Row: FirstFreeRow + Counter
    Variables.IncreaseVariable Value: Counter IncrementValue: 1
    END
    END
    Excel.SaveExcel.Save Instance: ExcelInstance
    Excel.CloseExcel.CloseAndSave Instance: ExcelInstance

     

     


    Thanks,
    Deenuji Loganathan
    🤖 Automation Evangelist
    Deenuji - Follow me on LinkedIn

    -------------------------------------------------------------------------------------------------------------
    If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🌟

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 241 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 179

#3
Haque Profile Picture

Haque 159

Last 30 days Overall leaderboard