如何在SQL Server数据库中搜索特定字符串?

时间:2017-12-11 17:40:12

标签: sql sql-server

我想在SQL DB中搜索特定字符串。我知道要搜索的数据库,但我不知道搜索字符串在哪个表中。

2 个答案:

答案 0 :(得分:0)

你可以通过单个查询来完成它,它可以帮助我找到任何正在使用的对象和故障排除问题。

DECLARE @TOSEARCH NVARCHAR(50)='获取'

SELECT m.name, modify_date FROM ( SELECT DISTINCT so.name FROM syscomments sc INNER JOIN sysobjects so ON sc.id=so.id WHERE sc.TEXT LIKE '%'+@ToSearch+'%' ) m LEFT JOIN sys.objects ON sys.objects.name = m.name ORDER BY modify_date DESC

答案 1 :(得分:-1)

查看以下代码是否可以为您提供帮助。这段代码我得到了site。我没有试过,但我知道一个尝试过,并说我工作了。

DECLARE @SQL VARCHAR(8000)
DECLARE @filtro VARCHAR(200)
DECLARE @filtro_www VARCHAR(200)

-- start sql declaration
SET @SQL = ''
SET @filtro = '%text_searched%'

SELECT
   tabelas.name   AS Tabela 
  ,colunas.name   AS Coluna
  ,tipos.name     AS Tipo
  ,colunas.length AS Tamanho
INTO
  #result
FROM 
  sysobjects tabelas
  INNER JOIN syscolumns colunas
  ON colunas.id = tabelas.id
  --
  INNER JOIN systypes tipos
  ON tipos.xtype = colunas.xtype
WHERE 
  tabelas.xtype = 'u'
    AND
  -- put here the type of column that will be search
  tipos.name IN('text', 'ntext', 'varchar', 'nvarchar')


-- cursor to search into table
DECLARE cTabelas cursor LOCAL fast_forward FOR
SELECT DISTINCT Tabela FROM #result

DECLARE @nomeTabela VARCHAR(255)

OPEN cTabelas

fetch NEXT FROM cTabelas INTO @nomeTabela

while @@fetch_status = 0
BEGIN

  -- cursor to search for all columns in the current table
  DECLARE cColunas cursor LOCAL fast_forward FOR
  SELECT Coluna, Tipo, Tamanho FROM #result WHERE Tabela = @nomeTabela

  DECLARE @nomeColuna VARCHAR(255)
  DECLARE @tipoColuna VARCHAR(255)
  DECLARE @tamanhoColuna VARCHAR(255)

  OPEN cColunas

  -- mount the columns from select statement
  fetch NEXT FROM cColunas INTO @nomeColuna, @tipoColuna, @tamanhoColuna

  while @@fetch_status = 0
  BEGIN
    -- declare variable
    SET @SQL = 'declare @hasresults bit' + CHAR(13) + CHAR(10) + CHAR(13) + CHAR(10)
    -- cria o select
    SET @SQL = @SQL + 'select' + CHAR(13) + CHAR(10)
    SET @SQL = @SQL + CHAR(9) + '''' + @nomeTabela + ''' AS NomeTabela'
    SET @SQL = @SQL + CHAR(9) + ',' + @nomeColuna + CHAR(13) + CHAR(10)
    -- add a column with type and size
    SET @SQL = @SQL  + CHAR(9) + ',' + '''' + @tipoColuna + ''' AS ''' + @nomeColuna + '_Tipo''' + CHAR(13) + CHAR(10)
    SET @SQL = @SQL  + CHAR(9) + ',' + 'DATALENGTH(' + @nomeColuna + ') AS ''' + @nomeColuna + '_Tamanho_Ocupado''' + CHAR(13) + CHAR(10)    
    SET @SQL = @SQL  + CHAR(9) + ',' + '''' + @tamanhoColuna + ''' AS ''' + @nomeColuna + '_Tamanho_Maximo''' + CHAR(13) + CHAR(10)

    -- define temporary table(#result)
    SET @SQL = @SQL + 'into' + CHAR(13) + CHAR(10) + CHAR(9) + '#result_' + @nomeTabela + CHAR(13) + CHAR(10)
    -- add the from statement
    SET @SQL = @SQL +  'from' + CHAR(13) + CHAR(10) + CHAR(9) + @nomeTabela + CHAR(13) + CHAR(10)
    -- start assembly of the clause where
    SET @SQL = @SQL + 'where' + CHAR(13) + CHAR(10)
    SET @SQL = @SQL + CHAR(9) + @nomeColuna + ' like ''' + @filtro + '''' + CHAR(13) + CHAR(10)

    SET @SQL = @SQL + CHAR(13) + CHAR(10) + 'select @hasresults = count(*) from #result_' + @nomeTabela + CHAR(13) + CHAR(10)
    SET @SQL = @SQL + CHAR(13) + CHAR(10) + 'if @hasresults > 0'
    SET @SQL = @SQL + CHAR(13) + CHAR(10) + 'begin'
    SET @SQL = @SQL + CHAR(13) + CHAR(10) + CHAR(9) + 'select * from #result_' + @nomeTabela
    SET @SQL = @SQL + CHAR(13) + CHAR(10) + 'end' + CHAR(13) + CHAR(10)
    SET @SQL = @SQL + CHAR(13) + CHAR(10) + 'drop table #result_' + @nomeTabela
    SET @SQL = @SQL + CHAR(13) + CHAR(10)

    fetch NEXT FROM cColunas INTO @nomeColuna, @tipoColuna, @tamanhoColuna
    -- uncomment the line below to see Sql generated into window messages
    -- print @sql
    EXEC(@SQL)
    SET @SQL = ''
  END

  close cColunas
  deallocate cColunas

  fetch NEXT FROM cTabelas INTO @nomeTabela
END

close cTabelas
deallocate cTabelas

DROP TABLE #result

有一些葡萄牙语的名字,但只是名字,然后你可以替换。只是名字。