The INSERT query run on SQL view but not in visual basic - sql

I am trying to insert into a Bill table with 3 columns: BillID, FoodID and Count. No primary key because BillID and FoodID can be repeated.
I run this query:
insert into BillInfo (BillID, FoodID, Count)
values (3, 'SP05', 1)
It works fine in Microsoft Access (query view), but not in my Visual Basic project.
I tried different table, and it can insert okay, but not this table. I think something is wrong with violation key, but the exception yield
Syntax error in insert into statement
And I don't know how to check it. Please help me.
Dim query = "insert into BillInfo(BillID, FoodID, Count) values(3, 'SP05', 1)"
Dim result = DataProvider.Instance.executeNonQuery(query)
My DataProvider class basically can execute any query with any parameter.
I just tried hard code as an example

Count is a reserved word in any database engine that I can think about.
You should try to avoid such words in your database schema.
Anyway, if you want to use that word, then include it in square brackets
"Insert into BillInfo(BillID, FoodID, [Count]) values(3, 'SP05', 1)"

Related

ORA-01722! Why can't I INSERT a NUMBER into a NUMBER data field without getting this error?

Can someone tell me what is going on here?
So I have a simple table location and it only has two columns; one is a number and the other varchar2.
I'm simply trying to insert some data into the locations table so I can get cracking with the other larger datasets but keep getting this damn error every time.
Error starting at line : 7 in command -
INSERT INTO location
VALUES (1, 'Head Office')
Error report -
ORA-01722: invalid number
NOTE: Before down-voting, YES - I have seen others posting about this but is usually for something less obvious than my situation where they are trying to enter a string into a number field or a number into a string field!
In my case however, the data in the INSERT statement is a number AND the data type is also NUMBER!
DATA STRUCTURE:
CREATE TABLE location(
locID NUMBER(4) NOT NULL,
locName VARCHAR2(100) NOT NULL
);
INSERT STATEMENT:
INSERT INTO location
VALUES (1, 'Head Office');
The error code can be seen above there where I first mentioned it.
Thanks in advance.
P.S. It may be worth mentioning that the ID field in 'location' table is being used as a FOREIGN KEY in a separate table 'employees'. I have however, checked that the data types matched!
EDIT #1: I'm using ORACLE SQL Developer
Always include the columns when doing an insert:
INSERT INTO location (locId, locname)
VALUES (1, 'Head Office');
From your description of the problem, this should not actually fix it. This is just a good habit.
The above is correct SQL for your table. If the error continues to happen it is probably coming from a trigger on the table.
Think like its stupid, you are getting number error from "head office" not from 1. Actually you are trying to insert string into number.
If you dont want to write column names to insert you should totally define all values in insert in place as located in table. I assume your table structure is
locId|locNumber
So your insert should be like below
insert into table values (1,'head office')
I hope you understand shortcut logic

PostgreSQL: INSERT INTO syntax error

I'm following an older tutorial learning Postgres, so it's possible maybe something has changed since it was published. In the tutorial (using psql) I create a table then do some insert statements. Here is the tutorial and corresponding psql commands that cause error:
http://www.postgresqlforbeginners.com/2010/11/create-table-and-constraints.html
create table people(
id int PRIMARY KEY,
name varchar NOT NULL
);
insert into people(0,'Steve Jobs');
insert into people(1,'Mike Markkula');
insert into people(2,'Mike Scott');
insert into people(3,'John Sculley');
insert into people(4,'Michael Spindler');
insert into people(5,'Gil Amelio');
insert into people(6,'Mike Scott');
I get this error for each insert statement:
ERROR: syntax error at or near "0"
LINE 1: insert into people(0,'Steve Jobs');
^
I've tried copy pasting, capitalizing the sql commands (ie INSERT), running the command from shell outside of psql, adding spaces, using " instead of ' quotes... All result in the same errors. Has something changed or am I possibly doing something wrong?
The problem is the missing values (as noted in a comment).
I want to make some suggestions. First, whenever you use insert, you should always list the columns. This is especially important if you are learning the language -- you should be learning good habits.
Second, you don't need multiple inserts. A shorter way to insert multiple rows is:
insert into people (id, name)
values (0,'Steve Jobs'),
(1,'Mike Markkula'),
(2,'Mike Scott'),
(3,'John Sculley'),
(4,'Michael Spindler'),
(5,'Gil Amelio'),
(6,'Mike Scott');
And you should learn about serial. A more common way to write this code would be:
create table people (
id serial PRIMARY KEY,
name varchar NOT NULL
);
insert into people (name)
values ('Steve Jobs'),
('Mike Markkula'),
('Mike Scott'),
('John Sculley'),
('Michael Spindler'),
('Gil Amelio'),
('Mike Scott');
The id is assigned automatically by the database (starting at 1 rather than 0).
I should add: I am personally uncomfortable with having varchar without a length. This is perfectly fine in Postgres, but some databases would interpret it as varchar(1).

Syntax error in Insert Statement but Insert Statement is fine

I'm receiving a "Syntax error in INSERT INTO statement" when trying to submit this query.
INSERT INTO
Modified (ModifiedID, Username, Table, RecordID)
VALUES
('12','null','Accident','1')
All datatypes match in the database. If I run the query without inserting into "Table", it works. I don't know why, when I add the table string value to the query I receive the error.
Try this
INSERT INTO Modified (ModifiedID, Username, [Table], RecordID)
VALUES ('12','null','Accident','1')
In most SQL dialects, TABLE is a RESERVED WORD. The [] is Microsoft's delimiter and I believe backticks ` are used in MySQL
Also, do you really want 'null' as opposed to NULL, they have different meanings
In HSQLDB:
INSERT INTO Modified (ModifiedID, Username, "Table", RecordID)
VALUES (12, NULL, 'Accident', 1);
I am just going to assume that "key-ish" sounding fields, eg. ModifiedID and RecordID, are supposed to be INTEGER or BIGINT rather than, say, VARCHAR. DDL would be nice.
In MySQL:
INSERT INTO Modified (ModifiedID, Username, `Table`, RecordID)
VALUES ('12', NULL, 'Accident', '1')

PowerBuilder: Check record and insert into SQL table if not available

I am fairly new to PowerBuilder Classic 12. I need to check whether a record is available and if not insert from a textbox. I would probably need a DataStore since someone suggested a preference to SQL statements. Thanks.
this code is behaving funny, please where is the problem? at one time it works but running the program again it accepts a data that has already been inserted. the program is not giving any error but i can see the same data stored in the table.
string id, idno
idno=trim(sle_idno.text)
if idno="" then
messagebox("EMPTY","Enter a record")
return
end if
SELECT employee.idnumber
INTO :id
FROM employee ;
if idno=id then
messagebox("AVAILABLE","Record available")
return
end if
INSERT INTO employee
( idnumber )
VALUES ( :idno ) ;
Fixing existing code
You're missing a WHERE clause; this is trying to stuff all idnumber values into id. That's why your existing code is failing.
Have a look at the help file that comes with PB. (The PDF manuals that come with it are good too, but the help file is right there on the menu of the IDE.) There are plenty of examples in there on how you should code embedded SQL statements. You should be checking the error attributes of the transaction object (in this case it is implicitly SQLCA) to determine if the transaction executed successfully or not, returned any rows, etc....
A new approach
There are several ways to approach this with a DataStore. The easiest is to make a DataWindow object against the employee table, that takes an argument and uses it in the SQL statement in the WHERE clause. In your script, set up a DataStore using this object and Retrieve() it using the idnumber as the argument, check the return value of Retrieve() (the number of rows returned if positive or zero, an error if negative) and if it's zero, InsertRow() on your DataStore, do your SetItem()'s on it (you'll want to load more than just idnumber, right?) and then do an Update() on it.
You are looking for Merge.
I will explain it using an example. Just send the values in Database via Stored Proc? and use following technique.
Sample DDL
CREATE TABLE Employee
(
EmployeeID INTEGER PRIMARY KEY,
EmployeeName VARCHAR(15)
)
CREATE TABLE EmployeeSalary
(
EmployeeID INTEGER ,
EmployeeSalary INTEGER
)
Sample DML For Employee
INSERT INTO Employee
VALUES(1,'SMITH')
INSERT INTO Employee
VALUES(2,'ALLEN')
INSERT INTO Employee
VALUES(3,'JONES')
INSERT INTO Employee
VALUES(4,'MARTIN')
INSERT INTO Employee
VALUES(5,'JAMES')
Sample DML For EmployeeDetails
INSERT INTO EmployeeSalary
VALUES(1,23000)
INSERT INTO EmployeeSalary
VALUES(2,25500)
INSERT INTO EmployeeSalary
VALUES(3,20000)
Merge Query
MERGE EmployeeSalary AS stm
USING (SELECT EmployeeID,EmployeeName FROM Employee) AS sd
ON stm.EmployeeID = sd.EmployeeID
WHEN MATCHED THEN UPDATE SET stm.EmployeeSalary = stm.EmployeeSalary + 12
WHEN NOT MATCHED THEN
INSERT(EmployeeID,EmployeeSalary)
VALUES(sd.EmployeeID,25000);
References
First Reference
Second Reference
Third Reference
Fourth Reference

Use INSERT-OUTPUT to provide values for another INSERT

Good day,
I was wondering if it is possible to use an INSERT-OUTPUT statement in such a way as to provide the value(s) for another, outer, INSERT statement. That way values can be added to an entity table and an intersection table in a single statement - I hope I'm wording this effectively. For example:
INSERT INTO [#tblIntersect] ([Entity1ID], [Entity2ID])
VALUES
(
INSERT INTO [#tblEntity1] ([Value])
OUTPUT [inserted].[ID] AS [entity1ID], #entity2ID AS [entity2ID]
VALUES ('One')
)
So the inner INSERT-OUTPUT statement will add a new entity to table #tblEntity1. The new entity's ID (which is set as IDENTITY(1, 1) will then be returned through the OUTPUT statement, along with a static value (which I already have in my code), to provide the two values for the outer INSERT statement.
The reason I think it might be possible is because execution of the inner INSERT-OUTPUT statement on its own returns a table anyway, and such output can usually be used to provide values for INSERT statements.
Obviously this example doesn't work; I was hoping it's just a simple syntax problem.
Thank you in advance for any comments and advice.
Your requirement is possible according to the documentation.
Assuming #tblIntersect has two matching id columns this should work
INSERT INTO [#tblEntity1] ([Value])
OUTPUT [inserted].[ID] AS [entity1ID], #entity2ID AS [entity2ID]
INTO #tblIntersect
VALUES ('One')