sql query insert - sql

How can I insert multiple values in 2 tables? I thought something like this, but that obviously doesn't work:
insert into login as l,
klantGegevens as k
(l.password,
l.rechten,
l.status,
k.voornaam,
k.achternaam,
k.woonplaats,
k.postcode,
k.telefoonnr)
values
('test',
1,
1,
'niels',
'jansen',
'Amsterdam',
'5993hk',
0623232323)
EDIT:
string intoDatabase = "insert into login (password,rechten,status) values(#password,#rechten,#status) insert into klantGegevens (voornaam,achternaam,woonplaats,postcode,telefoonnr) values(#voornaam,#achternaam,#woonplaats,#postcode,#telefoonnr)";

Only in separate commands :
insert into login (password,rechten,status)
values('test',1,1)
insert into klantGegevens(voornaam,achternaam,woonplaats,postcode,telefoonnr)
values('niels','jansen','Amsterdam','5993hk',0623232323)

Related

How to insert values into a postgres table using for loop?

I have a script like this in postgres
begin;
INSERT INTO "schema"."table"(price, different_table_foreign_key)
VALUES
(1, 1)
end;
for testing purposes I want to fill table 100 times with the same values as seen above.
how can I do this using a for loop?
No need for a loop, you can use generate_series() for that:
INSERT INTO "schema"."table"(price, different_table_foreign_key)
select 1,1
from generate_series(1,100);
If you want a different value for each row, just use the one returned by `generate_series()
INSERT INTO "schema"."table"(price, different_table_foreign_key)
select 1, g.value
from generate_series(1,100) as g(value)

MS SQL INSERT INTO trouble

I need to insert new record in table.
The first column name ob_no is int, not null. So I need generate number which is maximum ob_no at the moment +1. How can I do it? Something Like (max(ob_no) + 1) but it doesn't work in SQL 2005. Thanks for any ideas.
INSERT INTO et_thanks_2014 (ob_no, c_name)
VALUES (???, 'Some Text')
You should use identities if you don't need values without lag:
INSERT INTO et_thanks_2014 (ob_no, c_name)
SELECT MAX(ob_no) + 1, 'Some Text'
FROM et_thanks_2014

SQL server--> Inserting multiple rows into a table with identity column

I wanna insert a multiple rows into a table with identity column
I have a table called 'STU' with the following columns:
SNO (Primary Key, and Identity [i.e. autoincrementing])
NAME (not null)
CLASS (not null)
SECTION (not null)
CREATE TABLE STU (SNO INT IDENTITY (1,1) CONSTRAINT PK_SNO PRIMARY KEY (SNO),
NAME VARCHAR(25), CLASS VARCHAR(20), SECTION CHAR );
The values are got inserted while executing insert query for each row individually
INSERT INTO STU VALUES('A','1','A');
INSERT INTO STU VALUES('B','2','B');
INSERT INTO STU VALUES('C','3','C');
INSERT INTO STU VALUES('D','4','D');
INSERT INTO STU VALUES('E','5','E');
but while trying to insert multiple values using single insert query in every possible shots, it took throws an error
INSERT INTO STU (SNO,NAME,CLASS,SECTION)
VALUES(NULL,NULL,NULL,NULL)
,('A','B','C','D','E','F')
,('1','2','3','4','5','6')
,('A','B','C','D','E','F');
also
INSERT INTO STU (NAME,CLASS,SECTION) VALUES
('A','B','C','D','E','F'),
('1','2','3','4','5','6'),
('A','B','C','D','E','F');
also
INSERT INTO STU (NAME,CLASS,SECTION) VALUES
('A','1','A'),
('B','2','A'),('C','3','A'),
('D','4','D'),
('E','5','E'),
('F','6','F');
also
INSERT INTO STU (SNO,NAME,CLASS,SECTION) VALUES
(NULL,'A','1','A'),
(NULL,'B','2','A'),
(NULL,'C','3','A'),
(NULL,'D','4','D'),
(NULL,'E','5','E'),
(NULL,'F','6','F');
All your Insert statements are bad:
Make sure you are supplying the same number of columns as in the INSERT statement
Make sure you are not supplying SNO (it's automatic)
Here is an example:
INSERT INTO STU (NAME,CLASS,SECTION) VALUES
('A','B','C'),
('1','2','3'),
('A','B','C');
If you are on SQL Server 2005, use UNION ALL
INSERT STU (NAME,CLASS,SECTION)
SELECT 'A' AS NAME,'1' AS CLASS, 'A' AS SECTION
UNION ALL
SELECT 'B', '2', 'B'
UNION ALL
SELECT 'C', '3', 'C'
Versions later than 2005, you could use
INSERT STU (NAME,CLASS,SECTION)
VALUES ('A','1','A'),
('B','2','B'), ...
You just need to concatenate the SQL you had in your first example.
You can't insert into the identity column (unless you turn identity insert on of course).
INSERT INTO STU (NAME,CLASS,SECTION)
VALUES
('A','1','A'),
('B','2','A'),
('C','3','A'),
('D','4','D'),
('E','5','E'),
('F','6','F');
If you do want to turn identity insert on then you'd need to do the following:
SET IDENTITY_INSERT STU ON
INSERT INTO STU (SNO, NAME,CLASS,SECTION)
VALUES
(1, 'A','1','A'),
(2, 'B','2','A'),
(3, 'C','3','A'),
(4, 'D','4','D'),
(5, 'E','5','E'),
(6, 'F','6','F');
SET IDENTITY_INSERT STU OFF
SQL Fiddle
You can write this as:
INSERT INTO STU VALUES
('A','1','A'),
('B','2','B'),
('C','3','C'),
('D','4','D'),
('E','5','E');

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

Not in In SQL statement?

I have set of ids in excel around 5000 and in the table I have ids around 30000. If I use 'In' condition in SQL statment I am getting around 4300 ids from what ever I have ids in Excel. But If I use 'Not In' with Excel id. I have getting around 25000+ records. I just to find out I am missing with Excel ids in the table.
How to write sql for this?
Example:
Excel Ids are
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
Table has IDs
1,
2,
3,
4,
6,
8,
9,
11,
12,
14,
15
Now I want get 5,7,10 values from Excel which missing the table?
Update:
What I am doing is
SELECT [GLID]
FROM [tbl_Detail]
where datasource = 'China' and ap_ID not in (5206896,
5206897,
5206898,
5206899,
5117083,
5143565,
5173361,
5179096,
5179097,
5179150)
Try this:
SELECT tableExcel.ID
FROM tableExcel
WHERE tableExcel.ID NOT IN(SELECT anotherTable.ID FROM anotherTable)
Here's an SQL Fiddle to try this: sqlfiddle.com/#!6/31af5/14
You're probably looking for EXCEPT:
SELECT Value
FROM #Excel
EXCEPT
SELECT Value
FROM #Table;
Edit:
Except will
treat NULL differently(NULL values are matching)
apply DISTINCT
unlike NOT IN
Here's your sample data:
declare #Excel Table(Value int);
INSERT INTO #Excel VALUES(1);
INSERT INTO #Excel VALUES(2);
INSERT INTO #Excel VALUES(3);
INSERT INTO #Excel VALUES(4);
INSERT INTO #Excel VALUES(5);
INSERT INTO #Excel VALUES(6);
INSERT INTO #Excel VALUES(7);
INSERT INTO #Excel VALUES(8);
INSERT INTO #Excel VALUES(9);
INSERT INTO #Excel VALUES(10);
declare #Table Table(Value int);
INSERT INTO #Table VALUES(1);
INSERT INTO #Table VALUES(2);
INSERT INTO #Table VALUES(3);
INSERT INTO #Table VALUES(4);
INSERT INTO #Table VALUES(6);
INSERT INTO #Table VALUES(8);
INSERT INTO #Table VALUES(9);
INSERT INTO #Table VALUES(11);
INSERT INTO #Table VALUES(12);
INSERT INTO #Table VALUES(14);
INSERT INTO #Table VALUES(15);
Import your excel file into SQL Server using the Import Data Wizard found in SQL Server Management Studio.
Then you can write the following query to find any IDs which are in the file but not in the table:
SELECT id
FROM imported_table
WHERE id NOT IN (SELECT id FROM db_table)
You should move excel data to a table in SQL Server, and then do the query in SQL Server.
select distinct id from Excel where id not in (select your ids from Sqltable)
(Obviously select your ids from Sqltable is a select which returns the Ids existing on SQL Server).
You may think that moving data to SQL Server is hard to do, but, on the contrary, it's very easy:
1) create a table
CREATE TABLE ExcelIds (Id int)
2) add a new column in excel with the following formula:
="insert into ExcelIds values(" & XX & ")"
where XX is the reference to the cell in the column with excel Ids.
3) copy the "inserts" from Excel into SSMS or whatever tool you're usin in SQL Server, and execute them.
Now you have 2 tables in SQL Server, so that querying it is absolutely easy.
When you're over, just drop the table
DROP TABLE ExcelIds
NOTE: I didn't create a key on SQL Server table because I suppose that the Ids can be repeated. Neither is justified to create a more complex SQL Query to avoid duplicates in ExcelIds for this ad hoc solution.