如何从SQLSTRING返回名称表

时间:2014-02-21 09:43:29

标签: vb.net sql-server-2008

如何返回inter SQLSTRING表的名称,例如:

SELECT * FROM TABLE1

然后返回=(表1)

SELECT ID,JOB FROM TABLE1

然后返回=(表1)

1 个答案:

答案 0 :(得分:0)

目前尚不清楚。如果您想使用UNION,您必须知道记录的来源。您可以添加一列:

SELECT T1.ID, T1.JOB, Source = 'Table1' 
FROM TABLE1 T1

UNION ALL

SELECT T2.ID, T2.JOB, Source = 'Table2' 
FROM TABLE2 T2

如果您想要从sql-string中提取表名:

Dim sql = "SELECT * FROM TABLE1"
Dim matches = Regex.Matches(sql, "(?<=from|join)(\s+\w+\b)", RegexOptions.IgnoreCase)
Dim tableNames = matches.Cast(Of Match)().Select(Function(m) m.Value.TrimStart()).ToList()

Console.Write(String.Join(",", tableNames)) ' TABLE1