6 Replies Latest reply on Sep 4, 2014 8:57 AM by Adrien Guguin

    Create dynamic sql queries

      Hi All,


      I want to display a table based on the following select statement:


      select * from employees where userName = @userName


      I should be able to read userName from a browser cookie then run this query based on that parameter.



      Is that possible ? How can I do it ?





      Progress Software

        • 1. Re: Create dynamic sql queries

          If that's not possible. Can I display data in a dashboard based on the tableau server logged in user ? i.e. if the user logged in server is jsmith then the dashboard will display results bases on the query: select * from employees where userName = 'jsmith'


          Is that possible ?

          • 2. Re: Create dynamic sql queries
            James Baker

            Yes, there is a feature called "user filters" for this purpose, with a calculated field function called USERNAME().

            • 3. Re: Create dynamic sql queries
              Wendy Foslien

              James -- I've got a related question, a more generic version of the same question Khaled poses. Can Tableau dynamically modify the content of a data connection? For example, if I had a data connection that contained a table valued function call like this:


              Select * from [dbo].[MY_TABLE_VALUED_FUNCTION_VW](12345,'WENDY','2010/08/01 00:00:00','2010/08/30 11:59:59')


              Could I modify the parameters dynamically from dashboard selections? Or, do I only have the ability for Tableau to generate the WHERE clause on the table?

              • 4. Re: Create dynamic sql queries
                James Baker

                No, if a data connection is set up via Custom SQL, you cannot pass dynamically pass parameters to that connection SQL.

                • 5. Re: Create dynamic sql queries

                  is it possible to pass paramters to the data_connection?  e.g my requirement is to filter out the data based on some fields like user_id, region_id etc.. How can i build the dynamic sql to filter data dynamically based on user_id?

                  • 6. Re: Create dynamic sql queries
                    Adrien Guguin



                    I was reading this post as I am looking to do something similar.I know it is quite old, but i thought i d still reply for other people who would read this.


                    It looks like you could do it now. Go to connect data -->custom SQL. Create a parameter based on the Username  field and then use the parameter in your query:


                    Select * from employees where Username = <Parameters.username>.


                    It doesn't seem to work in the FROM part of your SQL as the parameter is a string, therefore it adds ' ' before and after the value which doesn't match the table name anymore.