在 SQL2005 中使用一个命令将多行插入到临时表中 [英] Insert multiple rows into temp table with one command in SQL2005

查看:31
本文介绍了在 SQL2005 中使用一个命令将多行插入到临时表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got some data in the following format:

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

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

I need to insert it into a temp table like this:

CREATE TABLE #TEMP
(
Node int
)

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

So that I can use it in a comparison with data in another table.

以上数据代表节点"列的单独行.

The data above represents separate rows of the "Node" column.

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

Is there an easy way to insert this data, all in one command?

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

Also, the data will actually being coming in as seen, as a string... so I need to be able to just concat it into the SQL query string. I can obviously modify it first if needed.

推荐答案

尝试类似的事情

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

这篇关于在 SQL2005 中使用一个命令将多行插入到临时表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆