sql - 插入 ... 从表中选择列 .... 加入 #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
我希望我都清楚了。
谢谢你的帮助
解决方案
抱歉打扰...已经工作了..我通过加入同一张表犯了一个简单的错误,没有解决问题..也许它可能对某人有帮助,所以我在这里过去了完整的代码:
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
推荐阅读
- flutter - 从 _widgetOptions.elementAt 调用时 ListView 不显示
- python - PySpark:如何将一列拆分为 3 列
- python - 如何将用户计数器添加到 BMI 计算器?
- python - 我在哪里可以获得 python 应用程序的 Twitter 访问令牌?
- java - 如何从 void 方法中检索对象
- android - ()(函数)在类中定义并调用时不起作用
- java - 在 Java WebApp 中同步的位置
- firebase - Firebase 和 json 服务器
- json - 如何通过 JSON(列格式)获取 Sharepoint 列表中的当前用户
- java - 如何从 Java 中的 BotD 中隐藏 Geckodriver 中的 WebDriver?