Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8075505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:00:28+00:00 2026-06-05T15:00:28+00:00

i have a string that contain a sql command, something like this: strCommand =

  • 0

i have a string that contain a sql command,

something like this:

strCommand = “Select [Feild1],
[Feild2]
From TableName
Order By [Feild1] desc” ;

How can find table name in this string?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:00:29+00:00Added an answer on June 5, 2026 at 3:00 pm

    The solutions so far have all gone with the searching within strings approach. You’ve not mentioned if your SQL queries will always look similar, but there are many variants of a query to include which these solutions will break on. Consider…

    • SELECT Field1, Field2 FROM TableName
    • SELECT Field1, Field2 FROM [TableName]
    • SELECT Field1, Field2 FROM dbo.TableName
    • SELECT Field1, Field2 FROM Table1Name, Table2Name

    If the query you’re trying to parse is one you have the database for, you can get SQL server to do the hard work of parsing the query for you, instead of trying to account for all the cases in SQL. You can execute a query using SET SHOWPLAN_ALL ON, which will produce a table of the query plan. You can then analyse the Arguments column, which contains all of the fields the query will involve in a standard format. An example program is below:

    SqlConnection conn = new SqlConnection(CONNECTIONSTRING);
    conn.Open();
    
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = "SET SHOWPLAN_ALL ON";
    cmd.ExecuteNonQuery();
    cmd.CommandText = "SELECT [Field1], [Field2] FROM [TableName]";
    
    DataTable dt = new DataTable();
    dt.Load(cmd.ExecuteReader());
    
    Regex objectRegex = new Regex(@"^OBJECT:\(\[(?<database>[^\]]+)\]\.\[(?<schema>[^\]]+)\]\.\[(?<table>[^\]]+)\]\.\[(?<field>[^\]]+)\]\)$", RegexOptions.ExplicitCapture);
    
    List<string> lstTables = new List<string>();
    foreach (DataRow row in dt.Rows)
    {
        string argument = row["Argument"].ToString();
        if (!String.IsNullOrEmpty(argument))
        {
            Match m = objectRegex.Match(argument);
            if (m.Success)
            {
                string table = m.Groups["schema"] + "." + m.Groups["table"];
                if (!lstTables.Contains(table))
                {
                    lstTables.Add(table);
                }
            }
        }
    }
    
    Console.WriteLine("Query uses the following tables: " + String.Join(", ", lstTables));
    

    This will deal with all forms of query name and return all tables which are involved in the query, no matter how they are included.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that contains a czech character. the string is 0bálka This
If I have a string that contains the html from a page I just
I have a SQL script that will insert a long string into a table.
I have a table storing millions of rows. It looks something like this: Table_Docs
i have a Class called Services in the App_code folder that contain Method like
I currently have a database that gets updated from a legacy application. I'd like
I have strings that contain a variable number of leading hyphens and which may
I have a string that contains HTML image elements that is stored in a
I have a string that contains ampersands. I need to escape each ampersand in
I have a string that contains an escaped URL: http%3A%2F%2Fexample.com%3Ffoo%3Dbar+baz I'm trying to decode

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.