I am trying to insert a user into my database using the following insert statement
insert into [user] (username,password,idnumber,address,phonenumber,isAdmin,vehicleid) values ('Langton','123456','63-222-78393','3 where reaod who','999300324',False,0)
But when I run the code it tells me there is a syntax error in my insert statement, what could be the problem.
HiYou missed the quotes for 'False'
The problem lies with your vehicleid, this has to have a value and '0' is an incorrect number for a record/field. Change this to '1' or more.
Related
I am trying to insert into a table I created using multiple values. It throws error on incorrect syntax near coma in the first values line end.
I couldn't identify problem. I did try to insert separately, it inserted like a charm. Now I suspect the multi row inserting has some problem due to declared variable or GETDATE or both? It's not able to hold in session? I tried to put in transaction like begin tran and commit tran, still no luck. I have huge rows, any suggestions please?
DECLARE #USER VARCHAR(40)
SET #USER = SYSTEM_USER
INSERT INTO MyTable (Id, Name, User, Date)
VALUES (1, 'Cris', #USER, GETDATE()),
(2, 'Joel', #USER, GETDATE()),
(3, 'Kris', #USER, GETDATE());
I get this error:
Msg 102, Level 15, State 1, Line 18
Incorrect syntax near ','.
However, no error when inserting separately using the declare statement in individual insert.
The Issue is not with the Data But with your Column Names and the SQL Statement.
In SQL Server, there are certain System reserved Works which we call as Keywords. Usually, We don't entertain the use of these Keywords as Column Names since this may affect the Query Performance. The Column Names
Name, User and Date
Are Keywords or Data Types in SQL Server, so while compiling the statements it will make confusions to the Compiler. So You Can solve this issue with either of the below 2 approaches :
Approach# 1
Change the Column Names which have the Same name as of the Keywords. You Can Identify this column easily by typing the COlumn Name in the SQL Server Management Studio and if the Text Color Changes to Blud, Pink or Gray ( Default Color Settings in SSMS) these are some system-defined Keywords
Blue : Data Types
Pink : System Defined Functions
Gray : Keywords
Please Note :
Since changing the Column name will also cause issues with other Applications and you will have to change it in all those areas This approach is Less preferred if you already have a Stable system. But if you are just in the starting phase of your development then this will be a better way to solve future issues.
Approach# 2
This is the most easiest Idea, in all the places where you are using any of the systems reserved keywords listed above, put them inside Square brackets and this issue will be solved. This is the Quickest way to fix your issue
Your Updated Code should be like this :
DECLARE #USER VARCHAR(40)
SET #USER = SYSTEM_USER
INSERT INTO MyTable (Id, [Name], [User], [Date])
VALUES (1, 'Cris', #USER, GETDATE()),
(2, 'Joel', #USER, GETDATE()),
(3, 'Kris', #USER, GETDATE());
Below is a line of one of my insert statements, it keeps throwing a missing comma error - however Im either blind as a bat or going stupid?!
INSERT INTO barrister (barrister_id,firstname,surname,telephone,email_address)
VALUES (NEXT VALUE FOR sq_barrister, 'John', 'Smith',
'01392345465', 'john_smith#wtflawers.com');
Raul is right. NEXT VALUE FOR is from SQL Server. In Oracle, use
sq_barrister.nextval
I tried to insert some rows into SQL server database and throws an error:
Conversion failed when converting the varchar value 'null' to data type bit
Could anyone explain what is this for?
This is not in a program.
What you might be trying
INSERT INTO TESTTABLE(BITCOLUMN) VALUES('NULL')
What should it actually be..
INSERT INTO TESTTABLE(BITCOLUMN) VALUES(NULL)
If you want to insert a null value into a column, you need to write NULL, and not 'NULL' (wrapping it in quotes indicates that it is a VARCHAR containing the letters N, U, L L - not a null value).
That is what the error message is telling you: you're trying to insert the VARCHAR value 'NULL' into a column that will only accept bit data.
Change the 'NULL' in your insert query to NULL.
You need something like this:
INSERT INTO TableName(ColumnName) VALUES(NULL); --this is the right way
Instead of this:
INSERT INTO TableName(ColumnName) VALUES('NULL'); --this is the wrong way
Excuse my attempt at this, very rusty with SQL.
When I run the following code:
"IF NOT ISNULL Then INSERT INTO [XX].[dbo].[XXX] end if(
I get the following error message "AN EXPRESSION OF NON-BOOLEAN TYPE SPECIFIED"
I have tried to find out how to solve this error with no luck.
Basically I want to insert into [xx] if the cell is 'NOT NULL'.
you're missing the cell value to compare:
try this:
IF NOT ISNULL <cellValue> Then INSERT INTO [XX].[dbo].[XXX] end if ...
Since you refer to "dbo" I guess you're working with Sybase or SQL Server. It looks like you want to insert a single row of values based on a variable at hand. The accepted answer may work in your specific case but I think the more generic solution is below:
insert into <table> (<column list>)
select <values to insert>
where <#variable or column_value> is not null
The accepted answer has syntax that is not valid for Sybase/SQL Server. If you prefer that approach it should look something like the below:
if <cellvalue> is not null begin insert into <table> (...) values (...) end
I cannot figure out why this is not working. I get the same thing when I try to do an update query as well.
Here is the error " There was an error parsing the query. [ Token line number = 1,Token line offset = 43,Token in error = where ] "
Here is the actual Query INSERT INTO ads (title,price,body,enabled,where,interval,posted) VALUES('test','899','test',True,'Columbus',15,'11/25/2009 10:12:30 AM')
Where would be 'Columbus'
I am using visual studio express 2008 C#
WHERE is a reserved word, try wrapping it in brackets
INSERT INTO ads (title,price,body,enabled,[where],interval,posted)
VALUES('test','899','test',True,'Columbus',15,'11/25/2009 10:12:30 AM')
i think you should provide the value of the primary key in your insert statement,maybe SQL Server Compact databases are not generated automatically or you dont configure that.
I had the same problem this is the INSERT statement which was not working and got the same error:
INSERT INTO Customers(CustomerName,CustomerAddress,CustomerPhone)
VALUES ('Osama','Amman','656565')
this is the INSERT statement which was working fine:
INSERT INTO Customers(CustomerID,CustomerName,CustomerAddress,CustomerPhone)
VALUES ('4564','Osama','Amman','656565')
also if you have in your table columns with names have spaces like (Customer Name)
you must use brackets in your sqlCe statement as:
INSERT INTO Customers([CustomerID],[Customer Name],[Customer Address],[Customer Phone])
VALUES ('4564','Osama','Amman','656565')
also if you use SELECT SCOPE_IDENTITY() to get last record inserted in INSERT Statement
as:
INSERT INTO Customers(CustomerID,CustomerName,CustomerAddress,CustomerPhone)
VALUES ('4564','Osama','Amman','656565') SELECT SCOPE_IDENTITY()
don't use it...