如何把多个用逗号隔开的值分别写入表怎么写?

时间:2021-07-11 14:57:59
insert into table(id,title) values("这里怎么写?","标题") where id in("1,2,3,4,5,6")

我要得到的结果是
id     title
1       标题
2       标题
3       标题
4       标题
5       标题
6       标题

17 个解决方案

#1


INSERT INTO TABLE(ID,TITLE)
SELECT 1,'标题' UNION ALL
SELECT 2,'标题' UNION ALL
SELECT 3,'标题' UNION ALL
SELECT 4,'标题' UNION ALL
SELECT 5,'标题' UNION ALL
SELECT 6,'标题'

#2


1,2,3,4,5,6
是个变量啊这样写要有多少啊!???

#3


变量的话用动态SQL

#4


CREATE   FUNCTION [dbo].[Split]
    (
        @Sql VARCHAR(8000),
        @Splits VARCHAR(100)
    )
RETURNS @temp TABLE (c VARCHAR(100), t VARCHAR(100))
AS
BEGIN
    DECLARE @i INT
    SET @Sql = RTRIM(LTRIM(@Sql))
    SET @i = CHARINDEX(@Splits, @Sql)
    WHILE @i >= 1
    BEGIN
        INSERT @temp VALUES(LEFT(@Sql, @i - 1),'标题')
        SET @Sql = SUBSTRING(@Sql, @i + LEN(@Splits), LEN(@Sql) - @i)
        SET @i = CHARINDEX(@Splits, @Sql)
    END
    
    IF @Sql <> ''
        INSERT @temp VALUES (@Sql, '标题')
    RETURN
END

楼主用这个自定义函数看看吧.

#5


如果是想根据一个字符串插入多个值就只能用动态SQL了

#6


楼上具体怎么写?

#7


declare @id nvarchar(100),@s nvarchar(200)
set @id=N'1,2,3,4,5'
set @s=N'select '+replace(@id,',',N',''标题1'' union all select ')
set  @s=@s+N',''标题1'''
insert [table](id,title)exec(@s)

#8


if object_id('[table]')is not null drop table [table]
go
create table [table](ID int,title nvarchar(10))
declare @id nvarchar(100),@s nvarchar(200)
set @id=N'1,2,3,4,5'
set @s=N'select '+replace(@id,',',N',''標題'' union all select ')
set  @s=@s+N',''標題'''
insert [table](id,title)exec(@s)
select * from [table]
/*
ID          title
----------- ----------
1           標題
2           標題
3           標題
4           標題
5           標題

(5 個資料列受到影響)

*/

#9


引用 8 楼 wufeng4552 的回复:
SQL codeifobject_id('[table]')isnotnulldroptable[table]gocreatetable[table](IDint,titlenvarchar(10))declare@idnvarchar(100),@snvarchar(200)set@id=N'1,2,3,4,5'set@s=N'select'+replace(@id,',',N',''標題'' union all select')set@s=@s+N',''標題'''insert[table](id,title)exec(@s)select*from[table]/*
ID          title
----------- ----------
1           標題
2           標題
3           標題
4           標題
5           標題

(5 個資料列受到影響)*/

水哥正解

#10


DECLARE @STR VARCHAR(8000)
DECLARE @SQL VARCHAR(8000)
SET @STR='1,2,3,4,5,6'
SET @SQL='SELECT ID='+REPLACE(@STR,',',' UNION ALL SELECT ')

--SELECT @SQL

CREATE TABLE #T (ID INT)
INSERT #T EXEC( @SQL)

SELECT ID,'标题'AS TITLE FROM #T


INSERT INTO TB (ID,TITLE) SELECT ID,'标题'AS TITLE FROM #T

/*
ID          TITLE 
----------- ----- 
1           标题
2           标题
3           标题
4           标题
5           标题
6           标题

(所影响的行数为 6 行)

*/

#11


if object_id('tb') is not null
  drop table tb
go
create table tb( ID int,[text] varchar(100),)

go
declare @s varchar(8000)
set @s='1,3,6,10,11,17'
insert tb
select
id=substring(@s, number, charindex(',', @s + ',', number) - number),
'标题'
from master..spt_values
where type='p' 
    and substring(',' + @s,number,1) = ',' 

select * from tb

/*
ID          text
----------- ----------------------------------------------------------------------------------------------------
1           标题
3           标题
6           标题
10          标题
11          标题
17          标题

(6 行受影响)


*/

#12


写个自定义函数

#13


8楼的水哥 nvarchar(100),@s nvarchar(200)
这个值是不是自已可以改大点啊?以免'1,2,3,4,5,6'这个值太长的话就写不完,是不是这样?

#14


恩对,改成NVARCHAR(4000)就行了

#15


我刚试了下两个值都改成了4000

运行这个结果错误
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: 'sele' 附近有语法错误。

declare @id nvarchar(100),@s nvarchar(200)
set @id=N'567,568,569,570,571,572,573,574,575,576,577,578,579,580,581,582,583,584,585,586,587,588,589,590,591,592,593,594,595,596,597,598,599,600,601,602,603,604,605,606,607,608,609,610,611,612,613,614,615,616,617,618,619,620,621,622,623,624,625,626,627,628,629,630,631,632,633,634,635,636,637,638,639,640,641,642,643,644,645,646,647,648,649,650,651,652,653,654,655,656,657,658,659,660,661,662,663,664,665,666,667,668,669,670,671,672,673,674,675,676,677,678,679,680,681,682,683,684,685,686,687,688,689,690,691,692,693,694,695,696,697,698,699,700,701,702,703,704,705,706,707,708,709,710,711,712,713,714,715,716,717,718,719,720,721,722,723,724,725,726,727,728,729,730,731,732,733,734,735,736,737,738,739,740,741,742,743,744,745,746,747,748,749,750,751,752,753,754,755,756,757,758,759,760,761,762,763,764,765,766,767,768,769,770,771,772,773,774,775,776,777,778,779,780,781,782,783,784,785,786,787,788,789,790,791,792,793,794,795,796,797,798,799,800,801,802,803,804,805,806,807,808,809,810,811,812,813,814,815,816,817,818,819,820,821,822,823,824,825,826,827,828,829,830,831,832,833,834,835,836,837,838,839,840,841,842,843,844,845,846,847,848,849,850,851,852,853,854,855,856,857,858,859,860,861,862,863,864,865,866,867,868,869,870,871,872,873,874,875,876,877,878,879,880,881,882,883,884,885,886,887,888,889,890,891,892,893,894,895,896,897,898,899,900,901,902,903,904,905,906,907,908,909,910,911,912,913,914,915,916,917,918,919,920,921,922,923,924,925,926,927,928,929,930,931,932,933,934,935,936,937,938,939,940,941,942,943,944,945,946,947,948,949,950,951,952,953,954,955,956,957,958,959,960'
set @s=N'select '+replace(@id,',',N',''标题1'' union all select ')
set  @s=@s+N',''标题1'''
insert [table](id,title)exec(@s)

#16


写错了我写的是4000,发上来忘改了

#17


把NVARCHAR(4000)改成VARCHAR(8000)试试

#1


INSERT INTO TABLE(ID,TITLE)
SELECT 1,'标题' UNION ALL
SELECT 2,'标题' UNION ALL
SELECT 3,'标题' UNION ALL
SELECT 4,'标题' UNION ALL
SELECT 5,'标题' UNION ALL
SELECT 6,'标题'

#2


1,2,3,4,5,6
是个变量啊这样写要有多少啊!???

#3


变量的话用动态SQL

#4


CREATE   FUNCTION [dbo].[Split]
    (
        @Sql VARCHAR(8000),
        @Splits VARCHAR(100)
    )
RETURNS @temp TABLE (c VARCHAR(100), t VARCHAR(100))
AS
BEGIN
    DECLARE @i INT
    SET @Sql = RTRIM(LTRIM(@Sql))
    SET @i = CHARINDEX(@Splits, @Sql)
    WHILE @i >= 1
    BEGIN
        INSERT @temp VALUES(LEFT(@Sql, @i - 1),'标题')
        SET @Sql = SUBSTRING(@Sql, @i + LEN(@Splits), LEN(@Sql) - @i)
        SET @i = CHARINDEX(@Splits, @Sql)
    END
    
    IF @Sql <> ''
        INSERT @temp VALUES (@Sql, '标题')
    RETURN
END

楼主用这个自定义函数看看吧.

#5


如果是想根据一个字符串插入多个值就只能用动态SQL了

#6


楼上具体怎么写?

#7


declare @id nvarchar(100),@s nvarchar(200)
set @id=N'1,2,3,4,5'
set @s=N'select '+replace(@id,',',N',''标题1'' union all select ')
set  @s=@s+N',''标题1'''
insert [table](id,title)exec(@s)

#8


if object_id('[table]')is not null drop table [table]
go
create table [table](ID int,title nvarchar(10))
declare @id nvarchar(100),@s nvarchar(200)
set @id=N'1,2,3,4,5'
set @s=N'select '+replace(@id,',',N',''標題'' union all select ')
set  @s=@s+N',''標題'''
insert [table](id,title)exec(@s)
select * from [table]
/*
ID          title
----------- ----------
1           標題
2           標題
3           標題
4           標題
5           標題

(5 個資料列受到影響)

*/

#9


引用 8 楼 wufeng4552 的回复:
SQL codeifobject_id('[table]')isnotnulldroptable[table]gocreatetable[table](IDint,titlenvarchar(10))declare@idnvarchar(100),@snvarchar(200)set@id=N'1,2,3,4,5'set@s=N'select'+replace(@id,',',N',''標題'' union all select')set@s=@s+N',''標題'''insert[table](id,title)exec(@s)select*from[table]/*
ID          title
----------- ----------
1           標題
2           標題
3           標題
4           標題
5           標題

(5 個資料列受到影響)*/

水哥正解

#10


DECLARE @STR VARCHAR(8000)
DECLARE @SQL VARCHAR(8000)
SET @STR='1,2,3,4,5,6'
SET @SQL='SELECT ID='+REPLACE(@STR,',',' UNION ALL SELECT ')

--SELECT @SQL

CREATE TABLE #T (ID INT)
INSERT #T EXEC( @SQL)

SELECT ID,'标题'AS TITLE FROM #T


INSERT INTO TB (ID,TITLE) SELECT ID,'标题'AS TITLE FROM #T

/*
ID          TITLE 
----------- ----- 
1           标题
2           标题
3           标题
4           标题
5           标题
6           标题

(所影响的行数为 6 行)

*/

#11


if object_id('tb') is not null
  drop table tb
go
create table tb( ID int,[text] varchar(100),)

go
declare @s varchar(8000)
set @s='1,3,6,10,11,17'
insert tb
select
id=substring(@s, number, charindex(',', @s + ',', number) - number),
'标题'
from master..spt_values
where type='p' 
    and substring(',' + @s,number,1) = ',' 

select * from tb

/*
ID          text
----------- ----------------------------------------------------------------------------------------------------
1           标题
3           标题
6           标题
10          标题
11          标题
17          标题

(6 行受影响)


*/

#12


写个自定义函数

#13


8楼的水哥 nvarchar(100),@s nvarchar(200)
这个值是不是自已可以改大点啊?以免'1,2,3,4,5,6'这个值太长的话就写不完,是不是这样?

#14


恩对,改成NVARCHAR(4000)就行了

#15


我刚试了下两个值都改成了4000

运行这个结果错误
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: 'sele' 附近有语法错误。

declare @id nvarchar(100),@s nvarchar(200)
set @id=N'567,568,569,570,571,572,573,574,575,576,577,578,579,580,581,582,583,584,585,586,587,588,589,590,591,592,593,594,595,596,597,598,599,600,601,602,603,604,605,606,607,608,609,610,611,612,613,614,615,616,617,618,619,620,621,622,623,624,625,626,627,628,629,630,631,632,633,634,635,636,637,638,639,640,641,642,643,644,645,646,647,648,649,650,651,652,653,654,655,656,657,658,659,660,661,662,663,664,665,666,667,668,669,670,671,672,673,674,675,676,677,678,679,680,681,682,683,684,685,686,687,688,689,690,691,692,693,694,695,696,697,698,699,700,701,702,703,704,705,706,707,708,709,710,711,712,713,714,715,716,717,718,719,720,721,722,723,724,725,726,727,728,729,730,731,732,733,734,735,736,737,738,739,740,741,742,743,744,745,746,747,748,749,750,751,752,753,754,755,756,757,758,759,760,761,762,763,764,765,766,767,768,769,770,771,772,773,774,775,776,777,778,779,780,781,782,783,784,785,786,787,788,789,790,791,792,793,794,795,796,797,798,799,800,801,802,803,804,805,806,807,808,809,810,811,812,813,814,815,816,817,818,819,820,821,822,823,824,825,826,827,828,829,830,831,832,833,834,835,836,837,838,839,840,841,842,843,844,845,846,847,848,849,850,851,852,853,854,855,856,857,858,859,860,861,862,863,864,865,866,867,868,869,870,871,872,873,874,875,876,877,878,879,880,881,882,883,884,885,886,887,888,889,890,891,892,893,894,895,896,897,898,899,900,901,902,903,904,905,906,907,908,909,910,911,912,913,914,915,916,917,918,919,920,921,922,923,924,925,926,927,928,929,930,931,932,933,934,935,936,937,938,939,940,941,942,943,944,945,946,947,948,949,950,951,952,953,954,955,956,957,958,959,960'
set @s=N'select '+replace(@id,',',N',''标题1'' union all select ')
set  @s=@s+N',''标题1'''
insert [table](id,title)exec(@s)

#16


写错了我写的是4000,发上来忘改了

#17


把NVARCHAR(4000)改成VARCHAR(8000)试试