sql: insert value - sql

I have a problem about inserting values into the table with sqlite.
supposing the table:
create table test
{
KeyName varchar(50) primary key,
KeyValue varchar (255)
};
I want to insert data like ('john', 'friend'), but I don't know whether the 'john' existed.
Currently I solve it:
using select * where KeyName = "john"
according the result from list 1, I use insert or update;
I'd like to know whether there is better solution?
thanks

you can use insert or replace which replaces the record if it already exists.
so you query be INSERT OR REPLACE INTO
check this link : http://www.sqlite.org/lang_conflict.html

Related

Storing an object into a SQL database in Oracle

Simply trying to find the correct syntax/method to enter create SQL objects and store them inside an oracle database. (school project so it's got to be possible)
CREATE OR REPLACE TYPE Person AS OBJECT
(id NUMBER,
fname VARCHAR(255),
lname VARCHAR(255)) NOT FINAL
CREATE OR REPLACE TYPE customer UNDER Person (
num_purchases NUMBER,
email VARCHAR(255)
);
CREATE TABLE Customers (
id NUMBER,
cust customer
);
INSERT INTO Customers(id, cust)
VALUES (1, customer(1, "John", "Doe", 44, "doezer#gmail.com"));
Returns the error
ORA-00984: column not allowed here
I can't seem to find any way to declare a specific order for putting the values inside of the customer object other than the order that they were declared in. Thank you!
Seems you are inserting columns not literal values. replace your double quotes to single quotes.
INSERT INTO Customers(id, cust)
VALUES (1, customer(1, 'John', 'Doe', 44, 'doezer#gmail.com'));
SELECT ID, TREAT(cust AS Person).id, TREAT(cust AS Person).fname FROM Customers;
See dbfiddle

Bulk Insert - How to tell SQLServer to insert empty-string and not null

This seems like a trivial question. And it is. But I have googled for over a day now, and still no answer:
I wish to do a bulk insert where for a column whose datatype is varchar(100), I wish to insert an empty string. Not Null but empty. For example for the table:
create table temp(columnName varchar(100))
I wish to insert an empty string as the value:
BULK INSERT sandbox..temp FROM
'file.txt' WITH ( FIELDTERMINATOR = '|#', ROWTERMINATOR = '|:' );
And the file contents would be row1|:row2|:|:|:. So it contains 4 rows where last two rows are intended to be empty string. But they get inserted as NULL.
This question is not the same as the duplicate marked question: In a column, I wish to have the capacity to insert both: NULL and also empty-string. The answer's provided does only one of them but not both.
Well instead of inserting empty string explicitly like this why not let your table column have a default value of empty string and in your bulk insert don't pass any values for those columns. Something like
create table temp(columnName varchar(100) default '')

Select row just inserted without using IDENTITY column in SQL Server 2012

I have a bigint PK column which is NOT an identity column, because I create the number in a function using different numbers. Anyway, I am trying to save this bigint number in a parameter #InvID, then use this parameter later in the procedure.
ScopeIdentity() is not working for me, it saved Null to #InvID, I think because the column is not an identity column. Is there anyway to select the record that was just inserted by the procedure without adding an extra ID column to the table?
It would save me a lot of effort and work if there is a direct way to select this record and not adding an id column.
insert into Lab_Invoice(iID, iDate, iTotal, iIsPaid, iSource, iCreator, iShiftID, iBalanceAfter, iFileNo, iType)
values (dbo.Get_RI_ID('True'), GETDATE(),
(select FilePrice from LabSettings), 'False', #source, #user, #shiftID, #b, #fid, 'Open File Invoice');
set #invID = CAST(scope_identity() AS bigint);
P.S. dbo.Get_RI_ID('True') a function returns a bigint.
Why don't you use?
set #invId=dbo.Get_RI_ID('True');
insert into Lab_Invoice(iID,iDate,iTotal,iIsPaid,iSource,iCreator,iShiftID,iBalanceAfter,iFileNo,iType)
values(#invId,GETDATE(),(select FilePrice from LabSettings),'False',#source,#user,#shiftID,#b,#fid,'Open File Invoice');
You already know that big id value. Get it before your insert statement then use it later.
one way to get inserted statement value..it is not clear which value you are trying to get,so created some example with dummy data
create table #test
(
id int
)
declare #id table
(
id int
)
insert into #test
output inserted.id into #id
select 1
select #invID=id from #id

How to specify input format in SQL create table?

I'm new in SQL and I need to create table with specified field format. How to add CHECK condition that will assure that input will be formatted e.g.
[LLLDD]
where L is a letter and D is a digit?
Try this if you are adding the constraint on a new table
CONSTRAINT ck_data_checker CHECK ([columnName] LIKE ('[A-Z][A-Z][A-Z][0-9][0-9]'))
Try this if you are adding the constraint on existing table
ALTER TABLE tableName
ADD CONSTRAINT ck_data_checker CHECK ([columnName] LIKE ('[A-Z][A-Z][A-Z][0-9][0-9]'))
Try this: http://sqlfiddle.com/#!6/3974b
create table test (
field1 char(5),
check (field1 like '[a-z][a-z][a-z][0-9][0-9]')
);
insert into test values ('ttt09'); --this will succeed
If you were to change the insert to:
insert into test values ('testi'); -- this will fail
insert into test values ('12345'); -- this will fail
I'm no sql server expert, but I think you can add a LIKE with a regular expression. Have a look at these websites
https://msdn.microsoft.com/en-us/library/ms179859.aspx
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/dc127433-2982-4065-b290-f411a075a694/use-regular-expressions-to-check-sql-server-2012-table-fields?forum=databasedesign

Insert empty string into INT column for SQL Server

A SAMPLE table has only one column ID of type int, default null.
In Oracle when I do:
insert into SAMPLE (ID) values ('');
the new record is added with blank value. But in SQL Server 2008, when I run the same insert statement, the new record has the value of 0.
Is there a way to force SQL Server 2008 to default blank string to NULL instead of 0 (for numerical type of columns)?
Assuming that your INSERT statement is part of a stored procedure re-used in many places of your application (or, perhaps, is a batch always constructed by the same part of the client code) and that the inserted value is a number passed as a string argument, you could modify the INSERT like this:
INSERT INTO SAMPLE (ID) VALUES (NULLIF(#argument, ''));
Use NULL instead.
insert into SAMPLE (ID) values (NULL);
How about another idea - define an INSTEAD OF INSERT Trigger.
Despite the fact that you're trying to insert a string, with this the operation is "intercepted", empty string is replaced by NULL, and the insert succeeds.
If you define this trigger on your table, then you can continue to insert empty string as before, with no other changes.
Edit: As Martin Smith points out, this effectively is a comparison to 0 (the equivalent of empty string as an int) meaning you won't be able to store 0 in this table. I leave this answer here in case that's acceptable to your situation - either that or re-do all your queries!
CREATE TRIGGER EmptyStringTrigger
ON [SAMPLE]
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO [SAMPLE](ID)
SELECT CASE
WHEN ID = '' THEN NULL
ELSE ID
END
FROM inserted
END
SQL Fiddle example
You can't insert a 'string' into a int column. Oracle must be just handling that for you.
Just try inserting NULL if that's what you need.
insert into SAMPLE (ID) values (NULL);
One more option
insert into SAMPLE (ID) values (DEFAULT)