Sql Veritabanı Tüm Tablo,Kolon ve İçeriklerde Arama Yapmak
<blockquote> <p>CREATE PROC SearchAllTables</p> <p><br /> (<br /> @SearchStr nvarchar(100)<br /> )<br /> AS<br /> BEGIN<br /> CREATE TABLE #Results (TableName NVARCHAR(370),ColumnName nvarchar(370), ColumnValue nvarchar(3630))</p> <p>SET NOCOUNT ON</p> <p>DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)<br /> SET @TableName = ''<br /> SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')</p> <p>WHILE @TableName IS NOT NULL<br /> BEGIN<br /> SET @ColumnName = ''<br /> SET @TableName =<br /> (<br /> SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))<br /> FROM INFORMATION_SCHEMA.TABLES<br /> WHERE TABLE_TYPE = 'BASE TABLE'<br /> AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName<br /> AND OBJECTPROPERTY(<br /> OBJECT_ID(<br /> QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)<br /> ), 'IsMSShipped'<br /> ) = 0<br /> )</p> <p>WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)<br /> BEGIN<br /> SET @ColumnName =<br /> (<br /> SELECT MIN(QUOTENAME(COLUMN_NAME))<br /> FROM INFORMATION_SCHEMA.COLUMNS<br /> WHERE TABLE_SCHEMA = PARSENAME(@TableName, 2)<br /> AND TABLE_NAME = PARSENAME(@TableName, 1)<br /> AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar')<br /> AND QUOTENAME(COLUMN_NAME) > @ColumnName<br /> )</p> <p>IF @ColumnName IS NOT NULL<br /> BEGIN<br /> INSERT INTO #Results<br /> EXEC<br /> (<br /> 'SELECT ''' +@TableName+ ''', ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630)<br /> FROM ' + @TableName + ' (NOLOCK) ' +<br /> ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2<br /> )<br /> END<br /> END<br /> END</p> <p>SELECT * FROM #Results</p> <p>END</p> </blockquote> <p>Örnek kullanım </p> <blockquote> <p>exec SearchAllTables TESLİMATŞART1</p> </blockquote> <p>Alıntıdır : <a href="https://www.ozgurguler.net/blog/sql-veritabani-tum-tablokolon-ve-iceriklerde-arama-yapmak/">https://www.ozgurguler.net/blog/sql-veritabani-tum-tablokolon-ve-iceriklerde-arama-yapmak/</a></p>
0 Yorum