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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
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 TableNameSELECT Field1, Field2 FROM [TableName]SELECT Field1, Field2 FROM dbo.TableNameSELECT Field1, Field2 FROM Table1Name, Table2NameIf 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: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.