I am looking for a good way of checking whether a folder exists from within a Julia script. For now what I am doing is:
function dir_exists(dir)
exists = true
try
readdir(dir)
catch err
exists = false
end
exists
end
but I was wonder if there's a way that doesn't rely on exception handling.
The function is you are looking for is isdir.
Related
We have a function that needs to select items from a linked server, like
Alter function dbo.ttest1()
returns int
as
begin
SELECT * FROM LINKED_SERVER.Database.Schema.Table
WHERE .....
RETURN 0
end
In case of the remote server is not available, the function will throw out the error saying connection failed, but I want to cover it and let it return a default value.
Since this is compile-level error, BEGIN TRY --- END TRY is not able to cover it, what's worse, since we are in function, EXEC(#string),sp_executesql, SELECT * FROM OPENQUERY('...'), sp_testlinkedserver none of them works. the worst, I cannot afford to take risk of changing it into an SP (the grammar SELECT dbo.ttest1() is penetrated everywhere in the project).
Are there any good solutions?
VB.NET and SQL Server 2014. This piece of code produce a weird error. HasRows is TRUE but the selection is empty and, more the this, the selection shouldn't be empty because there are many records into the table that satisfy the where conditions.
If I make the same select directly in SQL Server, all works fine. The anno and nomediv parameters are corrects.
try
connection1.Open()
sqlcmd = String.Format("select distinct HomeTeam from tutto WHERE anno='{0}' AND div= '{1}' AND HomeTeam <> 'NULL'", annocampionato, nomediv(i))
SetSqlCommand(sqlcmd, 1)
sqlreader1 = sqlcommand1.ExecuteReader()
Do While sqlreader1.Read()
If sqlreader1.HasRows Then '
nomesquadra = RTrim(sqlreader1.GetString(0))
......
End If
Loop
connection1.Close()
Catch ex As Exception
MsgBox(ex.Message)
connection1.Close()
End Try
Try to explain better: the program enters into the WHILE loop and sqlreader1.hasrows is TRUE but sqlreader1.GetString(0) return index out of range exception. If I change the command to SELECT * FROM tutto, the result is the same.
Thanks in advance
Pamela
P.S. I'll avoid to use the string concat for the release version
I think your logic is backward. Granted, the documentation is thin.
If you get one line then the following happens:
Do While sqlreader1.Read()
returns true and reads the data.
If sqlreader1.HasRows Then '
returns false because there is no MORE data.
It makes no sense to check HasRows in the loop. In fact, it makes no sense to check it at all - just loop over the Read() method.
I have used the following code to copy the text from a file to a CLOB. However it is giving me a PL/SQL numeric or value error at the position where writeappend is performed.
declare
l_fhandle utl_file.file_type;
l_clob CLOB;
l_buffer VARCHAR2(4096);
BEGIN
l_fhandle := utl_file.fopen('/data',
'FILE.TXT',
'R');
dbms_lob.createtemporary(l_clob, TRUE, DBMS_LOB.CALL);
LOOP
BEGIN
utl_file.get_line(L_FHANDLE, l_buffer);
dbms_output.put_line(l_buffer);
dbms_lob.writeappend(l_clob, length(l_buffer), l_buffer);
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('Inside No data found');
INSERT INTO TAB_CLOB_FILE
(FILENAME, BODYCONT)
VALUES
('FILE', l_clob);
dbms_output.put_line('Inserted data into table');
EXIT;
END;
END LOOP;
END;
Please help me figure out what is wrong
Your problem is most likely here:
utl_file.fopen('/data', 'FILE.TXT','R');
The fist parameter is the name of an oracle directory OBJECT, not a physical path to a directory. From the Oracle docs, "Directory location of file. This string is a directory object name and is case sensitive. The default is uppercase. Read privileges must be granted on this directory object for the UTL_FILE user to run FOPEN." The incorrect call should have thrown an exception though.
You need to first create a directory object such as:
create directory MY_DIR as '/data';
Then change the fopen call to: utl_file.fopen('MY_DIR', 'FILE.TXT','R');
You can read about directory objects here.
There's a situation like: If the Salary column in updated with a value lesser than it's original value, print an error message and let the update NOT happen. This is what I've written so far:
CREATE OR REPLACE TRIGGER TRIG1
BEFORE UPDATE OF SAL ON EMP
for each row
USER_XCEP EXCEPTION
WHEN (NEW.SAL<OLD.SAL)
BEGIN
RAISE USER_XCEP
EXCEPTION
WHEN USER_XCEP THEN
DBMS_OUTPUT.PUT_LINE('UPDATION NOT ALLOWED - ILLEGAL VALUES');
END;
And I get the error - Incorrect Trigger Specification
Is there any other way to achieve this?
You're almost there; you need a DECLARE block in a trigger if you want to declare anything; this means that your WHEN clause is in the wrong place.
create or replace trigger trig1
before update
of sal
on emp
for each row
when (new.sal < old.sal)
declare
user_xcep EXCEPTION;
PRAGMA EXCEPTION_INIT( user_xcep, -20001 );
begin
raise user_xcep;
end;
SQL Fiddle
A few points:
Never catch an exception and then call DBMS_OUTPUT.PUT_LINE; it's pointless. Someone has to be there to view the result for each and every record. If you don't want something to happen raise the exception and then catch it. I've added an error code to your exception so that you can catch this outside the trigger and handle it how you wish (don't print anything to stdout).
It's a minor point but I've added a little whitespace; not much. I couldn't initially see where the problem was with your code because you didn't have any.
You were missing semi-colons after the exception declaration and RAISE.
Read more about internally defined exceptions in the documentation
Hi i have the following stored procedure...
CREATE OR REPLACE PROCEDURE DB.INSERTGOOD
(
--CapRefCursor OUT Cap_Cur_Pkg.CapCur,
p_APPLIANT_TLT IN GOODRIGHT_MANUAL.APPLICANT_TLT%TYPE,
p_APPLIANT_NME IN GOODRIGHT_MANUAL.APPLICANT_NME%TYPE,
p_APPLICANT_SURNME IN GOODRIGHT_MANUAL.APPLICANT_SURNME%TYPE,
p_COMPANY_NME IN GOODRIGHT_MANUAL.COMPANY_NME%TYPE,
p_ID_CDE IN GOODRIGHT_MANUAL.ID_CDE%TYPE,
p_ADD1 IN GOODRIGHT_MANUAL.ADD1%TYPE,
p_OCCUPATION1 IN GOODRIGHT_MANUAL.OCCUPATION1%TYPE,
p_REMARK1 IN GOODRIGHT_MANUAL.REMARK1%TYPE,
p_SOURCE IN GOODRIGHT_MANUAL.SOURCE%TYPE
)
IS
BEGIN
INSERT
INTO GOODRIGHT_MANUAL
(
SEQ_ID,
APPLICANT_TLT,
APPLICANT_NME,
APPLICANT_SURNME,
COMPANY_NME,
ID_CDE,
ADD1,
OCCUPATION1,
REMARK1,
GOODRIGHT_MANUAL.SOURCE
)
VALUES
(
goodright_seq.nextval,
p_APPLIANT_TLT,
p_APPLIANT_NME,
p_APPLICANT_SURNME,
p_COMPANY_NME,
lower(p_ID_CDE),
p_ADD1,
p_OCCUPATION1,
p_REMARK1,
p_SOURCE
);
COMMIT;
-- OPEN CapRefCursor FOR
--select 'True';
EXCEPTION
WHEN DUP_VAL_ON_INDEX
THEN ROLLBACK;
-- select 'False';
END DB.INSERTGOOD;
/
Here i want to return a string TRUE if the transaction commit successfully and FALSE if transaction rollback.
An Output Variable CapRefCursor is defined but i don't know how to assign true false to that variable and return it.
Thanks in advance.
You have defined a procedure with no OUT parameter, therefore it can not return anything.
You have several options to return success information:
define a function instead of a procedure. A function always returns something, you can define a string to be returned as VARCHAR2 for example in your case.
add an OUT parameter to the procedure. OUT parameters are logically equivalent to function returned values. You can have more than one such parameters.
modify your logic so that the procedure returns nothing when it works and throws an exception when it fails.
I would go with solution (3) because:
solution (1) and (2) are bug-prone: you may easily forget to check the return code in which case your program will continue as if no error had happened in case of failure. Ignoring error is the surest way to transform a benign bug into a monstrosity because it can lead to extensive data corruption. Your program may go on for months without you realising that it is intermittently failing!
Exception logic is designed to overcome this problem and makes the code cleaner and clearer. No more ugly if-then-else after every single procedure call. For this reason alone, solutions (1) and (2) are considered code-smell (anti-pattern) when used extensively to return success/error state.
Less code is involved, just remove the EXCEPTION block and let the error propagate.
procedures that fail will undo their work without rolling back the whole transaction if you let the exception propagate (and don't issue intermediate commits).
Finally, in general you should not control transaction logic in your sub-procedures. A procedure that does a single insert is probably part of a larger transaction. You should not let this procedure either commit or rollback. Your calling code, be it PL/SQL, GUI or script should decide if the transaction should move forward and complete or be rolled back.