What's wrong with my EXECUTE command? - sql

I'm trying to make a PROCEDURE that makes it easier to store changes users make in their settings (like a server log, you know right) into a table user_settings_changelog. I finally made the PROCEDURE acceptable so my sql program (HeidiSQL) would store it. Now the problem is this: how to properly EXECUTE it. I tried this multiple times with multiple lines of code, but it seemed nothing worked. Can anyone help me out here?
The PROCEDURE query:
DELIMITER $$
CREATE PROCEDURE setting_adjustment_log (name_setting VARCHAR(45),
changed_from VARCHAR(45), changed_to VARCHAR(45), username
VARCHAR(45))
BEGIN
INSERT INTO user_settings_changelog
VALUES (GETDATE(), name_setting, changed_from, changed_to,
username);
END$$
The table user_settings_changelog has 5 columns: date DATETIME, name_setting VARCHAR(45), changed_from VARCHAR(45), changed_to VARCHAR(45) and username VARCHAR(45).
The EXECUTE query:
EXECUTE setting_adjustment_log ('background','black','white','TheCoderNoob');
The error HeidiSQL gives me:
SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('background','black','white','TheCoderNoob')' at line 1
Maybe useful to mention: I am using a version of USBwebserver from a few years ago.
EDIT: I've been looking at the EXECUTE/CALL query in MySQL Workbench for a while, it seems like the database expects something between the name of the procedure and the given data or something. When I hover over it, it reads:
Syntax error: 'background' (single quoted text) is not a valid input at this position

First, you should always include the column names in an insert statement. (There might be a few exceptions, but if you are learning SQL, then sticks with best practices.)
Second, distinguish the input parameters from possible column names.
Third, your code looks like MySQL, so use NOW() or a similar function:
DELIMITER $$
CREATE PROCEDURE setting_adjustment_log (
in_name_setting VARCHAR(45),
in_changed_from VARCHAR(45),
in_changed_to VARCHAR(45),
in_username VARCHAR(45)
)
BEGIN
INSERT INTO user_settings_changelog (date, name_setting, changed_from, changed_to, username)
VALUES (NOW(), in_name_setting, in_changed_from, in_changed_to,
in_username);
END$$
When you call the stored procedure, use call:
CALL setting_adjustment_log('background', 'black', 'white', 'TheCoderNoob');

Related

SQL Server stored procedure: how to return column names/values of type failures in variable?

Ambiguous thread name, I apologize. I am not new to SQL, but I'm new to coding longer stored procedures so I don't deal with variables much outside of passing through maybe a table name or returning row count, etc.
I have a stored procedure that is executing an insert from a staging table to a fact table. There are a couple type casts in the insert.
If the insert fails due to a typecast. Is there any way to return the name of the column that failed, along with what the failed value was? How would I code that? I know that Try_parse would make it so the stored procedure doesn't fail on type cast failure, but I want to be able to pass back exactly what column and value failed.
I show an example here:
Create Procedure dbo.Example_Insert
#updateUser varchar(255)
As
Begin
Insert Into dbo.Energy_Costs (Energy_Cost_Id, Project_Id, Propane_Cost_Dollars,
Electricity_Cost_Dollars, Fuel_Savings_Evaluator)
Select
Next Value For energy_cost_id,
r.project_id,
Cast(r.propane_cost_dollars As Decimal(18,2)),
Cast(r.electricity_cost_dollars As Decimal(18,2)),
#update_user fuel_savings_evaluator
From
staging_table r
return ##ROWCOUNT
end
You can use CURSOR in sql then insert one line at a time. When insert fail return value currently row error.
I hope my idea suitable with you.

DB2: How to get generated always as statement to work with session user

I need to get a userstamp into a table and have not managed to figure out how the GENERATED FOR EACH ROW ON UPDATE AS statement works with the SESSION_USER variable in DB2 10.5 (LUW).
Managed to get an implementation working using a function which has a fake variable for forcing the evaluation in update statements:
CREATE OR REPLACE FUNCTION XXX.CURRENT_USER( tmp varchar(128))
SPECIFIC xxx.XXX_CURRENT_USER
RETURNS VARCHAR(128)
CONTAINS SQL DETERMINISTIC NO EXTERNAL ACTION
BEGIN
RETURN session_user ;
END
GO
CREATE TABLE xxx (
i INTEGER,
t VARCHAR(128) GENERATED ALWAYS AS (XXX.CURRENT_USER(i))
)
However, would be nice have less "hacky" implementation for a basic thing like this.
For the time stamps there is that "FOR EACH ROW ON UPDATE AS ROW CHANGE TIMESTAMP" statement but no equivalent for other register variables it seems.
Help is very much appreciated
Does this work?
CREATE TABLE xxx (
i INTEGER,
t VARCHAR(128) WITH DEFAULT session_user
);
I don't have DB2 on hand to check, but this is very similar to the syntax used in other databases (although the more typical syntax does not use WITH).

Msg 102, Level 15, State 1

I was just trying to teach myself how to work with databases in SQL Server, watching my first tutorial. The guy got it all right, but I get this error:
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'rollno'.
The code:
create database mytestdb
create table mytesttable
(
rollno int,
firstname varchar(50),
lastname varchar(50)
)
Being a beginner, I don't know what to do.
Try this...
create database mytestdb;
GO
USE mytestdb;
GO
create table mytesttable
(
rollno int,
firstname varchar(50),
lastname varchar(50)
);
GO
When you execute all the statements in one go, without the key word (batch sperator GO) sql server tried to execute all the statements as one piece of work. But there are certainly rules in sql server which tell you what statements can or can't be executed as one piece of work(in one batch).
Hence use the key word GO to tell sql server you want these pieces of work executed separately, mind you GO is not T-SQL it is SQL Server Management Studio (SSMS) term.
I noticed, it happens due to Unicode File's Byte Order Mark (BOM). It can preset as a non-visible character, and invalidates the SQL syntax.
If I copy/paste the SQL, it works. If I load the file, it won't.
Here is how to fix. (an example only).
You can remove those marks by opening the file in notepad and saving as an ANSI Encoding while saving the file.
See the first few characters in a plain text file.
Try this
create database mytestdb;
GO
USE mytestdb
create table mytesttable
(
rollno int,
firstname varchar(50),
lastname varchar(50)
);

Convert Oracle Trigger to Sql Server trigger

I am new to Sql Server and having difficulty to convert the oracle triggers to Sql server.
Can some one help me with that.Here is one of the example:
create or replace
TRIGGER "NEW".TRG_dummy
BEFORE INSERT ON TBL_dummy
FOR EACH ROW
BEGIN
SELECT SEQ_dummy.NEXTVAL
INTO :NEW.dummy_ID
FROM DUAL;
END;
Any help in this will be appreciated.It would be great if I get a method to convert as I have a lot of other objects to be migrated.
Replacing of the trigger doesn't make sense here since you don't even need a trigger for the same functionality in SQL Server. You only need to declare the column as an IDENTITY, e.g.
CREATE TABLE TBL_AFR (
AFR_ID INT IDENTITY NOT NULL, --<< this automatically does the same thing
.. the other columns
);

Informix SQL - What is wrong with this simple stored procedure &| trigger syntax?

IBM Informix Dynamic Server Version 11.50.FC6
I am trying to execute a simple stored procedure from within an update trigger. Together, they are used to update a field with the current timestamp when another field in the same row is updated.
Table sp_test:
id (serial int, unique, not null, primary key)
stat (char(1), not null, default="A")
add_date (date, not null, default today)
upd_date (date, null)
The stored procedure code is:
create procedure upd_row_date_proc (cid int)
update sproc_trig_rec set upd_date = current where id = cid;
end procedure;
This executes fine and creates the routine, but the trigger I am trying to implement on updates is not working.
The trigger code is:
create trigger upd_row_date_trig
update of stat on sproc_trig_rec
after (execute procedure upd_row_date_proc(id));
I've tried a bunch of syntax variations, but cannot get it to work.
I usually get my error on the ( char of the 3rd line. Here's the error code:
201: A syntax error has occurred.
Error in line 3
Near character position 0
Does anyone know what I'm doing wrong in the syntax of the trigger? Could this type of updating be defined in the creation of the table, or do I need to accomplish it by doing it the way described above?
Thanks for any help
This finally worked for me
create trigger ken_trig
update of stat on sproc_trig_rec
referencing old as ken_pre_upd
for each row (execute procedure ken_proc(ken_pre_upd.id));