powered by simpleCommunicator - 2.0.60     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / Microsoft SQL Server [игнор отключен] [закрыт для гостей] / Вставить файл @name в таблицу FileTable в субдиректорию @dir (первый уровень )
2 сообщений из 2, страница 1 из 1
Вставить файл @name в таблицу FileTable в субдиректорию @dir (первый уровень )
    #39594416
aleks222
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Вставить файл @name в таблицу FileTable
в субдиректорию @dir (первый уровень ).

Это работает. Но хочется вставит "прямо". Без update.
Есть идеи?

Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
begin transaction;

        if not exists (select * from TourML.SpoFiles where is_directory = 1 and name = @dir and parent_path_locator is null )
          insert TourML.SpoFiles( is_directory, name ) 
            select 1, @dir 
              where not exists (select * from TourML.SpoFiles where is_directory = 1 and name = @dir and parent_path_locator is null );
        
        declare @id table( stream_id uniqueidentifier );
        insert TourML.SpoFiles( name, file_stream ) 
          output inserted.stream_id into @id(stream_id)
          select cast( newid() as nvarchar(255) ), cast( @XML as varbinary(max) );

        with fo as ( select path_locator from TourML.SpoFiles where is_directory = 1 and name = @dir and parent_path_locator is null )
           , fl as ( select f.* from TourML.SpoFiles as f inner join @id as t on t.stream_id = f.stream_id where f.is_directory = 0 )
           update fl set path_locator = fl.path_locator.GetReparentedValue(hierarchyid::GetRoot(), fo.path_locator), name = @name
              from fl cross join fo;

commit transaction;
...
Рейтинг: 0 / 0
Вставить файл @name в таблицу FileTable в субдиректорию @dir (первый уровень )
    #39595133
aleks222
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Ладно, я победил

Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
      begin transaction;

        if not exists (select * from TourML.SpoFiles where is_directory = 1 and name = @dir and parent_path_locator is null )
          insert TourML.SpoFiles( is_directory, name ) 
            select 1, @dir 
              where not exists (select * from TourML.SpoFiles where is_directory = 1 and name = @dir and parent_path_locator is null );
        
        with f  as ( select * from TourML.SpoFiles )
           , fl as ( select * from f where is_directory = 0 and file_type = N'xml' and name = @name )
           , fo as ( select * from f where is_directory = 1 and name = @dir and parent_path_locator is null )
           , x  as ( select path_locator = isnull( ( select path_locator from fl where parent_path_locator = fo.path_locator )
                                                 , fo.path_locator.GetDescendant( (select max(path_locator) from f where parent_path_locator = fo.path_locator ), null)
                                                 )
                          , name = @name
                          , file_stream = cast( @XML as varbinary(max) )
                          , creation_time = @now
                       from fo
                   )
          merge f using x on f.path_locator = x.path_locator
            when not matched then insert ( path_locator, name, file_stream, creation_time ) values( path_locator, name, file_stream, creation_time )
            when matched and ( f.file_stream <> x.file_stream ) then update set file_stream = x.file_stream, creation_time = @now
       ;
       
      commit transaction;
...
Рейтинг: 0 / 0
2 сообщений из 2, страница 1 из 1
Форумы / Microsoft SQL Server [игнор отключен] [закрыт для гостей] / Вставить файл @name в таблицу FileTable в субдиректорию @dir (первый уровень )
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]