2012-04-27 161 views
1

我正在使用SQL Server 2008並關閉下面的示例HERE搜索數據庫中的所有表並返回一個值?

以下是我到目前爲止的代碼。 (唯一的區別是將它從存儲過程轉換爲查詢並刪除了不需要的模式名稱。)

我想要做的是在結果中添加另一列並給出第一列的數據值。換句話說,如果每個表都以主鍵開始,那麼我的結果將包括找到數據的行的主鍵。這甚至有可能嗎?謝謝。

DECLARE @DataToFind nvarchar(MAX) = '' 
DECLARE @ExactMatch BIT = 0 

DECLARE @Temp TABLE(RowId INT IDENTITY(1,1), SchemaName sysname, TableName sysname, ColumnName SysName, DataType VARCHAR(100), DataFound BIT) 

    INSERT INTO @Temp(TableName,SchemaName, ColumnName, DataType) 
    SELECT C.Table_Name,C.TABLE_SCHEMA, C.Column_Name, C.Data_Type 
    FROM Information_Schema.Columns AS C 
      INNER Join Information_Schema.Tables AS T 
       ON C.Table_Name = T.Table_Name 
     AND C.TABLE_SCHEMA = T.TABLE_SCHEMA 
    WHERE Table_Type = 'Base Table' 
      And Data_Type In ('ntext','text','nvarchar','nchar','varchar','char') 


DECLARE @i INT 
DECLARE @MAX INT 
DECLARE @TableName sysname 
DECLARE @ColumnName sysname 
DECLARE @SchemaName sysname 
DECLARE @SQL NVARCHAR(4000) 
DECLARE @PARAMETERS NVARCHAR(4000) 
DECLARE @DataExists BIT 
DECLARE @SQLTemplate NVARCHAR(4000) 

SELECT @SQLTemplate = CASE WHEN @ExactMatch = 1 
          THEN 'If Exists(Select * 
              From ReplaceTableName 
              Where Convert(nVarChar(4000), [ReplaceColumnName]) 
                 = ''' + @DataToFind + ''' 
             ) 
            Set @DataExists = 1 
           Else 
            Set @DataExists = 0' 
          ELSE 'If Exists(Select * 
              From ReplaceTableName 
              Where Convert(nVarChar(4000), [ReplaceColumnName]) 
                 Like ''%' + @DataToFind + '%'' 
             ) 
            Set @DataExists = 1 
           Else 
            Set @DataExists = 0' 
          END, 
     @PARAMETERS = '@DataExists Bit OUTPUT', 
     @i = 1 

SELECT @i = 1, @MAX = MAX(RowId) 
FROM @Temp 

WHILE @i <= @MAX 
    BEGIN 
     SELECT @SQL = REPLACE(REPLACE(@SQLTemplate, 'ReplaceTableName', QUOTENAME(SchemaName) + '.' + QUOTENAME(TableName)), 'ReplaceColumnName', ColumnName) 
     FROM @Temp 
     WHERE RowId = @i 


     PRINT @SQL 
     EXEC SP_EXECUTESQL @SQL, @PARAMETERS, @DataExists = @DataExists OUTPUT 

     IF @DataExists =1 
      UPDATE @Temp SET DataFound = 1 WHERE RowId = @i 

     SET @i = @i + 1 
    END 

SELECT TableName, ColumnName 
FROM @Temp 
WHERE DataFound = 1 

回答

1

返回主鍵是絕對有可能的。您將需要:

  • 確定名稱,如果每個表中的主鍵列或列。如果你可以堅持一個專欄,它會讓你的生活更輕鬆。
  • 添加一個變量的主鍵值的內容,說@PrimaryKeyValue
  • 修改@SQLTemplate此列的值返回爲@PrimaryKeyValue,像這樣:

    SELECT @SQLTemplate = CASE WHEN @ExactMatch = 1 
              THEN 'select @PrimaryKeyValue = min(' + @PrimaryKeyColumnName + ') 
                from ReplaceTableName 
                where Convert(nVarChar(4000), [ReplaceColumnName]) = ''' + @DataToFind + ''')' 
              ELSE 'select @PrimaryKeyValue = min(' + @PrimaryKeyColumnName + ') 
                from ReplaceTableName 
                where Convert(nVarChar(4000), [ReplaceColumnName]) Like ''%' + @DataToFind + '%'')' 
             END, 
         @PARAMETERS = '@PrimaryKeyValue nvarchar(4000) OUTPUT', 
         @i = 1 
    
相關問題