本文介绍了T-SQL从Select中插入批量文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在SQL Server 2008中,我有一条select语句在文本输出下方弹出.
In SQL server 2008, I have select statement that spits below text output.
'text3d','text2','text6'
'text3d','text2','text6'
'text1d','text2','text6'
'text1d','text2','text6'
'text18d','text2','text3
'text18d','text2','text3
'text15','text2','text5'
'text15','text2','text5'
.....
.....
我要将上面的输出插入到Test1表中.
I want to insert above output into Test1 table.
create table Test1 (c1 varchar(20),c2 varchar(20),c3 varchar(20))
如果有帮助,我可以修改选择脚本,以其他方式生成上述输出,而不是单引号"和逗号".
I can modify select script that generates above output with something else instead of 'single quote' and 'comma' if it helps.
谢谢.
推荐答案
不是很优化,但可能有效:
Not very optimized, but probably working:
DECLARE @values VARCHAR(MAX)
DECLARE c_select CURSOR FAST_FORWARD FOR
SELECT <your statement here>
OPEN c_select
FETCH NEXT FROM c_select INTO @values
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC('INSERT INTO table2 (col1, col2, col3) VALUES (' + @values + ')')
FETCH NEXT FROM c_select INTO @values
END
CLOSE c_select
DEALLOCATE c_select
这篇关于T-SQL从Select中插入批量文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!