【问题标题】:Insert multiple rows into temp table with one command in SQL2005在 SQL 2005 中使用一个命令将多行插入临时表
【发布时间】:2010-04-20 23:52:39
【问题描述】:

我有一些格式如下的数据:

-1,-1,-1,-1,701,-1,-1,-1,-1,-1,304,390,403,435,438,439,442,455

我需要将它插入到这样的临时表中:

CREATE TABLE #TEMP
(
Node int
)

这样我就可以将它用于与另一个表中的数据进行比较。

上面的数据代表“节点”列的单独行。

有没有一种简单的方法可以在一个命令中插入这些数据?

此外,数据实际上会以字符串形式出现...所以我需要能够将其连接到 SQL 查询字符串中。如果需要,我显然可以先修改它。

【问题讨论】:

  • 谷歌“sql server 拆分功能”

标签: sql sql-server-2005 tsql


【解决方案1】:

试试类似的东西

CREATE TABLE #TEMP 
( 
    Node int 
) 


DECLARE @textXML XML
DECLARE @data NVARCHAR(MAX), 
        @delimiter NVARCHAR(5)
SELECT  @data = '-1,-1,-1,-1,701,-1,-1,-1,-1,-1,304,390,403,435,438,439,442,455 ',
        @delimiter = ','
SELECT    @textXML = CAST('<d>' + REPLACE(@data, @delimiter, '</d><d>') + '</d>' AS XML)

INSERT INTO #TEMP
SELECT  T.split.value('.', 'nvarchar(max)') AS data
FROM    @textXML.nodes('/d') T(split)

SELECT * FROM #TEMP

DROP TABLE #TEMP

【讨论】:

    【解决方案2】:

    您可以像这样动态创建查询:

    declare @sql varchar(1000)
    set @sql = 'insert into #TEMP select ' + replace(@values, ',', ' union all select ')
    exec @sql
    

    与往常一样,在动态创建查询时,您必须小心,以便只使用受信任的数据。

    【讨论】:

      【解决方案3】:

      我将创建一个返回表变量的函数,然后将该函数加入到 select 中

      用途:

      select * from myTable a 
      inner join dbo.buildTableFromCSV('1,2,3') on a.id = b.theData
      

      这是我的功能

      CREATE  FUNCTION [dbo].[buildTableFromCSV] ( @csvString  varchar(8000) )  RETURNS @myTable TABLE (ID int identity (1,1), theData varchar(100))
      AS    BEGIN 
          DECLARE @startPos       Int         -- position to chop next block of chars from
          DECLARE @currentPos     Int         -- position to current character we're examining
          DECLARE @strLen     Int
      
          DECLARE @c          char(1)         -- current subString
      
          -- variable initalization
          -- -------------------------------------------------------------------------------------------------------------------------------------------------
              SELECT @csvString   = @csvString + ','
              SELECT @startPos        = 1
              SELECT @currentPos  = 1
      
      
      
              SELECT @strLen      = Len(@csvString)
      
      
      
      
          -- loop over string and build temp table
          -- -------------------------------------------------------------------------------------------------------------------------------------------------
      
              WHILE @currentPos <= @strLen BEGIN
                  SET @c = SUBSTRING(@csvString, @currentPos,  1 )        
      
      
      
      
                  IF ( @c = ','  ) BEGIN
      
      
                      IF ( @currentPos - @startPos > 0 ) BEGIN
      
                          INSERT 
                          INTO        @myTable ( theData )
                          VALUES          (  CAST( SUBSTRING ( @csvString, @startPos, @currentPos - @startPos) AS varchar ) )
      
                      END
                      ELSE
                      begin
                          INSERT 
                          INTO        @myTable ( theData )
                          VALUES          (  null )
      
                      end
                      SELECT @startPos = @currentPos + 1
      
                  END
      
                  SET @currentPos = @currentPos + 1
              END
      
              delete from @myTable where theData  is null
      
          return  
      END
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-16
        • 2011-05-08
        • 1970-01-01
        • 1970-01-01
        • 2016-02-15
        • 1970-01-01
        • 2011-03-17
        相关资源
        最近更新 更多