从另一个表插入表甚至列数不匹配?

时间:2013-01-16 23:07:20

标签: sql sql-server tsql

我正在尝试将数据从一个表(或存储过程)插入临时表。临时表列数与源表列数不匹配。我希望从开始列插入源数据到临时表,其余列将清空字符串列。

这意味着如果Temp表有10列而源表只有3列,则将前3列插入临时表,其余7列为空或空。

示例:

CREATE TABLE #VarTemp(Col1 nvarchar(256), Col2 nvarchar(256), Col3 nvarchar(256),
                      Col4 nvarchar(256), Col5 nvarchar(256), Col6 nvarchar(256),
                      Col7 nvarchar(256), Col8 nvarchar(256), Col9 nvarchar(256),
                      Col10 nvarchar(256))

INSERT into #VarTemp 
    EXEC sp_FindStringInTable 'Nareshbhai%', 'dbo', 'UserstoClean';

SELECT * FROM #VarTemp 

如果我执行单独的

EXEC sp_FindStringInTable 'Nareshbhai%', 'dbo', 'UserstoClean';

我正在

 Constitid  FirstName   LastName    email   status          pincode
 3151502    Nareshbhai  Desai   desanar@iit.edu Inactive    desai

错误是

  

发生错误。检查以确保对象存在。   (0行(s)受影响)

     

消息0,级别11,状态0,行0   当前命令发生严重错误。结果(如果有的话)应该被丢弃。

sp_FindStringInTable

ALTER PROCEDURE [dbo].[sp_FindStringInTable]
    @stringToFind VARCHAR(100), @schema sysname, @table sysname 
AS 
    DECLARE @sqlCommand VARCHAR(8000) 
    DECLARE @where VARCHAR(8000) 
    DECLARE @columnName sysname 
    DECLARE @cursor VARCHAR(8000) 

BEGIN TRY 
   SET @sqlCommand = 'SELECT * FROM ' + @schema + '.' + @table + ' WHERE' 
   SET @where = '' 

   SET @cursor = 'DECLARE col_cursor CURSOR FOR 
      SELECT COLUMN_NAME 
      FROM ' + DB_NAME() + '.INFORMATION_SCHEMA.COLUMNS 
      WHERE TABLE_SCHEMA = ''' + @schema + ''' 
        AND TABLE_NAME = ''' + @table + ''' 
        AND DATA_TYPE IN (''char'', ''nchar'', ''ntext'', ''nvarchar'', ''text'', ''varchar'')' 

   EXEC (@cursor) 

   OPEN col_cursor    
   FETCH NEXT FROM col_cursor INTO @columnName    

   WHILE @@FETCH_STATUS = 0    
   BEGIN    
       IF @where <> '' 
           SET @where = @where + ' OR' 

       SET @where = @where + ' ' + @columnName + ' LIKE ''' + @stringToFind + '''' 
       FETCH NEXT FROM col_cursor INTO @columnName    
   END    

   CLOSE col_cursor    
   DEALLOCATE col_cursor  

   SET @sqlCommand = @sqlCommand + @where 
   --PRINT @sqlCommand 
   EXEC (@sqlCommand)  
END TRY 
BEGIN CATCH 
   PRINT 'There was an error. Check to make sure object exists.' 
   IF CURSOR_STATUS('variable', 'col_cursor') <> -3 
   BEGIN 
       CLOSE col_cursor    
       DEALLOCATE col_cursor  
   END 
END CATCH
GO

1 个答案:

答案 0 :(得分:1)

这不会起作用,因为它出现在这里:

''SELECT * FROM'+ @schema +'。' + @table +'WHERE''

您正在加载动态sql以在表中生成ALL COLUMNS的列结果。除非所有这些表都明确地只有你明确定义的10列,否则会破坏。为什么需要动态选择表到临时表?如果您只想查找具有特定类型数据类型的表并选择前十个结果,那么这将有效。

declare @Temp table ( id int identity, val varchar(64) )

--insert into @Temp
select distinct
  'select top 10 * from ' + s.name + '.' + t.name

from sys.tables t (nolock) 
    join sys.schemas s (nolock) on t.schema_id = s.schema_id
    join sys.columns c (nolock) on t.object_id = c.object_id
    join sys.types ty (nolock) on c.system_type_id = ty.system_type_id
        and ty.name in ('char', 'nchar', 'ntext', 'nvarchar')  --etc, if you want to find specific column types listed only

declare @Current int = 1, @max int, @SQL varchar(max);

select @Max = max(id) from @Temp

while @Current <= @Max
BEGIN
    select @SQL = val from @Temp where id = @Current

    exec (@SQL)

    set @Current += 1;
END