CREATE PROCEDURE in NetBeans IDE - sql-server

I just wanted to add a simple procedure in Account table in Bank_Admin Schema.
The procedure adds some amount of money to the existing balance an account holder has. I guess, by reading the Sql command, you will figure it out.
But I am getting this error:
[Exception, Error code 30,000, SQLState 42X01] Syntax error:
Encountered "BEGIN" at line 2, column 1.
Line 2, column 1
Execution finished after 0.036 s, 1 error occurred
.Please help me with this code...
CREATE PROCEDURE deposit_in_bank (IN bank_account int, IN deposit_amount int)
BEGIN
UPDATE BANK_ADMIN.ACCOUNT SET balance = balance + deposit_amount WHERE account_no = bank_account
END

I'm assuming that defining a SQL stored procedure in NetBeans is the same as doing so directly as a SQL statement. If so...
I don't think your IN keywords are valid, all parameters are presumed to be input parameters unless specified as an output parameter.
As scsimon points out, you also need AS before your BEGIN.
I don't think you actually need your BEGIN and END at all either. Or the brackets around your parameters.
And I don't think you're writing your parameter variables correctly (they should be prefixed with #, so the following should work:
CREATE PROCEDURE deposit_in_bank
#bank_account INT,
#deposit_amount INT
AS
UPDATE BANK_ADMIN.ACCOUNT
SET balance = balance + #deposit_amount
WHERE account_no = #bank_account

Related

Trying to read returned value from stored procedure always shows 0

In SQL Server, I have an existing Document_Add stored procedure that works and returns a good DocID value (in Visual Studio vb code) and cannot change. Calling it like this in SQL:
EXEC #DocID = PADS2.dbo.Document_Add #SystemCode...
This runs the stored procedure, but #DocID is always 0 (whether declared as INT or varchar).
Expecting #DocID to be 2594631 or similar.
Any ideas?
I always say RTFM - read the fine manual provided by microsoft.
https://learn.microsoft.com/en-us/sql/t-sql/statements/create-procedure-transact-sql?view=sql-server-ver16
1 - Example D shows how to use input parameters.
2 - Example F show how to use output parameters.
Just remember, input and output parameters are scalar, not tables.
If you do need to input of a table look at Example G.
3 - Example G - how to pass a table value parameter
4 - Example B - return multiple result sets.
If you execute two SELECT statements in the procedure, you will have MARS (multiple active result sets).
By default, a procedure returns a value of zero. It is typical used to indicate if the procedure was a success. You can code a non zero value to indicate an error.
Always read the docs!
<If this is not the place to follow up, please advise.>
Credit to rjs123431:
SQL SP that ends like this:
...
SELECT #Rslt = CONVERT(VARCHAR(2000),#NewId)
select #Rslt;
END
Returns a string '2680914' in vb:
sDocid = DA.ExecScalarString(EXEC PADS2.DBO.[Document_Add] 'C', ...)
But returns 0 when called from SQL like this:
EXEC #DocID = PADS2.dbo.Document_Add #SystemCode...
This returns correct DocID (2680914) when using a Temp Table like this:
INSERT INTO #TempTable EXEC PADS2.dbo.Document_Add #SystemCode...
set #sDocID = (SELECT DocID FROM #TempTable)

select ... into variable from table where 1=0 leads to the replacement of the variable with null

We are migrating a lot of code from SQL Server to Postgresql. We met the following problem, a serious difference between SQL Server and Postgresql.
Of course, below, by the expression 1=0, I meant cases when the query conditions do not return a single record.
A query in SQL Server:
select #variable = t.field
from table t
where 1 = 0
saves the previous value of the variable.
A query in Postgresql:
select t.field
into variable
from table t
where 1 = 0
replaces the previous value of the variable with null.
We have already rewritten a lot of code without taking this feature into account.
Is there an easy way in postgresql, without rewriting the code, to save the value of a variable in such cases? For example, maybe there is some kind of server's or database's or session's settings? We did not find any relevant information in the documentation. We do not understand such a pattern of behavior in postgresql, which requires the introduction of additional variables and lines of code to check the result of the every query.
As far as I know there is no way to change postgresql's behavior here.
I don't have access to the SQL/PSM specifications, so I couldn't tell you which one matches the standard (if any / if SELECT INTO <variable> even is in it).
You don't need to use additional variables though, you can use INTO STRICT and catch the exception when no rows were returned:
DO $$
DECLARE
variable int = 1;
BEGIN
BEGIN
SELECT 1
INTO STRICT variable
WHERE FALSE;
EXCEPTION
WHEN NO_DATA_FOUND THEN
END;
RAISE NOTICE 'kept the previous value: %', variable;
END
$$
shows "kept the previous value: 1".
Though it is obviously more verbose than the SQL Server version.

Calculating age with currval in postgreSQL

I have created a database with a People table which contains all the information for people such as their first name, last name, gender,date of birth, and email. Each row in this table has a serial peopleID. I am trying to create a stored procedure and trigger to automatically calculate the age of a person that was just entered. I am trying to use currval to get the last inserted peopleID.
This is my stored procedure:
CREATE OR REPLACE FUNCTION PersonAge (peopleID INT)
RETURNS INTERVAL AS
$$
DECLARE
birthday date := (SELECT People.dateOfBirth
FROM People
);
BEGIN
RETURN age(birthday);
END;
$$
LANGUAGE plpgsql
This is my trigger:
CREATE TRIGGER CalculateAge
AFTER INSERT OR UPDATE ON People
FOR EACH ROW
EXECUTE PROCEDURE PersonAge( SELECT currval(‘people_id_seq’));
This is the error I am getting when I try to implement it with an insert statement:
ERROR: syntax error at or near "currval"
LINE 5: EXECUTE PROCEDURE PersonAge( SELECT currval('people_id_seq')...
^
********** Error **********
ERROR: syntax error at or near "currval"
SQL state: 42601
Character: 116
You pass arguments in not supported way, please read docs
Note that the function must be declared with no arguments even if it
expects to receive some arguments specified in CREATE TRIGGER — such
arguments are passed via TG_ARGV, as described below.
and
TG_ARGV[]
Data type array of text; the arguments from the CREATE TRIGGER
statement. The index counts from 0. Invalid indexes (less than 0 or
greater than or equal to tg_nargs) result in a null value.
(emphasis mine)

PL/SQL: SQL Statement ignored?

Hi everyone getting this error message when trying to create a trigger and its got me a little stumped.
Here is my trigger code.
CREATE OR REPLACE TRIGGER CUSTOMER_AD
AFTER DELETE ON CUSTOMER
REFERENCING OLD AS OLD
FOR EACH ROW
DECLARE
nPlaced_order_count NUMBER;
BEGIN
SELECT COUNT(*)
INTO nPlaced_order_count
FROM PLACED_ORDERS p
WHERE p.FK1_CUSTOMER_ID = OLD.CUSTOMER_ID;
IF nPlaced_order_count > 0 THEN
INSERT into previous_customer
(customer_id,
first_name,
last_name,
address,
AUDIT_USER,
AUDIT_DATE)
VALUES
(:old.customer_id,
:old.first_name,
:old.last_name,
:old.address,
UPPER(v('APP_USER')),
SYSDATE);
END IF;
END CUSTOMER_AD;
And the error I'm getting 'Error at line 4: PL/SQL: SQL Statement ignored 0.10 seconds'
Anyone any guesses why?
thanks for the help
The error shown is only the highest level. Depending where you're running it you should be able to see the stack trace. The client will determine exactly how to do that; SQL*Plus or SQL Developer would show you more than this anyway, but I don't really know about other clients. If you can't see the details in your client then you can query for them with:
select * from user_errors where name = 'CUSTOMER_AD' and type = 'TRIGGER'
Assuming the tables all exist, it's probably this line:
WHERE p.FK1_CUSTOMER_ID = OLD.CUSTOMER_ID;
which should be:
WHERE p.FK1_CUSTOMER_ID = :OLD.CUSTOMER_ID;
When referencing the old (or new) value from the table, the name as specified in the referencing clause has be preceded by a colon, so :OLD in this case. As you're doing already in the insert ... values() clause.
(From comments, my assumption turned out to be wrong - as well as the missing colon problem, the table name is really placed_order, without an s).
Seems like you copied code from both answers to your previous question without really understanding what they were doing. You might want to look at the trigger design guidelines (particularly the one about not dupicating database functionality) and the syntax for create trigger which introduces the referencing clause.

SSIS SQL Task: strange value for stored procedure output parameter with default value

Here's a simplified version of my sproc:
CREATE PROCEDURE usp_AddSomething
#i_input1 int,
#o_output1 int = 1 OUTPUT,
#o_output2 int = 2 OUTPUT
AS
-- Do stuff
If I don't explicitly set values for #o_output1 or #o_output2 within the sproc then I expect to see the default output values of 1 and 2. It works this way when I call EXEC usp_AddSomething ... from a normal SQL script. However, when I run the same statement in an SSIS SQL Task like so:
EXEC ? = usp_AddSomething
#i_input1 = ?,
#o_output1 = ? OUTPUT,
#o_output2 = ? OUTPUT
I get seemingly random values (e.g. 15305391 and 69085360) instead of the default 1 and 2.
But if the values are explicitly set somewhere in the sproc like so:
...
SELECT #o_output1 = 1
SELECT #o_output2 = 2
RETURN 0
Then I get the expected values of 1 and 2 in SSIS.
My SSIS vars are Int32, the Parameter Mapping on the SQL task uses LONG and the ordinal positions (i.e. 0, 1, 2) are setup correctly under Parameter Name.
It's easy enough to redundantly initialize the values of the output parameters to match their defaults, but I'd really like to know WHY it's not working. Maybe another quirky SSIS thing? SQL Server 2005 SP4.
A good way to see what's going on is to start a trace with profiler and see the t-sql that ssis is sending to the database engine. Then try executing that from ssms and see if you get the same results.

Resources