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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Microsoft Dynamics GP (Archived)

Is there a way to find which SQL field/table used by a custom user smartlist?

(0) ShareShare
ReportReport
Posted on by 102

Trying to do sql queries based on user created smartlists, thanks ahead!

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    These are the SQL tables used for smartlist in  SmarlList Designer window. ASIEXP81,ADH00100,ADH00101,ADH00102,ASITAB20. Hope it may be helpful for you.

  • VinhTon Profile Picture
    102 on at

    Unfortunately I don't see any of those tables, thanks for the input

  • L Vail Profile Picture
    65,271 on at

    I think these tables are in the Dynamics database (whatever you call your system database)

    Leslie

  • MG-16101311-0 Profile Picture
    26,225 on at

    Can you tell us what the Smarlist that the user created does or what columns it contains?

  • VinhTon Profile Picture
    102 on at

    It's a general question as I'm the BI guy and people ask me often to source data based on their smartlist. How I currently do it is look at the fields and search for it in the sql tables which obviously can be time consuming and hit or miss at times.

  • Community Member Profile Picture
    on at

    Here is a SQL query that will literally find "anything" in SQL. Just connect to your Company database and change the "N" value to whatever you are looking for.

    --------------------------------------------------------------------------------

    -- Search all columns in all tables in a database for a string.

    -- Does not search: image, sql_variant or user-defined types.

    -- Exact search always for money and smallmoney; no wildcards for matching these.

    --------------------------------------------------------------------------------

    declare @SearchTerm nvarchar(4000) -- Can be max for SQL2005+

    declare @ColumnName sysname

    --------------------------------------------------------------------------------

    -- SET THESE! Usually just the 1st line between the ' and ' marks.

    --------------------------------------------------------------------------------

    set @SearchTerm = N'46768' -- Term to be searched for, wildcards okay

    set @ColumnName = N'' -- Use to restrict the search to certain columns, wildcards okay, null or empty string for all cols

    --------------------------------------------------------------------------------

    -- END SET

    --------------------------------------------------------------------------------

    set nocount on

    declare @TabCols table (

         id int not null primary key identity

       , table_schema sysname not null

       , table_name sysname not null

       , column_name sysname not null

       , data_type sysname not null

    )

    insert into @TabCols (table_schema, table_name, column_name, data_type)

       select t.TABLE_SCHEMA, c.TABLE_NAME, c.COLUMN_NAME, c.DATA_TYPE

       from INFORMATION_SCHEMA.TABLES t

           join INFORMATION_SCHEMA.COLUMNS c on t.TABLE_SCHEMA = c.TABLE_SCHEMA

               and t.TABLE_NAME = c.TABLE_NAME

       where 1 = 1

           and t.TABLE_TYPE = 'base table'

           and c.DATA_TYPE not in ('image', 'sql_variant')

           and c.COLUMN_NAME like case when len(@ColumnName) > 0 then @ColumnName else '%' end

       order by c.TABLE_NAME, c.ORDINAL_POSITION

    declare

         @table_schema sysname

       , @table_name sysname

       , @column_name sysname

       , @data_type sysname

       , @exists nvarchar(4000) -- Can be max for SQL2005+

       , @sql nvarchar(4000) -- Can be max for SQL2005+

       , @where nvarchar(4000) -- Can be max for SQL2005+

       , @run nvarchar(4000) -- Can be max for SQL2005+

    while exists (select null from @TabCols) begin

       select top 1

             @table_schema = table_schema

           , @table_name = table_name

           , @exists = 'select null from [' + table_schema + '].[' + table_name + '] where 1 = 0'

           , @sql = 'select ''' + '[' + table_schema + '].[' + table_name + ']' + ''' as TABLE_NAME, * from [' + table_schema + '].[' + table_name + '] where 1 = 0'

           , @where = ''

       from @TabCols

       order by id

       while exists (select null from @TabCols where table_schema = @table_schema and table_name = @table_name) begin

           select top 1

                 @column_name = column_name

               , @data_type = data_type

           from @TabCols

           where table_schema = @table_schema

               and table_name = @table_name

           order by id

           -- Special case for money

           if @data_type in ('money', 'smallmoney') begin

               if isnumeric(@SearchTerm) = 1 begin

                   set @where = @where + ' or [' + @column_name + '] = cast(''' + @SearchTerm + ''' as ' + @data_type + ')' -- could also cast the column as varchar for wildcards

               end

           end

           -- Special case for xml

           else if @data_type = 'xml' begin

               set @where = @where + ' or cast([' + @column_name + '] as nvarchar(max)) like ''' + @SearchTerm + ''''

           end

           -- Special case for date

           else if @data_type in ('date', 'datetime', 'datetime2', 'datetimeoffset', 'smalldatetime', 'time') begin

               set @where = @where + ' or convert(nvarchar(50), [' + @column_name + '], 121) like ''' + @SearchTerm + ''''

           end

           -- Search all other types

           else begin

               set @where = @where + ' or [' + @column_name + '] like ''' + @SearchTerm + ''''

           end

           delete from @TabCols where table_schema = @table_schema and table_name = @table_name and column_name = @column_name

       end

       set @run = 'if exists(' + @exists + @where + ') begin ' + @sql + @where + ' print ''' + @table_name + ''' end'

       print @run

       exec sp_executesql @run

    end

    set nocount off

  • VinhTon Profile Picture
    102 on at

    Definitely can use this, thanks!

  • Suggested answer
    Jothi Krishnan N Profile Picture
    1,897 on at

    Assuming you have access to GP. If it is user created Smartlist, go to the Smartlist report and click Modify button and then go to Options >> Display SQL and mark all the checkboxes in that window to see the complete SQL query used in the smart list.

  • VinhTon Profile Picture
    102 on at

    This is exactly what I'm looking for.

    I do have access to GP but not to create/modify smartlist. Let me request it and confirm this works. Thanks!

  • Jothi Krishnan N Profile Picture
    1,897 on at

    Also you can use the "Create View" option in the same area to convert the SQL query behind it to a SQL View and use it. But you may have to drop and recreate it if user make changes to SL.

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

News and Announcements

Season of Giving Solutions is Here!

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics GP (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans