让我尽量使其易于理解。 我有一个名为' Script_Text'的列的表。此列包含一个脚本。比如,一个非常长的脚本,带有类名,函数名,整个钻。 我试图在所有这些脚本上找到函数名称。我不想单独检查每一个人。
这不是我想要的:
select * from table where script_text like '%function%'
脚本或查询,并不重要。想要它用于SQL Server。我的数据库不超过200个表。不要关心效率。 感谢
编辑1:我对查询看起来像什么的假设可能会让你失望,只是忽略它。对于我认为这会如何起作用,我可能是愚蠢的。
此外,所有功能都以Function
关键字开头。例如:Function GetUserName()
。我希望我的查询返回GetUserName()
。
我正在使用的其中一个表格是ObjectSymbol
。该表有3列。 SymbolName
,Description
,Script_Text
。
一个特定符号名的Script_Text列的内容是此
Function PostSearch() As Long
' Delete any @DFLT or @T records from the search results
DO something
' Do more things
End Function
成功的查询将返回该特定符号名的PostSearch()。
编辑3:
imports System
imports System.Diagnostics
Imports System.XML
Imports System.Data
Imports Microsoft.VisualBasic
Class HookScript
Inherits FcProcFuncSetEvent
Function presave() as long
Do stuff here
End Function
Function IngrPostRowUpdate() as Long
Do more stuff here
RETURN 1
End Function
Function postsave() As Long
Do some more stuff here
End Function
Private Function GetValue(val as Object) as String
If IsDBNull(val) Then
GetValue = ""
Else
GetValue = CStr(val)
End If
End Function
End Class
GetValue
是查询返回的内容。
答案 0 :(得分:2)
这个怎么样?
首先,我将表变量声明为模拟测试场景。
DECLARE @YourTable TABLE(ID INT IDENTITY,SomeScript NVARCHAR(MAX));
INSERT INTO @YourTable VALUES
(
N'Do something here
blah
blah
private function HereIsAFunctionName(){
Some body
}
public function SomeOtherFunction(){blah}
private function OneMore(){
Body
}
')
,(
N'Do something here
blah
blah
private function ThisIsAFunction(){
Some body
}
');
第一部分(CTE)将使用字符串拆分技巧通过function
替换单词</x><x>
(带空格)来创建XML块。然后,您可以读取每个块的开头直到开头(
。您可以阅读结束)
以获取参数列表。
WITH Casted AS
(
SELECT ID
,CAST(N'<x>' + REPLACE((SELECT SomeScript AS [*] FOR XML PATH('')),N' function ',N'</x><x>') + N'</x>' AS XML) AS SplitMe
FROM @YourTable
)
SELECT Casted.ID
,LEFT(x.value(N'.',N'nvarchar(max)'),CHARINDEX('(',x.value(N'.',N'nvarchar(max)'))-1) AS Part
FROM Casted
OUTER APPLY Casted.SplitMe.nodes(N'x[position()!=1]') AS A(x)
结果
ID Part
1 HereIsAFunctionName
1 SomeOtherFunction
1 OneMore
2 ThisIsAFunction
WITH Casted AS
(
SELECT ID
,CAST(N'<x>' + REPLACE((SELECT REPLACE(SomeScript,'end function','') AS [*] FOR XML PATH('')),N' function ',N'</x><x>') + N'</x>' AS XML) AS SplitMe
FROM @YourTable
)
SELECT Casted.ID
,LEFT(x.value(N'.',N'nvarchar(max)'),CHARINDEX(')',x.value(N'.',N'nvarchar(max)'))) AS Part
FROM Casted
OUTER APPLY Casted.SplitMe.nodes(N'x[position()!=1]') AS A(x)
你的问题是由换行引起的,我预计空白......
试试这个:
DECLARE @YourTable TABLE(ID INT IDENTITY,SomeScript NVARCHAR(MAX));
INSERT INTO @YourTable VALUES
(
N'Do something here
blah
blah
private function HereIsAFunctionName(){
Some body
}
public function SomeOtherFunction(){blah}
private function OneMore(){
Body
}
')
,(
N'imports System
imports System.Diagnostics
Imports System.XML
Imports System.Data
Imports Microsoft.VisualBasic
Class HookScript
Inherits FcProcFuncSetEvent
Function presave() as long
Do stuff here
End Function
Function IngrPostRowUpdate() as Long
Do more stuff here
RETURN 1
End Function
Function postsave() As Long
Do some more stuff here
End Function
Private Function GetValue(val as Object) as String
If IsDBNull(val) Then
GetValue = ""
Else
GetValue = CStr(val)
End If
End Function
End Class
');
- 查询
WITH Casted AS
(
SELECT ID
,CAST(N'<x>' + REPLACE((SELECT REPLACE(REPLACE(REPLACE(SomeScript,'end function',''),CHAR(13),' '),CHAR(10),' ') AS [*] FOR XML PATH('')),N' function ',N'</x><x>') + N'</x>' AS XML) AS SplitMe
FROM @YourTable
)
SELECT Casted.ID
,LEFT(x.value(N'.',N'nvarchar(max)'),CHARINDEX(')',x.value(N'.',N'nvarchar(max)'))) AS Part
FROM Casted
OUTER APPLY Casted.SplitMe.nodes(N'x[position()!=1]') AS A(x);