Following regex is not working - sql

I have this constraint in the SQL Server statement.
([VehNo] like '[a-zA-Z][a-zA-Z],[a-zA-Z][0-9][0-9][0-9][0-9][a-zA-Z]')
But when I try to insert data this is giving me error.
INSERT INTO [dbo].Car
VALUES ('SGD1234F','Ferrari','F30','2014-09-24','500000.00','500.00','excellent');
The error is as follows
Msg 547, Level 16, State 0, Line 2 The INSERT statement conflicted
with the CHECK constraint "vehNo_ck_validity". The conflict occurred
in database "t322", table "dbo.Car", column 'VehNo'. The
statement has been terminated.
What is the change that I have to make?

Your regexp contains an unwanted comma. I have added spaces to highlight comma in your regexp.
[a-zA-Z][a-zA-Z] , [a-zA-Z][0-9][0-9][0-9][0-9][a-zA-Z]
For such regexp, VehNo should be SG,D1234F
Remove extra comma and your insert statement will work.
This is another version of your regexp: [a-zA-Z]{3}\d{4}[a-zA-Z]

Related

How to Use the OUTPUT of an Insert Query for another insert query

I'm trying to insert 2 records into 2 tables by using a subquery, but it gives me a syntax error.
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'INSERT'.
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near ')'.
The query that I'm trying to execute is
INSERT INTO [Files] ([FileTransformationId],[FileTypeEnumId])
VALUES
(
(INSERT INTO [FileTransformations] OUTPUT INSERTED.FileTransformationId DEFAULT VALUES),
2
)
Is this possible?
You need to store the output in a variable first. So, I think you want something like this:
DECLARE #variable TABLE (value INT)--change type depending on your need
INSERT INTO [FileTransformations]
OUTPUT INSERTED.FileTransformationId INTO #variable(value)
VALUES(.....)
INSERT INTO [Files] ([FileTransformationId],[FileTypeEnumId])
SELECT value, 2 FROM #variable
See How do I use an INSERT statement's OUTPUT clause to get the identity value? for more info on using output variables and Insert into table from table variable? for then inserting from that table variable.
Another option, if you just want the id of the last inserted record is to use scope_identity(). So you could write the above as:
INSERT INTO [FileTransformations]
VALUES(.....)
INSERT INTO [Files] ([FileTransformationId],[FileTypeEnumId])
VALUES scope_identity(), 2

Oracle SQL Syntax Error - missing comma

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

How to ignore the splitting character in strings with Liquibase SQL syntax

I have this SQL script:
Insert into DB_TABLE1 (N, B) values ('30','66');
Insert into DB_EXECUTION (COMMAND)
VALUES ('/* Script generated at 20.04.2006 12:38:44 */
/* error_permissible = 955*/
Create Table map_encodekey
( privatekey VARCHAR2(30) NOT NULL
);
/* error_permissible = 2260*/
Alter Table map_encodekey
Add Constraint map_encodekey_pk
Primary Key
(privatekey);
commit;
');
This script is executed by Liquibase, but it raises this error
...
Caused by: java.sql.SQLSyntaxErrorException: ORA-01756: quoted
string not properly terminated
...
The problem is that Liquibase splits the second insert statement when it finds the ';' in the quoted string of the DB_EXECUTION.COMMAND field; in fact Liquibase doesn't interpret the SQL statements as SQL, but treats them as normal strings.
This is a problem, because I have to split the statements somehow because I have multiple insert statements in the same script (so the splitStatements option is not an option for me) meanwhile I have to tell Liquibase to treat SQL strings as strings which start and end with single quotes.
Is there any way to solve this problem?
Thank you
Giulio

Msg 102, Level 15, State 1, Line 2 Incorrect syntax near ',' trying to INSERT INTO

i've succesfully created db and tables, but when i try populate one of a table, like this
INSERT INTO Products(IsProductActive,ProductName,ProductCount)
VALUES(0,'productName1',0),
(0,'productName2',0),
(1,'productName3',9),
(1,'productName4',7),
(1,'productName5',3),
(1,'productName6',10),
(0,'productName7',0),
(1,'productName8',6),
(1,'productName9',12),
(1,'productName10',20);
GO
i got an error:
Msg 102, Level 15, State 1,
Line 2 Incorrect syntax near ','.
firstly which ',' is meant, secondly - what is wrong?
PS: i use MS Management Studio v 9.0 if it is needed...
Versions of SQL Server 2005 and below do not support the multiple VALUE clause syntax
SQL Server 2005 is version 9...
See How do I insert multiple rows WITHOUT repeating the "INSERT INTO dbo.Blah" part of the statement? for more
if you are using SQL SERVER 2005 and below, the query wouldn't work because it doesn't support multiple value clause insert statement. You should insert it one by one.
Like this one below,
INSERT INTO Products(IsProductActive,ProductName,ProductCount)
VALUES(0,'productName1',0)
GO
INSERT INTO Products(IsProductActive,ProductName,ProductCount)
VALUES(0,'productName2',0)
GO

SQL Server Compact. There was an error parsing the query

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...