Advertisement
hecrus

TreeOrder SaveField

Nov 4th, 2020
2,570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.10 KB | None | 0 0
  1. CREATE PROCEDURE [dbo].[fm_tst-form-order_saveEditableField]
  2.     @pk nvarchar(128),      -- id for item
  3.     @fieldCode nvarchar(64),    -- form field
  4.     @value nvarchar(max),     -- field value
  5.     @username nvarchar(256)  -- current user
  6. AS
  7. BEGIN
  8.  
  9. if(@fieldCode='f2') begin
  10.     declare @formItemID  nvarchar(128) = '', @elementID int = 0, @parentId  int = 0, @ord int =0, @temp nvarchar(256)='', @index int
  11.    
  12.     set @elementID = try_cast(dbo.str_splitPart(@value, ',',1) as int)
  13.     set @temp = dbo.str_splitPart(@value,',',2)
  14.    
  15.     set @parentId = try_cast(dbo.str_splitPart(@temp, ',',1) as int)
  16.     set @ord = try_cast(dbo.str_splitPart(@temp, ',',2) as int)
  17.    
  18.     set @temp = cast(@parentId + @ord + @elementID as nvarchar)
  19.     exec as_print @str  = @temp
  20.    
  21.     update tst_categories
  22.     set parentID = nullif(@parentId, 0), ord = @ord
  23.     where id = @elementID  
  24.    
  25.    
  26.     declare @t table(id int,  ord int)
  27.     insert into @t
  28.     select id, ord from tst_categories
  29.     where  isnull(parentID, 0) = @parentId  and id <>@elementID
  30.     order by ord,id
  31.    
  32.     set @index = 0
  33.     -- обновляем порядок у предыщущих (всех у кого номер меньше)
  34.     while (@index <@ord-1) begin
  35.         update tst_categories
  36.         set  ord = @index+1
  37.         where id in (select id from @t
  38.                 order by ord,id
  39.                 OFFSET @index ROWS
  40.                 FETCH NEXT 1 ROWS ONLY
  41.                 )
  42.         set @index = @index + 1    
  43.     end
  44.    
  45.  
  46.     set @index = @ord
  47.     declare @count int = (select count(id) from tst_categories
  48.                  where isnull(parentID, 0) = @parentId )
  49.     -- элементы после теущего
  50.     while (@index <@count) begin
  51.         update tst_categories
  52.         set  ord = @index+1+10000
  53.         where id in (select id from @t
  54.                  order by ord,id
  55.                 OFFSET @index-1 ROWS
  56.                 FETCH NEXT 1 ROWS ONLY
  57.                 ) and id <>@elementID
  58.         set @index = @index + 1    
  59.     end
  60.    
  61.    
  62.     select 1 Result, '' Msg
  63.     return
  64. end
  65.  
  66. select 0 Result, 'Не найден код сущности' Msg
  67.  
  68. END
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement