这是我的桌子
| | Id |
|1 |07A83EA7-6E00-4265-906C-FF187FE18EB3|
|2 |B6C10F47-7DEE-4D2B-AA3D-FF27B8FC105C|
|3 |10F56030-0803-4133-B57B-FF3F941A0D60|
是否可以根据行号选择Id
?
我尝试过:
SELECT [Id],ROW_NUMBER() OVER(Order by Id ASC) AS RowLol, Id
但这并不能真正满足我的预期,因为它会生成一个临时字段。
所需功能:
能够遍历所有条目,并将其用作循环中我的UPDATE
的参数
--DECLARE @json NVARCHAR(MAX) = (SELECT Data FROM ProcessEventMessages WHERE Id='AEF11F7B-06C1-4C40-9806-0004CAC6A042')
-- This will only work for ONE instance. Hence why i need an iterative ID, to pass along here^
SELECT * FROM OPENJSON(@json)
WITH (
message varchar(200) '$.message',
machineId varchar(200) '$.machineId',
machineName int '$.machineName',
ipAddress varchar(200) '$.ipAddress',
LocalTime datetime2(7) '$.time'
) AS ChangeTime;
--loop on id
DECLARE @loopCounter INT = 0;
WHILE @loopCounter < (SELECT count(Id) FROM ProcessEventMessages)
BEGIN
PRINT 'changing values...';
UPDATE ProcessEventMessages SET Data = JSON_MODIFY(@json,'$.time', FORMAT(DATEADD(hour,-2,JSON_VALUE(@json,'$.time')),'yyyy-MM-ddTHH:mm:ss.fff')) WHERE ROWNUMBER = @loopCounter)
SET @loopCounter = @loopCounter + 1;
END;
PRINT 'done';
GO
建议:
DECLARE @json NVARCHAR(MAX)
DECLARE @loopCounter INT = 0;
WHILE @loopCounter < (SELECT count(Id) FROM ProcessEventMessages)
BEGIN
PRINT 'Changing...';
SELECT * FROM OPENJSON(@json)
WITH (
message varchar(200) '$.message',
machineId varchar(200) '$.machineId',
machineName int '$.machineName',
ipAddress varchar(200) '$.ipAddress',
LocalTime datetime2(7) '$.time'
) AS ChangeTime;
;WITH CTE AS(
SELECT *,ROW_NUMBER() OVER(Order by Id ASC) AS RowNo
FROM ProcessEventMessages
)
UPDATE CTE
SET Data = JSON_MODIFY(@json,'$.time', FORMAT(DATEADD(hour,-2,JSON_VALUE(@json,'$.time')),'yyyy-MM-ddTHH:mm:ss.fff'))
WHERE RowNo = @loopCounter
SET @loopCounter = @loopCounter + 1;
END;
PRINT 'Done';
GO
尝试崩溃:
DECLARE @loopCounter INT = 0;
WHILE @loopCounter < (SELECT count(Id) FROM ProcessEventMessages)
BEGIN
PRINT 'Changing...';
DECLARE @json NVARCHAR(MAX) = (SELECT Data FROM ProcessEventMessages WHERE RowNo = @loopCounter) -- RowNo isn't declared here. Where would i move it?
SELECT * FROM OPENJSON(@json)
WITH (
message varchar(200) '$.message',
machineId varchar(200) '$.machineId',
machineName int '$.machineName',
ipAddress varchar(200) '$.ipAddress',
LocalTime datetime2(7) '$.time'
) AS ChangeTime;
;WITH CTE AS(
SELECT *,ROW_NUMBER() OVER(Order by Id ASC) AS RowNo
FROM ProcessEventMessages
)
UPDATE CTE
SET Data = JSON_MODIFY(@json,'$.time', FORMAT(DATEADD(hour,-2,JSON_VALUE(@json,'$.time')),'yyyy-MM-ddTHH:mm:ss.fff'))
WHERE RowNo = @loopCounter
SET @loopCounter = @loopCounter + 1;
END;
PRINT 'Done';
GO
示例:
DECLARE @RowNrr int = (SELECT *,ROW_NUMBER() OVER(Order by Id ASC) AS RowNrr FROM ProcessEventMessages)
SET @json = (SELECT Data FROM ProcessEventMessages WHERE @RowNrr = @loopCounter)
...
and then
...
;WITH CTE AS(
SELECT @RowNrr
)
but it doesn't work :/
答案 0 :(得分:1)
假设您将sql server用作dbms,现在,如果我正确理解了您的要求,那么您可能会寻找 CTE ,
;WITH CTE AS(
SELECT [Id],
ROW_NUMBER() OVER(Order by Id ASC) AS RowNo
FROM TableName
)
SELECT ID
FROM CTE
WHERE RowNo IN(1,2,3)
已更新
如果您尝试将更新命令与cte表单集成在一起,则可能看起来像这样。
;WITH CTE AS(
SELECT *,ROW_NUMBER() OVER(Order by Id ASC) AS RowNo
FROM ProcessEventMessages
)
UPDATE CTE
SET Data = JSON_MODIFY(@json,'$.time', FORMAT(DATEADD(hour,-2,JSON_VALUE(@json,'$.time')),'yyyy-MM-ddTHH:mm:ss.fff'))
WHERE RowNo = @loopCounter
注意:我不确定ID上的行号是否一致。因为如果插入新ID,则行号可能会发生变化。
答案 1 :(得分:-1)
((?<![\\])['"])((?:.(?!(?<![\\])\1))*.?)\1