Postgres insert into a table - sql

I have a SQL script like this which I run from the command line using psql:
insert into "A"."B" values
(1, 'name=a', 'a#example.com', 'K')
How do I convert it into INSERT command inside a database?
INSERT INTO "A"."B" (first_column, second_c, third_c, fourth_1)
VALUES ('2', 'name=a', 'a#example.com.com', 'K');
Also what does "A"."B" do? I read somewhere that double quotes are needed when table name has Capitals. I seem to get an error with that when I run commands inside the database.

You said that your database name was DB and your table name was B.
You can simply use the table name alone:
INSERT INTO "B" (first_column, second_c, third_c, fourth_1)
VALUES ('2', 'name=a', 'a#example.com.com', 'K');
If you want to include the database name, then use:
INSERT INTO "DB"."B" (first_column, second_c, third_c, fourth_1)
VALUES ('2', 'name=a', 'a#example.com.com', 'K');
The double quotes are only required when the name of any entity (e.g. table, column, etc...) is a reserved word.

You can use this query where A is schema and B is table name.
INSERT INTO "A"."B" (first_column, second_c, third_c, fourth_1)
VALUES ('2', 'name=a', 'a#example.com.com', 'K');

Related

SQL Server : newline character

I have a programming language that does not allow me to write queries on multiple lines. It has to be written all in a single line.
I am unable to send a GO command because it has to be in a new line..
So for example, this does not work:
insert into mytable (field1, filed2) values (1, 2), (3, 4); go
as it should be
insert into mytable (field1, filed2) values (1, 2), (3, 4);
go
I've tried multiple things but none worked:
insert into mytable (field1, filed2) values (1,2),(3,4); \r go
insert into mytable (field1, filed2) values (1,2),(3,4); \r\n go
insert into mytable (field1, filed2) values (1,2),(3,4); $r$n go
insert into mytable (field1, filed2) values (1,2),(3,4); char(10) go
insert into mytable (field1, filed2) values (1,2),(3,4); char(13) go
Is there a way to write it inline, and have SQL Server use it as 2 different lines?
GO is not part of the TSQL language. It is used and recognized only by SSMS and sqlcmd to cut your script into parts ("batches"), and then each part is compiled and run separately, one after the other.
The reason that SSMS and sqlcmd work this way this is that it makes it possible to have e.g. a CREATE TABLE statement, followed by INSERT statements for that table. The INSERT-part will only compile if the table already exists, and that will be the case only after the CREATE has been run.
It is OK to combine multiple INSERTs into one statement. When in doubt about where the next statement should start, you can add a semicolon (;) to mark the end of the previous statement.

What's wrong with this SQL INSERT statement?

I want to insert a certain value into a certain field of five different rows altogether. But whenever I run this query it doesn't get executed. What's wrong with it, and how can I fix it?
INSERT INTO `employee`(`password`) VALUES ('abc') WHERE `id` IN (1,2,3,4,5);
An INSERT can't have a WHERE clause. It looks like you meant to do an update:
UPDATE `employee`
SET `password` = 'abc'
WHERE `id` IN (1,2,3,4,5);
Or perhaps a multi-row insert:
INSERT INTO `employee` (`id`, `password`)
VALUES (1, 'abc'), (2, 'abc'), (3, 'abc'), (4, 'abc'), (5, 'abc');
Also, FYI, you really shouldn't be storing passwords as plain text, which it looks like you may be doing.

Missing Semicolon at the end of sql statement Access

I am trying to execute the following code. However, I continue to recieve the following 'Missing semicolon (;) at the end of SQL statement error in Microsoft Access.
The first query creates the table with the columns defined.
create table test
(
ProcessID int,
Name varchar(10),
Address varchar(10),
RandomData varchar(10)
);
The second query is causing the missing semicolon error.
INSERT into test
VALUES (123 , 'TestName', 'TestAdd', 'qwrj3ri'),
(456 , 'TestName2', 'TestAdd', 'qwerty'),
(789 , 'TestName', 'TestAdd', 'qwrj3ri'),
(1234, 'Testing123', 'tester', 'asdfghjk');
Code with amendments per above comments to make it Access friendly & remove typos:
INSERT INTO test ( ProcessID, Name, Address, RandomData)
VALUES (123 , 'TestName', 'TestAdd', 'qwrj3ri');
INSERT INTO test ( ProcessID, Name, Address, RandomData)
VALUES (456 , 'TestName2', 'TestAdd', 'qwerty');
INSERT INTO test ( ProcessID, Name, Address, RandomData)
VALUES (789 , 'TestName', 'TestAdd', 'qwrj3ri');
INSERT INTO test ( ProcessID, Name, Address, RandomData)
VALUES (1234, 'Testing123', 'tester', 'asdfghjk');
Useful reference: https://msdn.microsoft.com/en-us/library/bb243852(v=office.12).aspx
Specific comments:
#Damien_The_Unbeliever:
I don't think access supports multiple rows in the values.
Amended to include an insert into per row instead of a value set per row (values (...), (...)).
#Thomas Tschernich:
our missing single quote next to the end of your insert
Changed 'tester', sdfg') to 'tester', 'sdfg');
#JohnLBevan:
superfluous character on end of first set of values
Changed 'qwrj3ri'), T to 'qwrj3ri'),
You can insert multiple rows in one insert statement in SQL server,but in MS ACCESS its not possible as above listed.
More techniques on multiple inserts in access are described
beautifully here

SSRS: How do I display a list of customers, from A - K then L to Z, without having two separate reports?

I would like one report with a drop down menu, showing:
Customers A-K,
Customers L-Z
All Customers
I have an SQL table for customers, with the standard columns (Acc No., name, address, etc).
I would like this split by name as it takes a long time to bring the result of the entire table down.
I would prefer this to be in one report. Is this possible? I am using SQL Server 2008 R2 with reporting services.
I have tried a few different methods with no luck, so I'm open to any suggestions!
Thanks for reading my question and thanks in advance if you can find the time to help!
Please feel free to ask any questions.
create table #alphab
( alphaname varchar(15));
insert into #alphab VALUES ('ANT')
insert into #alphab values ('CAT')
insert into #alphab values ('pAT')
insert into #alphab values ('mAT')
insert into #alphab values ('dAT')
insert into #alphab values ('rAT')
insert into #alphab values ('dAT')
insert into #alphab values ('lAT')
insert into #alphab values ('cAT')
insert into #alphab values ('zAT')
insert into #alphab values ('xAT')
insert into #alphab values ('wAT')
insert into #alphab values ('oAT')
insert into #alphab values ('sAT')
insert into #alphab values ('yAT')
insert into #alphab values ('uAT')
select alphaname
from #alphab
where alphaname LIKE
CASE
WHEN #alphabetorder = 1 THEN ('[A-K | a-k]%')
WHEN #alphabetorder= 2 THEN ('[L-Z | l-z]%')
END
order by alphaname

SQL Oracle error

insert into tour_concerts values
('1', to_date('02/08/1974', 'DD/MM/YYYY'), 'Misc Concerts', 'UK'),
insert into tour_concerts values
('2', to_date('01/01/1977', 'DD/MM/YYYY'), 'The Hoople North America Your', 'USA'),
insert into tour_concerts values
('3', to_date('05/09/1971', 'DD/MM/YYYY'), 'Sheer Heart Attack UK tour', 'UK'),
insert into tour_concerts values
('4', to_date('09/02/1972', 'DD/MM/YYYY'), 'Works Japan tour', 'Japan'),
insert into tour_concerts values
('5', to_date('03/10/1975', 'DD/MM/YYYY'), 'Magic Tour', 'UK'),
insert into tour_concerts values
('6', to_date('02/01/1974', 'DD/MM/YYYY'), 'Freddie Mercury Tribute Concert for AIDS Awareness', 'UK');
SQL> #tour_concerts1;
('6', to_date('02/01/1974', 'DD/MM/YYYY'), 'Freddie Mercury Tribu
te Concert for AIDS Awareness', 'UK')
*
ERROR at line 2:
ORA-12899: value too large for column "S3327043"."TOUR_CONCERTS".
"TYPE" (actual: 50, maximum: 30)
Can someone help me fix this error?
OKAY I'VE FIXED IT
This error is clearly stating that you're trying to insert a too long varchar value in the 3rd column of the tour_concerts table.
You can fix this by:
Altering the table's structure to make the column accept more than 30 characters. For instance, 50 is the number of characters of the statement that is failing.
alter table tour_concerts modify column_name varchar2(50)
Use the Oracle substr function:
`insert into tour_concerts values ('6', to_date('02/01/1974', 'DD/MM/YYYY'), substr('Freddie Mercury Tribute Concert for AIDS Awareness', 0, 30), 'UK');
If these records are inserted thorugh an application via jdbc for instance, trim user input to not exceed your table's maximum sizes.
Error is clear: TOUR_CONCERTS.TYPE field allows 30 chars, no more, and you're trying to insert a longer string.
You have two ways to solve this:
Shorten string you're inserting (Freddie Mercury Tribute Concert for AIDS Awareness is longer than 30 chars)
Change your table definition and set TOUR_CONCERTS.TYPE field length to an higher value (say 200?)
Huh.I think you should alter your table.30 is too short.
If your DB is oracle:
alter table YOURTABLE alter column 'S3327043' varchar(100)
Check this article it seems your trying to insert longer value than the field, make the fields bigger than the maximal value you could need.