What is wrong with this SQL statement - sql

The error is:
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 'RA---SIN', 'DEC--SIN', 0.0,-90.0)' at line 1
INSERT INTO files_table (filename, folder, survey, telescope, author, observer, equinox, ctype1, ctype2, crval1, crval2) VALUES('H001_abcde_luther_chop.fits', 'C:\dev\data\FITS\surveys\', '', '','', -1.0, 'RA---SIN', 'DEC--SIN', 0.0,-90.0)
The statement that created the table was (the line breaks are just for ease of reading)
create table files_table (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
filename varchar(255), folder varchar(255), survey varchar(255), telescope varchar(255),
author varchar(255), observer varchar(255), equinox double, ctype1 varchar(255), ctype2
varchar(255), crval1 double, crval2 double);
Is it because I use ' instead of " - this hasn't troubled me before
Is it due to the -- in RA---SIN and DEC--SIN

It is because of the backslash before the single quote. Escape your backslashes (like so \\) and it should be ok.

I think it's because you are missing a value
You have 11 columns named and only 10 values

This is because you're trying to insert RA--SIN into equinox column, which is of type double.
I believe you're missing an '', so the query would work like this:
INSERT INTO files_table (filename, folder, survey, telescope, author, observer, equinox, ctype1, ctype2, crval1, crval2) VALUES('H001_abcde_luther_chop.fits', 'C:\dev\data\FITS\surveys\', '', '','','' -1.0, 'RA---SIN', 'DEC--SIN', 0.0,-90.0)

Related

Is there anyway to use BULK INSERT from .txt file and choose specific column to import into SQL?

I have a .txt file that consists of around 80 columns and 1,900,000 rows. I want to import specific columns into SQL Server using BULK INSERT and make a new table but I kept getting an error that says the column format is wrong. I also need to keep NULLS since some columns have NULL. I created a new table that consists of all the columns that I chose out of the 80 columns. I tried to import this data through the Import and Export wizard and it took me around 10 hours which is strange, that is why I want to try importing it through query.
My code:
CREATE TABLE IM20190930
(
FACILITY_NUMBER varchar(255),
CUSTOMER_NUMBER varchar(255),
BRANCH_CODE varchar(255),
ACCOUNT_STATUS varchar(255),
SEGMENT_RULE_ID varchar(255),
PD_SEGMENT varchar(255),
RATING_CODE varchar(255),
EXCHANGE_RATE varchar(255),
GROUP_SEGMENT varchar(255),
DOWNLOAD_DATE varchar(255),
OUTSTANDING varchar(255),
BI_COLLECTABILITY varchar(255),
DAY_PAST_DUE varchar(255)
)
BULK INSERT dbo.IM20190930
FROM 'c:\Users\Emily\Desktop\IM20190930.txt'
WITH
(FIELDTERMINATOR=',',
ROWTERMINATOR='|',
KEEPNULLS)
You can use the FORMATFILE = 'format_file_path' option to specify the mapping between file source and table columns.
A format file can be expressed in text or XML.
From the doc

Incorrect syntax around datetime2

The following CREATE statement is meant for SQL Server:
CREATE TABLE tclientlink
(
link_id INT,
ext_client_id VARCHAR(255),
goald_address_id VARCHAR(255),
goald_client_id VARCHAR(255),
instance_id VARCHAR(255),
source_id VARCHAR(255),
timestamp DATETIME2
);
The INSERT statement
INSERT INTO TCLIENTLINK(link_id, ext_client_id, goald_address_id, goald_client_id, instance_id, source_id, timestamp)
VALUES (13582, "0000059811", "3037260", "0000059811", "1", "1", 2018-08-22 15:13:34);
But when I try to validate this using an online tool, I get the following error message:
You have an error in your SQL syntax; it seems the error is around: 'datetime2 )'
What change do I need to make to my DDL above?
double quote indicate column name, as a result if you use double quote sql server engine will search that column name , so in case of value you have to use single quote and also for datetime column value should be quoted. so working query is below
CREATE TABLE tclientlink
(
link_id INT,
ext_client_id VARCHAR(255),
goald_address_id VARCHAR(255),
goald_client_id VARCHAR(255),
instance_id VARCHAR(255),
source_id VARCHAR(255),
timestamp DATETIME2
);
INSERT INTO TCLIENTLINK(link_id, ext_client_id, goald_address_id, goald_client_id, instance_id, source_id, timestamp)
VALUES (13582, '0000059811', '3037260', '0000059811', '1', '1', '2018-08-22 15:13:34');
Your insert statement might be this.
use ' instead of " for string value, and the datetime2 need to use ' contain it.
INSERT INTO TCLIENTLINK(link_id, ext_client_id, goald_address_id, goald_client_id, instance_id, source_id, timestamp) VALUES(13582,'0000059811','3037260','0000059811','1','1', '2018-08-22 15:13:34');
sqlfiddle

PHPmyadmin won't accept CREATE TABLE statement

I'm trying to use a simple CREATE TABLE statement to create a new table in my very first SQL-database. PHPmyadmin won't accept the code and gives me an error statement.
There doesn't appear to be anything wrong with the syntax of my SQL command. In fact, I receive the same error statement when I copy and past an example code from any internet tutorial to create a table.
this is my SQL command:
CREATE TABLE Guestbook(
ID int AUTO_INCREMENT PRIMARY KEY NOT NULL,
Name varchar(50) NOT NULL,
Message TEXT NOT NULL,
Date datetime,
Sport varchar(30),
Practicioner BOOLEAN default 0,
)
This is the error statement:
Static analysis:
3 errors were found during analysis.
A symbol name was expected! (near "Name" at position 74)
Unexpected beginning of statement. (near "50" at position 87)
Unrecognized statement type. (near "NOT NULL" at position 91)
SQL query:
CREATE TABLE Guestbook( ID int AUTO_INCREMENT PRIMARY KEY NOT NULL, Name varchar(50) NOT NULL, Message TEXT NOT NULL, Date datetime, Sport varchar(30), Practicioner BOOLEAN default 0, )
MySQL said: Documentation
#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 ')' at line 8
I can't imagine why this won't work. I'd like to be able to use to command line in phpMyadmin. and this seems pretty straigtht forward. Yet I've been fiddling around with it for ages and I can't figure out how to create even the simplest possible table.
Can anyone help me out?
You should remove the last comma in your CREATE statement:
CREATE TABLE Guestbook(
ID int AUTO_INCREMENT PRIMARY KEY NOT NULL,
Name varchar(50) NOT NULL,
Message TEXT NOT NULL,
Date datetime,
Sport varchar(30),
Practicioner BOOLEAN default 0
)

why can't I insert data into tables I created in sql server managment studio 2012? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I created the tables with this:
CREATE TABLE COSTUMER(
COSTUMER_ID INT,
TAXI_ID INT,
COSTUMER_PHONE_NUMBER INT,
COSTUMER_NAME VARCHAR(40)
DESTINATION VARCHAR(40)
);
CREATE TABLE TAXI(
TAXI_ID INT,
COSTUMER_ID INT,
TAXI_REGISTRATION_NUMBER INT,
TAXI_PHONE_NUMBER INT,
DRIVER_NAME INT,
DESTINATION
);
CREATE TABLE BOOKING(
DESTINATION VARCHAR(40),
TAXI_ID INT,
COSTUMER_ID,
COSTUMER_PHONE_NUMBER INT,
DRIVER_PHONE_NUMBER INT,
TAXI_REGISTRATION_NUMBER INT,
TAXI_NAME VARCHAR(40)
COSTUMER_NAME VARCHAR(40)
TIME_OF_ARRIVAL DATETIME,
);
After this my table appeared with all the attributes. Then I set my PKs and FKs manually.
now am trying to insert some data to each of the tables but I keep getting error 213:
Column name or number of supplied values does not match table definition
I searched and searched and still don't understand.
also when I insert the code for such as SELECT * FROM Costumer (that's the table name) it won't show in the small suggestion box as if it doesn't exist???
The below code should work for your requirement unless you have specified an identity column manually as you said that you made some changes manually.
In case you specified an identity column manually then you should not insert the value for the Identity column (say Customer_ID) or you should set SET IDENTITY_INSERT COSTUMER ON before inserting the ID column
CREATE TABLE COSTUMER(
COSTUMER_ID INT,
TAXI_ID INT,
COSTUMER_PHONE_NUMBER INT,
COSTUMER_NAME VARCHAR(40),
DESTINATION VARCHAR(40)
);
SELECT * FROM COSTUMER
INSERT INTO COSTUMER (COSTUMER_ID, TAXI_ID,COSTUMER_PHONE_NUMBER,COSTUMER_NAME,DESTINATION) VALUES (1, 33,123211241, 'John','Mexico' )
I think you are trying to insert more/less input to table
INSERT INTO [TableName] (Column1, Column2, Column3)
VALUES (Value1, Value2, Value3)
Number of columns should be equal to Number of Values. Please check your INSERT statement.
If you not included (Column1, Column2, Column3) in your statement, include it.
Post the insert statement you used. I see a few problems with the current code you posted.
You don't provide datatypes for a few of your attributes, for instance in the Booking table you need to provide some type for the CUSTOMER_ID attribute. Also you need the attributes and datatypes to be comma separated, except the last one doesn't need a trailing comma. For instance you have:
...
TAXI_REGISTRATION_NUMBER INT,
TAXI_NAME VARCHAR(40)
COSTUMER_NAME VARCHAR(40)
TIME_OF_ARRIVAL DATETIME,
...
This will throw an error probably saying incorrect syntax near COSTUMER_NAME.
You need something like:
CREATE TABLE BOOKING(
DESTINATION VARCHAR(40),
TAXI_ID INT,
COSTUMER_ID INT,
COSTUMER_PHONE_NUMBER INT,
DRIVER_PHONE_NUMBER INT,
TAXI_REGISTRATION_NUMBER INT,
TAXI_NAME VARCHAR(40),
COSTUMER_NAME VARCHAR(40),
TIME_OF_ARRIVAL DATETIME
);
For an insert try something along the lines of:
INSERT INTO <table_name> (<column1>,<column2>,<column3>)
VALUES (<col1Value>,<col2Value>,<col3Value>)
If you aren't getting auto completion you might try and refresh the DB in the management studio by right click > refresh, but I wouldn't rely on auto completion at all in SQL Server nor IntelliSense unless you use someting like this.
If you post the insertion script I might be able to help more.
Update: So, the problem you got was with arithmetic overflow, specifically trying to put 07854625781 into a int. All datatypes have maximum/minimums or other "constraints", an int can be in the range of -2^31 (-2,147,483,648) through 2^31 - 1 (2,147,483,647) (4 bytes). The value you enter exceeds that, it is being interpreted as 07,854,625,781 (7.8 billion) while the maximum for an int is only 2.147 billion. If you change the datatype of phone number to bigint that can hold -2^63 (-9,223,372,036,854,775,808) through 2^63-1 (9,223,372,036,854,775,807) (8 bytes) it will work, However I would advise you not to store a phone number in that way, rather, use char(length) if you know that the size and format will always be the same, if you think the length could vary (maybe from extensions or international numbers) I would use something along the lines of varchar(40).
Through this datatype change you will just have to surround input in 'single quotes' when you insert.
Cheers
The Problem that you are facing in the below insert query is the value 07854625781 has become too large to handle for int type. Int range 2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647). So change the datatype of COSTUMER_PHONE_NUMBER to Bigint.
INSERT INTO COSTUMER (COSTUMER_ID, TAXI_ID, COSTUMER_PHONE_NUMBER, COSTUMER_NAME, DESTINATION) values (1, 32, 07854625781, 'Denzel Washington', 'Heathrow Airport')
use the below query to change the datatype to bigint and then try insert(above command)
ALTER TABLE COSTUMER ALTER COSTUMER_PHONE_NUMBER bigint;
Steps to Follow
ALTER TABLE COSTUMER ALTER COSTUMER_PHONE_NUMBER bigint;
INSERT INTO COSTUMER (COSTUMER_ID, TAXI_ID, COSTUMER_PHONE_NUMBER, COSTUMER_NAME, DESTINATION) values (1, 32, 07854625781, 'Denzel Washington', 'Heathrow Airport');
Now do
Select * from COSTUMER
you will find the record inserted

Can't create new table

I'm new to SQL Server 2000 and face a problem. I want to make a new table but I encounter an error message with the following code:
create table Buku
(
Kode_Buku char(5) constraint PK_Kode_Buku Primary Key,
Judul_Buku varchar(10)not null,
Nama_Pengarang varchar(30) not null,
Penerbit varchar(30),
Kota_Terbit varchar(30) default,
Tahun_Terbit varchar(4) default,
Bahasa varchar(4) check,
Harga_Jual money,
)
and here's the error code:
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'Buku'.
You have three problems:
You have multiple cases where you say default but don't specify anything
You have a check but don't specify anything
Your last column definition says money, with a trailing comma
Now, none of these lead to the exact error message you're getting, so maybe there is more you're not telling us (is there more code before the create table bit?), but these little syntax problems are far too localized to be useful in this Q & A format.
EDIT
I just ran this on a SQL Server 2000 instance and it worked just fine:
create table Buku
(
Kode_Buku char(5) constraint PK_Kode_Buku Primary Key,
Judul_Buku varchar(10) not null, -- added space here
Nama_Pengarang varchar(30) not null,
Penerbit varchar(30),
Kota_Terbit varchar(30), -- removed default here
Tahun_Terbit varchar(4), -- removed default here
Bahasa varchar(4), -- removed check here
Harga_Jual money -- removed comma here
)
So I'm not sure what you're doing differently, but I can't get the error message you are seeing with the information you've provided in the question. If you're still getting an error message with this code (and only this code), you'll need to provide more information, such as ##VERSION, what interface you're using to submit the create table statement to SQL Server, etc.