I have two folders that the database writes files to using the directory access features. The destination folder is a landing point for documents requested by a webpage. Once they are viewed by the webpage they are no longer useful and can be cleaned up at some point.

My initial plan was to have an event that ran each night to delete files from this folder that had a create date older than 30 minutes prior.

This query displays the records I would want to delete correctly:

SELECT *

FROM ClientDataLand

WHERE create_date_time

<= DATEADD(MINUTE,-30,NOW())

The problem is that generates Error code -728 -- Update operation attempted on non-updatable remote query.

So how could one delete from a directory access server based on conditions, such as the create time?

asked 11 Sep '12, 13:21

Siger%20Matt's gravatar image

Siger Matt
3.1k486493
accept rate: 13%


Directory Access Servers are pretty picky about the type of cursors allowed on them. You can get around the issue by using a cursor in a stored procedure that only calls fetch next :

create procedure DelFiles ( in @expire integer )
begin
declare @fn varchar(128);
declare @cur cursor for select file_name from DBA.MyDir 
                        where create_date_time < dateadd( minute, -1 * @expire, now() );
open @cur;
fetch next @cur into @fn;
while sqlcode = 0 loop
  delete from DBA.MyDir where file_name = @fn;
  fetch next @cur into @fn;
end loop;
close @cur;
end

In this example, I've allowed you to pass in a parameter specifying how many minutes old a file should be to be deleted.

permanent link

answered 11 Sep '12, 15:05

Reg%20Domaratzki's gravatar image

Reg Domaratzki
5.0k33572
accept rate: 41%

edited 12 Sep '12, 09:44

Works like a charm. Thanks Reg.

(11 Sep '12, 21:33) Siger Matt

I don't understand why do I need to use execute immediate for the delete ? Is this necessary for the cursor ?

(12 Sep '12, 06:15) Thomas Dueme...
Replies hidden

I second that question.)
Possibly even a positioned delete would do? - As to Nick's explanation, I would think it's just that the DELETE on a directory access table needs a single filename as parameter, not any other condition, so the cursor shouldn't be a problem here... but I don't know myself.

(12 Sep '12, 07:07) Volker Barth

I only had execute immediate here because I'd copied the SQL from another sample. The following SQL works as well, so I've edited my answer, and I don't see why a positioned delete wouldn't work either, although I haven't tested it myself.

delete from DBA.MyDir where file_name = @fn;
(12 Sep '12, 09:43) Reg Domaratzki

Just to add: Nick Elson has answered a similar question in the general NG (thread "proxy table usable columns for delete" from 2012-08-09) - explaining the cause:

Q: one issue / question i am having . . .is it possible to issue deletes against the files querying by create_date_time, the only column that appears to be usable in the delete where clause is the file_name column. this is sql anywhere 12.01.

A: I am able to reproduce this and I believe this is a 'by design' limitation. So the bare SQL Delete Statement will not work. It seems to come down to the implementation not being set driven. The server is effectively doing this in Full Passthrough mode and the OS function to do a delete does not take such search criteria. That only accepts the filename.

One could write procedural logic to select for that criteria and fetch the file name via a cursor and use that to issue the correct delete operation. (see: execute immediate for that last part http://dcx.sybase.com/index.html#1201/en/dbreference/execute-immediate-statement.html )

Obviously, the suggested solution is exactly what Reg has implemented:)

permanent link

answered 12 Sep '12, 06:02

Volker%20Barth's gravatar image

Volker Barth
29.6k293444650
accept rate: 32%

edited 12 Sep '12, 06:04

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×10
×6
×1

question asked: 11 Sep '12, 13:21

question was seen: 967 times

last updated: 12 May '14, 05:29