2017-04-06 77 views
0

我運行以下查詢:子查詢返回的值超過1。當子查詢被用作表達

「子查詢返回超過1個值。這是:

從表

但是得到錯誤搜索特定圖案當子查詢如下=,!=,不允許<,< =,>,> =,或當子查詢用作表達「

DECLARE @SearchStr nvarchar(100) 
SET @SearchStr = '' 

--drop table #Results 

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 = 'RAP1' 

WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL) 
BEGIN 
    SET @ColumnName = 
    (

     SELECT COLUMN_NAME 
     FROM  INFORMATION_SCHEMA.COLUMNS 
     WHERE   
     TABLE_NAME = PARSENAME(@TableName, 1) 
      AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'int', 'decimal') 

    ) 

    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 ColumnName, ColumnValue FROM #Results 

DROP TABLE #Results 

什麼錯誤?如何獲得結果?請幫忙。

回答

0

這很可能是因爲表RAP1有多個列,並且您試圖將該表的每一列的值(名稱)保存在一個變量中(在SET @ColumnName中)。

如果下面的查詢返回超過1行那麼這就是你的問題:

DECLARE @TableName nvarchar(256) 
SET @TableName = 'RAP1' 

SELECT COLUMN_NAME 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = PARSENAME(@TableName, 1) 
    AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'int', 'decimal') 

我會改變你的腳本是這個樣子:

DECLARE @SearchStr nvarchar(100) 
SET @SearchStr = '' 

--drop table #Results 

CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630)) 
SET NOCOUNT ON 
DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110), @PrevColumnName nvarchar(128) 
SET @TableName = '' 
SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''') 

WHILE @TableName IS NOT NULL 
BEGIN 

SET @ColumnName = '' 
SET @PrevColumnName = '' 
SET @TableName = 'RAP1' 

IF @TableName IS NOT NULL 
BEGIN 
    WHILE (@ColumnName IS NOT NULL) 
    BEGIN 

     SELECT TOP 1 @ColumnName = COLUMN_NAME 
     FROM INFORMATION_SCHEMA.COLUMNS 
     WHERE TABLE_NAME = PARSENAME(@TableName, 1) 
      AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'int', 'decimal') 
      AND COLUMN_NAME > COALESCE(@PrevColumnName, @ColumnName) 

     IF @ColumnName = @PrevColumnName 
      begin 
       SET @ColumnName = NULL 
       SET @TableName = NULL 
      end 

     SET @PrevColumnName = @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 
END 
SELECT ColumnName, ColumnValue FROM #Results 
+0

感謝。然而,它並沒有返回具有我正在搜索的特定字符串的所有列(在我的@SearchStr中) – AskMe

+0

@TryingBest那麼,我只解決了腳本中運行腳本時遇到問題的部分。對於你在這裏提到的這個問題,我認爲你需要'''''''LIKE'的符號。 –

+0

我有我喜歡的%符號。這裏:SET @ SearchStr2 = QUOTENAME('%'+ SearchStr +'%','''')我還在做錯什麼? – AskMe

相關問題