Insert into script - sql

I tried to add a row to my table named Clienti, I opened query tool and wrote this query, anyway it isn't working, can you tell me the reason? It says the array isn't correctly defined.
INSERT INTO "Clienti"(
"Nome", "Cognome")
VALUES ('example', 'example2');

The problem is that your fields are defined as arrays of strings, not strings. That's why PostgreSQL is complaining about your INSERT statement: You're trying to put a single string into a field that's defined as an array of strings.
"Nome" character(20)[]
means Nome is an array of strings of 20 characters.
You probably want
"Nome" varchar(20)
(for a string of up to 20 characters)
or
"Nome" text
(for no length limit).
See the PostgreSQL documentation for more information about character types.

Try this insert query it will work.
INSERT INTO Clienti(Nome, Cognome)
VALUES ('example', 'example2');

Related

Oracle insert value into a column with power

How can I insert a value into a column with power? Please see the below example:
Can it be done via the UNISTR function?
insert into table values ('2332239 12'); -- I intentionally want to insert the number into a varchar field.
If you mean you want to insert a string that ends with Unicode superscript 12 you can just put Unicode characters in a Unicode string and insert them into a Unicode column:
INSERT INTO table VALUES(N'123¹²')
(Your column will have to be an NVARCHAR)
If your column is a varchar and you can't change it you'll have to encode the data somehow, and decode it very time you want to use it (not ideal)

Enquote multiple text fields in CSV - Sublime Text 3

I have a CSV file containing info to be inserted in a database with SQL using insert similar to the below example:
INSERT INTO `Person`(`name`, `occupation`, `residence`, `comments`) VALUES
(GIBEL ΕΛΛΗ,NULL,NULL,NULL)
I want to separate the text values from the null values. Specifically, I want to enquote text values and leave null as it is. Is there a way to do this using Sublime Text 3? Please bear in mind that I am not familiar with regexp. Each field in my DB is either VARCHAR or TEXT.
Example you supplied
INSERT INTO `Person`(`name`, `occupation`, `residence`, `comments`) VALUES (GIBEL ΕΛΛΗ,NULL,NULL,NULL)
Activate RegEx in ST3 Find and Replace.
Find Value
(.*VALUES \()(.*)(,NULL,NULL,NULL\))
Replace Value
$1"$2"$3
Apply Replace All
Result
INSERT INTO `Person`(`name`, `occupation`, `residence`, `comments`) VALUES ("GIBEL ΕΛΛΗ",NULL,NULL,NULL)

MS SQL does not allow VARCHAR over 128 characters, why?

I have a table with a column configured to hold nvarchar data type.
I am trying to add a row using
INSERT INTO TABLE_NAME VALUES (value1, value2...)
Sql-server gets stuck on a 180 character string that I am trying to assign to the nvarchar data type column returning:
Error: The identifier that starts with [part of string] is too long.
Maximum length is 128.
I don't understand why this is happening since nvarchar(max) should hold 2GByte of storage as I read here: What is the maximum characters for the NVARCHAR(MAX)?
Any ideas of what I've got wrong here?
UPDATE:
The table was created with this:
CREATE TABLE MED_DATA (
MED_DATA_ID INT
,ORDER_ID INT
,GUID NVARCHAR
,INPUT_TXT NVARCHAR
,STATUS_CDE CHAR
,CRTE_DTM DATETIME
,MOD_AT_DTM DATETIME
,CHG_IN_REC_IND CHAR
,PRIMARY KEY (MED_DATA_ID)
)
And my actual INSERT statement is as follows:
INSERT INTO MED_DATA
VALUES (
5
,12
,"8fd9924"
,"{'firstName':'Foo','lastName':'Bar','guid':'8fd9924','weightChanged':false,'gender':'Male','heightFeet':9,'heightInches':9,'weightPounds':999}"
,"PENDING"
,"2017-09-02 00:00:00.000"
,"2017-09-02 00:00:00.000"
,NULL
)
By default, double quotes in T-SQL do not delimit a string. They delimit an identifier. So you cannot use double quotes here. You could change the default but shouldn't.
If this is being directly written in a query window, use single quotes for strings and then double up quotes within the string to escape them:
INSERT INTO MED_DATA VALUES (5, 12, '8fd9924', '{''firstName'':''Foo'',''lastName'':''Bar'',''guid'':''8fd9924'',''weightChanged'':false,''gender'':''Male'',''heightFeet'':9,''heightInches'':9,''weightPounds'':999}', 'PENDING', '2017-09-02T00:00:00.000', '2017-09-02T00:00:00.000', NULL)
But if, instead, you're passing this string across from another program, it's time to learn how to use parameterized queries. That'll also allow you to pass the dates across as dates and not rely on string parsing to reconstruct them correctly.
Also, as noted, you need to fix your table definitions because they've currently nvarchar which means the same as nvarchar(1).
Are you aware of what an Identifier is? Here is a hint - it is a NAME. SQL Server is not complaining about your data, it is complaining about a field or table name. SOmehow your SQL must be totally borked so that part of the text is parsed as name of a field or table. And yes, those are limited to 128 characters.
This is clear in the error message:
Error: The identifier
clearly states it is an identifier issue.

Braces inside the array - postgresql

I have a table "temp" with two attributes: integer, text[].
I would like to insert a record with the brace inside the array.
For example a record like this:
1, {'1{c}1','a'}
where 1 is the integer and '1{c}1' is the first element of the array and 'a' the second element of the array.
I tried a simply insert like this:
INSERT INTO temp VALUES (id, '{'1{c}1','a'}');
but it says that is malformed.
As an addition, it's also possible to use array constructors, I think it's more safe to use, because array elements are just SQL constants and you also could use expressions inside the array constructors:
insert into "temp" values(1, array['1{c}1','a']);
it's clear that this is array of strings, and this too
insert into "temp" values(1, array['1','2']);
According to the PostgreSQL documentation for arrays,
You can put double quotes around any element value, and must do so if it contains commas or curly braces.
A correct syntax would like this:
INSERT INTO "temp" VALUES (1, '{"1{c}1",a}');
You can see a complete, working example on SQL fiddle.
You don't want those inner single quotes.
INSERT INTO temp VALUES (id, '{1{c}1,a}');

Insert Comma Separated Values to SQL

I would like to take a list that I have in a text file, of values which are separated by commas:
Example - 1,2,3,4,5,6,7,8,9,0
then put the values in a database table(I'm going to use this table for auto-complete with jQuery).
I would have done an array for the auto-complete but I have something like 1000 values so I think its better to pull from SQL(am i right?)
Try to explain it to me slowly cause I'm a novice and this is so confusing :)
If those are 1000 constant values (like countries), put them in array.
If they are fairly dynamic, put them in a table
Assuming the table is called T1 and has one field F1, you need to transform the string
1,2,3,4,5,6,7,8....N
to
INSERT INTO T1
VALUES (1),(2),(3),(4),(5),(6),(7),(8)......(N);