Skip to main content
Nintex Community Menu Bar

Show SQL Server table inside Nintex Form

  • July 4, 2018
  • 6 replies
  • 326 views
  • Translate

TomaszPoszytek
Forum|alt.badge.img+17

Ever wondered how to display SQL table inside your Nintex Form? Indeed, there is the “SQL Request” action, but it only allows you to show data from database as a dropdown, list of options, etc… and always – just a single column.

However there is an easy solution for that. The approach I am using includes usage of the “FOR XML” command in a SELECT statement (source). It is available in SQL Server starting from version 2008. It returns data from a query using an XML format, concatenated in a single row, in a single column. Perfect format to parse it!

Step by step

1. First prepare your SELECT query. Mine is for example:

  1. SELECT TOP(1) (SELECT name, lastname, email, role
  2. FROM users
  3. RIGHT JOIN roles ON roles.Id = users.roleId
  4. ORDER BY lastname ASC
  5. FOR XML) as datatable FROM users

With such statement I am sure, that I will receive just a single row and column, that will return the data in a proper XML format. Each row will be built using the following structure:

  1. <name>value</name><lastname>value</lastname><email>value</email><role>value</role><name>value</name><lastname>value</lastname><email>value</email><role>value</role>...

Put the query in the “SQL Request” control inside your form:

SQL Request action Nintex Forms

Set the field not to be visible. It is not going to be used directly.

2. Now add a “Calculated Value” control. It will be used to get the output from the “SQL Request” and parse it into a valid table. I am using the following formula to achieve it:

  1. '<table class="dataTable"><thead><tr><th>Name</th><th>Lastname</th><th>Email</th><th>Role</th></tr></thead><tbody>'+replace(replace(replace(replace(SQL REQUEST CONTROL NAME, '</role><name>', '</td></tr><tr><td>'),'</(?!td|tr)[a-zA-Z]+><(?!td|tr)[a-zA-Z]+>','</td><td>'), '</role>', '</td></tr>'), '<name>', '<tr><td>')+'</tbody></table>'

It simply creates a ready to use HTML table. It replaces ending and starting XML tags to starting and ending <tr><td> tags (to mark start and end of each row).

3. Next define a CSS styles for your table. I used the following page to create a set of CSS: https://divtable.com/table-styler/ 

Done!

The table is ready to be shown:

HTML table out of SQL table

I hope this can be find useful for you.

Did this topic help you find an answer to your question?

6 replies

Forum|alt.badge.img+11
  • Scout
  • 357 replies
  • July 5, 2018

Thank you Tomasz Poszytek for this excellent tip.

Translate

Forum|alt.badge.img+12
  • 848 replies
  • July 5, 2018

Nice! That surely will be useful.

Translate

TomaszPoszytek
Forum|alt.badge.img+17

You can go even further. Add DataTables script (https://DataTables.net) as custom includes, use proper classes and enjoy dynamic HTML tables, allowing sorting, searching and filtering  

Translate

Forum|alt.badge.img+2
  • 8 replies
  • May 2, 2019

Hello Tomasz poszytek

I am having trouble implementing this solution. Can you please help. 

My sql statement is 

Select (Select [IRB ID] as IRBID, [Approval Date] as ApprovalDate , [Expiration Date] as ExpirationDate, [Review Type] as ReviewType From [dbo].[vTrial_IRBApproval] where [Review Type] = 'initial review' FOR XML Path) as datatable 

This works fine and retrives everything in one row. 

 

Calculated Value control formula:

'<table>
<thead><tr><th>IRBID</th><th>ApprovalDate</th><th>ExpirationDate</th><th>ReviewType</th></tr></thead>
<tbody>'+replace(replace(replace(replace(sqlIRBApprl, '</ReviewType><IRBID>', '</td></tr><tr><td>'),'</(?!td|tr)[a-zA-Z]+><(?!td|tr)[a-zA-Z]+>','</td><td>'), '</ReviewType>', '</td></tr>'), '<IRBID>', '<tr><td>')+'
<tbody>
</table>'

When I use calculated value control with this formaula, entire form goes blank. 

Can you please let me know what I am doing wrong. 

Thank you

 

Translate

Forum|alt.badge.img+17
  • 911 replies
  • May 21, 2019

If everything goes blank, that's an indication that the code on the form is corrupt or not correct. I would suggest going at it piece by piece to ensure that your code is loading properly. 

Translate

Forum|alt.badge.img+1
  • Rookie
  • 1 reply
  • March 31, 2023

Great job! I liked this post.

I created a Nintex form using the responsive form to connect to a cloud based sql server. When I test the Connection to the server the result is successful and when I test the query I get the expected result. However when I preview the form no results appear in the dropdown.

Translate

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie Settings