首页 > 解决方案 > 插入 ... 从表中选择列 .... 加入 #temptable

问题描述

如您所见,我是新来的。也是 SQL Server 的新手。我已经使用 SSMS 两个星期了。所以我在学习,每天都有新的问题。这个问题似乎不是什么大问题;但是,我无法处理它。

我有一个字符串:

' texttype;commment|texttype2;comment2'

我必须将其拆分并放入列注释作为现有表列中的新插入。我已经使用SPLIT了产生这个的函数#temptable

items
--------
texttype;commment
texttype2;comment2

然后我使用 substring 和 charindex 在#temptable2 中有这个:

types                | commentary
---------------------+------------
texttype             | commment
texttype2            | comment2

我必须将此评论和评论2插入到此#temptable2中的特定现有表中

这是我的问题:如何做到这一点?使用什么样的Join?ofc 目标表有两列以上。不知道如何从#temptable2 定义这个插入/连接

目标表类似于:

ID | ID_Jobs | Column for comments | Column for types | A few more columns

我希望我都清楚了。

谢谢你的帮助

标签: sqlsql-serverjoinselectsql-insert

解决方案


抱歉打扰...已经工作了..我通过加入同一张表犯了一个简单的错误,没有解决问题..也许它可能对某人有帮助,所以我在这里过去了完整的代码:

GO
ALTER PROCEDURE AddCommentToJobFromString (
        @ID_JOBS int, 
        @CommentString nvarchar(2000),
        @OPERACTIVE INT
        )
AS 
BEGIN
        IF OBJECT_ID('tempdb.dbo.#temp', 'U') IS NOT NULL
        DROP TABLE #temp;

SELECT * into #temp from dbo.Split(@CommentString , '|') as t1
SELECT 
        SUBSTRING(t.items, 1 , CHARINDEX(';',t.items)-1) as typuwag,
        SUBSTRING(t.items,  CHARINDEX(';',t.items)+1, LEN(t.items)) as trescuwagi
INTO #temp2 
FROM #temp t 

INSERT INTO Uwagi (ID_Jobs, Uwaga, ID_TypyUwag, ID_Operatorzy_Creator, ID_Operatorzy_Last, DateCreated, LastModified)
--DECLARE @OPERACTIVE INT = 4628
--DECLARE @ID_JOBS INT = 65141
Select  
        @ID_JOBS,
        t2.trescuwagi,  
        TU.ID,
        @OPERACTIVE,
        @OPERACTIVE,
        getdate(),
        getdate()
        --t2.typuwag




FROM TypyUwag TU
inner join #temp2 t2
ON TU.TypUwagi = t2.typuwag  COLLATE DATABASE_DEFAULT

END
GO
EXEC AddCommentToJobFromString @ID_JOBS = 65141, @CommentString ='qwe;Tech visual check|Kosztorys;Estimate not ready yet', @OPERACTIVE = 4628

推荐阅读