从sql-server中table2的一行中的不同列中将记录插入为一列,作为table1中的不同行

时间:2018-12-18 11:05:31

标签: sql sql-server database

我有下表:

table1 #TempImagepath
column1 Path nvarchare(800)

table2 SiteImage
column1 SiteID bigint,
column2 Facebookurl nvarchare(800),
column3 Twitterurl nvarchare(800),
column4 Instaurl nvarchare(800)

我想将表2中的数据作为不同的行插入表1中,用于(Facebookurl,Twitterurl,Instaurl)SiteID = '10'

让我们说table2(SiteImage)中有一条记录为:

(10,"/uploads/Sites/1/CategoryImages/WebImages/7ec79e1a-92c2-4d7c-9139-6d177004d766-201701311804409066.jpg","/uploads/Sites/1/CategoryImages/MobileImages/e5ae525f-7dcf-4051-8463-6bb15f520860-201701311804425434.jpg","/uploads/Sites/1/CategoryImages/MobileImages/31d89a5e-5593-4074-881f-d3326b5cf105-201701311804444181.jpg")

然后我的结果应该给table1(#TempImagepath)记录,像这样:

"/uploads/Sites/1/CategoryImages/WebImages/7ec79e1a-92c2-4d7c-9139-6d177004d766-201701311804409066.jpg"
"/uploads/Sites/1/CategoryImages/MobileImages/e5ae525f-7dcf-4051-8463-6bb15f520860-201701311804425434.jpg"
"/uploads/Sites/1/CategoryImages/MobileImages/31d89a5e-5593-4074-881f-d3326b5cf105-201701311804444181.jpg"

1 个答案:

答案 0 :(得分:0)

尝试一下,我认为这可能对您有用

IF OBJECT_ID('Tempdb..#TempImagepath')IS NOT NULL
DROP TABLE #TempImagepath
IF OBJECT_ID('dbo.SiteImage')IS NOT NULL
DROP TABLE SiteImage

CREATE TABLE #TempImagepath
([Path] nvarchar(800))

CREATE TABLE SiteImage
(
 SiteID bigint IDENTITY,
 Facebookurl nvarchar(800),
 Twitterurl nvarchar(800),
 Instaurl nvarchar(800)
 )
 INSERT INTO SiteImage
 SELECT 'Facebookurl','Twitterurl','Instaur'

INSERT INTO #TempImagepath
SELECT 'Row'+ CAST(ROW_NUMBER()OVER(ORDER BY (SELECT 1))AS Varchar(10))+': '+  [Path] 
FROM SiteImage
CROSS APPLY (VALUES (Facebookurl),(Twitterurl),(Instaurl)
)AS A ([Path])

SELECT * FROM #TempImagepath

结果

Path
------------------
Row1: Facebookurl
Row2: Twitterurl
Row3: Instaur