Create this function to easily get the table number within a SQL statement.

-- =============================================
-- Create date: 2010.11.01
-- Description: Gets the AX table ID for the table name
-- =============================================
ALTER FUNCTION [dbo].[fnAXTableID]
(
-- Add the parameters for the function here
@tableName nvarchar(40)
)
RETURNS int
AS
BEGIN
-- Declare the return variable here
DECLARE @tableNum int

-- Add the T-SQL statements to compute the return value here
SELECT @tableNum = TableID
FROM SQLDictionary
WHERE [Name] = @tableName
AND FieldID = 0
AND Array = 0

-- Return the result of the function
RETURN @tableNum

END