Incorrect syntax for inserting data into SQL Server - sql

I am attempting to use the following insert command in SQL Sever Management Studio 2012:
INSERT INTO 'F' ('date', 'open', 'high', 'low', 'close', 'volume', 'amount_change','percent_change')
VALUES ('2012-12-19', 11.79, 11.85, 11.62, 11.73, 54884700, -0.06, -0.508906)
When I attempt this the error I get is
parameters supplied for object F which is not a function
I can confirm that there is a table F. I have tried various syntax using dbo.f, "F", 'F', [dbo].[F], all to no avail.

You need to remove the single quotes from the table name and the column names. If you want them escaped use brackets [].
INSERT INTO [F] ([date], [open], [high], [low], [close], [volume],
[amount_change], [percent_change])
VALUES ('2012-12-19', 11.79, 11.85, 11.62, 11.73, 54884700, -0.06, -0.508906)

Just a tidbit for your information, there is session setting that allows double quotes (ISO rules) for table and column names.
-- Set option on
SET QUOTED_IDENTIFIER ON
GO
-- Insert data
INSERT INTO "F" ("date", "open", "high", "low", "close", "volume", "amount_change","percent_change")
VALUES ('2012-12-19', 11.79, 11.85, 11.62, 11.73, 54884700, -0.06, -0.508906)
Check out this technet article for more details.
http://technet.microsoft.com/en-us/library/ms174393.aspx
TSQL is a big product with many little idiosyncrasies.
Sincerely
John
www.craftydba.com

You can use [F] for the table and make sure you are in the same database. It appears to work fine when I test it.
INSERT INTO [F] ('date', 'open', 'high', 'low', 'close', 'volume', 'amount_change','percent_change')
VALUES ('2012-12-19', 11.79, 11.85, 11.62, 11.73, 54884700, -0.06, -0.508906)

Related

SQL wont Complete INSERT INTO (incorrect Date)

Im attempting to insert data into a table using the INSERT INTO shown below:
INSERT INTO `paitent`(`PaitentID`, `fName`, `sName`, `DOB`, `Sex`, `NextOfKinFName`, `NextOfKinSName`, `NextOfKinRelationship`, `NextOfKinTelNo`, `DateOfAdmission`, `AdmissionNotes`, `PrescribedMedication`, `Doseage`, `StartMedDate`, `EndMedDate`, `MedFrequency`) VALUES (0001, "Peter", "Gregory", 1997-01-01, "Male", "Mary", "Gregory", "Wife", 09875463762, 2020-01-02, "Broken Leg", "N/A", "N/A", 2019-02-02, 2019-03-03, "N/A")
but when I run the SQL i get this error:
#1292 - Incorrect date value: '1995' for column 'DOB' at row 1
I cant understand why as there isn't even a 1995 date in the data that's being inputted.
Does anyone understand why I'm getting this error?
This is a bit long for a comment.
Date constants need to be enclosed in single quotes, so '2020-01-02'. However, in Standard SQL (your question doesn't have a tag), it is even better to identify it as a date constant by using the prefix date: date '2020-01-02' is the standard syntax for a date literal.

Error at or near square brackets - Postgres

I'm trying to run a PostgreSQL query which is:
insert into client (email, name) values ('johndoe#email.com', 'johnDoe');
insert into client_settings (client_id, data) values (currval('client_id_seq'), 0);
insert into client_verify (client_id, dataFields) values (currval('client_id_seq'), json_build_object('data1', ['a1', 'a2'], 'data2', ['b1', 'b2']) );
But I'm getting an error stating SQL Error [42601]: syntax error at or near "[".
The last json object(i.e., the dataFields) when inserted into the DB it should look like:
{"data1": ["a1", "a2"], "data2": ["b1", "b2"]}
Not sure what I am doing wrong. Is there something that I'm missing or a different way to do that?
After good research I found documentation to put 'Array' in front of those like:
json_build_object('data1', Array['a1', 'a2'], 'data2', Array['b1', 'b2'])
Is this what you need :
insert into client_verify (client_id, dataFields)
values (currval('client_id_seq'), json_build_object('data1', 'a1, a2', 'data2', 'b1, b2') );
Please try this:
insert into client_verify (client_id, dataFields)
values (currval('client_id_seq')
, json_build_object('data1', '["a1", "a2"]', 'data2', '["b1", "b2"]'));
Here you can check how you need to add your string:
https://www.freeformatter.com/json-escape.html#ad-output
The UNESCAPE is the option you need
Here is a demo :
DEMO

SQL: Consolidating results

I am a relative beginner in SQL (Learned and forgotten many times) and entirely self taught so please excuse my likely lack of proper terminology. I made a query that pulls items that have been returned, each items' row has a return code. Here is a result sample:
In the final report(Created with Visual Studio), I would like to be able to have a count of returns by return type but I would need to consolidate the 40 or so return codes into 4 or 5 return type groups. So RET_CODE values ) and . are both product quality issues and would count in the "Product Quality" row.
I could use some help with what the best way to accomplish this would be.
Thank You
Andrew
The bad way!
You could do this by creating the grouping within your SQL statement with something like
SELECT
*,
CASE
WHEN RET_CODE IN ('.', ')') THEN 'Quality Error'
WHEN RET_CODE IN ('X', 'Y', 'Z') THEN 'Some Other error'
ELSE [Desc1]
END AS GroupDescription
FROM myTable
The problem with this approach is that you have to keep repeating it every time you want to something similar.
The better option. (but not perfect!)
Assuming you do not already have such a table...
Create a table that contains the grouping. You can use this in the future whenever you need to do this kind of thing.
For example.
CREATE TABLE dbo.MyErrorGroupTable (RET_CODE varchar(10), GroupDescription varchar(50))
INSERT INTO dbo.MyErrorGroupTable VALUES
('.', 'Quality Error'),
(')', 'Quality Error'),
('X', 'Some Other Error'),
('Y', 'Some Other Error'),
('.', 'Some Other Error'),
('P', 'UPS Error'),
('A', 'PAck/Pick Error')
etc....
Then you can simply join to this table and use the GroupDescription in your report
e.g.
SELECT
a.*, b.GroupDescription
FROM myTable a
JOIN MyErrorGroupTable b ON a.RET_CODE = b.RET_CODE
Hope this helps...
You're looking for the GROUP BY clause.

Unexpected Beginning of statement - Issue with Syntax?

Currently using PHPMyAdmin, and I am trying to insert some data into a SQL Table.
The error that I am getting is Unexpected Beginning of Statement (near 'brand')
Brand is on the second line of the SQL statement.
The data that I am trying to enter:
INSERT INTO 'vehicles'
('reg_no', 'category', 'brand', 'description', 'dailyrate') VALUES
('BA5923W', 'car', 'Toyota', 'black 4 door 2.6 engine ', '9.99'),
('BA6611A', 'car', 'NISSAN ', '4 Door Saloon, Automatic', '9.99'),
('BM1245a', 'car', 'Golf', NULL, '9.00'),
('GA5955E', 'truck', 'NISSAN CABSTAR 3.0', 'Lorry, Manual ', '18.99')
cheers
You have an syntax error at you query.
For columns names you should use Grave Accent ` char like
(`reg_no`, `category`, `brand`, `description`, `dailyrate`)
and for data line Apostrophe ' char like
('BA5923W', 'car', 'Toyota', 'black 4 door 2.6 engine ', '9.99')
Fixed. I was not closing the block of SQL code that created the table, which comes before this.

What is wrong with this SQLite query?

I'm creating an AIR application which connects to a SQLite database. The database balks at this insert statement, and I simply cannot figure out why. There is no error, it simply does not move on.
INSERT INTO Attendee (AttendeeId,ShowCode,LastName,FirstName,Company,Address,Address2,City,State,ZipCode,Country,Phone,Fax,Email,BuyNum,PrimaryBusiness,Business,Employees,Job,Value,Volume,SpouseBusiness,DateAdded,ConstructionWorkType,UserPurchaser,DecisionMaker,SafetyProducts,NearFuturePurchase,RepContact,Winner) VALUES('39610','111111','SMITH','JIM','COMPANY','1000 ROAD STREET','','PITTSBURGH','PA','15219','','5555555555','5555555555','PERSON#EXAMPLE.NET','','','0000000000000000000','','','','','0?','Fri Jan 30 14:20:08 GMT-0500 2009','other','neither','no','gas_detection','no','no','winner')
I know that the app can connect to the database, because it creates the table just fine. Here's the schema for the table for your reference:
CREATE TABLE Attendee (AttendeeId TEXT PRIMARY KEY,ShowCode TEXT,LastName TEXT,FirstName TEXT,Company TEXT,Address TEXT,Address2 TEXT,City TEXT,State TEXT,ZipCode TEXT,Country TEXT,Phone TEXT,Fax TEXT,Email TEXT,BuyNum TEXT,PrimaryBusiness TEXT,Business TEXT,Employees TEXT,Job TEXT,Value TEXT,Volume TEXT,SpouseBusiness TEXT,DateAdded TEXT,ConstructionWorkType TEXT,UserPurchaser TEXT,DecisionMaker TEXT,SafetyProducts TEXT,NearFuturePurchase TEXT,RepContact TEXT, Winner TEXT)
There is a good chance that there is an error in the INSERT statement, because if I try to execute it on a separate Adobe Air SQLite admin program, it throws an ambiguous error (#3115).
Thanks for any insight you might have.
EDIT:
For those wondering, if I make a simple table, thus:
CREATE TABLE Attendee (AttendeeId TEXT)
And try to insert, thus:
INSERT INTO Attendee (AttendeeId) VALUES('09283A')
I still get error #3115.
Have you tried running smaller statements? Like, a really simple INSERT on a really simple table?
Have you tried quoting the column names? Maybe one of them is a reserved word.
insert into Attendee ("AttendeeId", "ShowCode", "LastName", "FirstName", "Company", "Address", "Address2", "City", "State", "ZipCode", "Country", "Phone", "Fax", "Email", "BuyNum", "PrimaryBusiness", "Business", "Employees", "Job", "Value", "Volume", "SpouseBusiness", "DateAdded", "ConstructionWorkType", "UserPurchaser", "DecisionMaker", "SafetyProducts", "NearFuturePurchase", "RepContact", "Winner") values ('39610', '111111', 'SMITH', 'JIM', 'COMPANY', '1000 ROAD STREET', '', 'PITTSBURGH', 'PA', '15219', '', '5555555555', '5555555555', 'PERSON#EXAMPLE.NET', '', '', '0000000000000000000', '', '', '', '', '0?', 'Fri Jan 30 14:20:08 GMT-0500 2009', 'other', 'neither', 'no', 'gas_detection', 'no', 'no', 'winner');
Try this one, it was computer generated.
Apparently you need to use double quotes rather than single quotes.