gpt4 book ai didi

sql - 在SSMS中,如何找到包含星号,一些文本和另一个星号的字符串?

转载 作者:行者123 更新时间:2023-12-02 04:37:47 27 4
gpt4 key购买 nike

我们有一个包含很多SP和View的数据库,其中大多数都是很棒的,但其中有些却带有模糊的标准(以前的员工是使用翻译程序从MS Access转移过来的)。

例如,有带有critera的SP,例如:

WHERE dbo.something NOT LIKE '*TEST*'

进行此设置的人的意思是:
WHERE dbo.something NOT LIKE '%TEST%'

我发现一些人已经在使用此方法,但是我必须键入确切的文本。用%字符替换单词TEST不起作用(%返回0条记录,TEST返回一些记录)。
USE [databasename]
SELECT @SEARCHSTRING = '*TEST*', @notcontain = ''

SELECT DISTINCT sysobjects.name AS [Object Name]
,case when sysobjects.xtype = 'P' then 'Stored Proc'
when sysobjects.xtype = 'TF' then 'Function'
when sysobjects.xtype = 'TR' then 'Trigger'
when sysobjects.xtype = 'V' then 'View'
end as [Object Type]
,USER_NAME(sysobjects.uid) AS trigger_owner
--,s.name AS table_schema
,OBJECT_NAME(parent_obj) AS table_name
FROM sysobjects,syscomments
WHERE sysobjects.id = syscomments.id
AND sysobjects.type in ('P','TF','TR','V')
AND sysobjects.category = 0
AND CHARINDEX(@SEARCHSTRING,syscomments.text)>0
AND ((CHARINDEX(@notcontain,syscomments.text)=0
or CHARINDEX(@notcontain,syscomments.text)<>0))

我想知道是否有人知道在所有带有星号,一些文本和另一个星号的对象中搜索字符串的方式。
非常感谢

最佳答案

这是另一位开发人员给我的。也许这可以解决问题?

DECLARE @SearchStr nvarchar(100)
SET @SearchStr = 'Coincide'--'Absolute Value of the Difference' --'DOIRteDc' --'Smartin1' -- 'Rgillesp'--'--'## YOUR STRING HERE ##'

-- Copyright © 2002 Narayana Vyas Kondreddi. All rights reserved.
-- Purpose: To search all columns of all tables for a given search string
-- Written by: Narayana Vyas Kondreddi
-- Site: http://vyaskn.tripod.com
-- Updated and tested by Tim Gaunt
-- http://www.thesitedoctor.co.uk
-- http://blogs.thesitedoctor.co.uk/tim/2010/02/19/Search+Every+Table+And+Field+In+A+SQL+Server+Database+Updated.aspx
-- Tested on: SQL Server 7.0, SQL Server 2000, SQL Server 2005 and SQL Server 2010
-- Date modified: 03rd March 2011 19:00 GMT
CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))

SET NOCOUNT ON

DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
SET @TableName = ''
SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')

WHILE @TableName IS NOT NULL

BEGIN
SET @ColumnName = ''
SET @TableName =
(
SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
AND OBJECTPROPERTY(
OBJECT_ID(
QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
), 'IsMSShipped'
) = 0
)

WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)

BEGIN
SET @ColumnName =
(
SELECT MIN(QUOTENAME(COLUMN_NAME))
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = PARSENAME(@TableName, 2)
AND TABLE_NAME = PARSENAME(@TableName, 1)
AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'int', 'decimal')
AND QUOTENAME(COLUMN_NAME) > @ColumnName
)

IF @ColumnName IS NOT NULL

BEGIN
INSERT INTO #Results
EXEC
(
'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) FROM ' + @TableName + ' (NOLOCK) ' +
' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
)
END
END
END

SELECT distinct ColumnName, ColumnValue FROM #Results

DROP TABLE #Results

关于sql - 在SSMS中,如何找到包含星号,一些文本和另一个星号的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40594076/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com