Inserting multiple rows using SQL - issue with manually incrementing numbers - sql

Someone else designed this table and I am not allowed to modify it so bear with me.
I am trying to insert multiple rows from one table into another. The table where I am inserting the rows has an ID but it does not auto-increment. I cannot figure out how to manually increment the id as I insert rows. The current code throws an error:
Error running query. Page15.CaseSerial is invalid in the select list
becauseit is not contained in either an aggregate function or the
GROUP BY clause.
I've tried adding a GROUP BY clause with no success.
Here's the code:
insert into page4 (serial, caseserial, linkserial, type, add1, add2, city, state, orgname, prefername, email, firstname, lastname, salutation, contactstatus, workphone, notes, cellphone, nametype, homephone, fax, zip, payments)
select id = max(serial), caseserial, linkserial, type, add1, add2, city, state,
orgname, prefername, email, firstname, lastname, salutation, contactstatus,
workphone, notes, cellphone, nametype, homephone, fax, zip, payments
from page16
It would be nice if I could write something to get the highest id from page4 and insert the next highest.
Thanks!

declare #maxId int
select #maxId = max(yourIdColumn)
from YourTable
set #maxId = #maxId + 1
insert into YourTable (yourIdColumn, ....)
values (#maxId, ....)
Disclaimer: not sure how this would transpose over to other RDBMS's, but this is with SQL Server in mind. Also, this handles inserting only one value. If you need to insert a set of values, then please let me know.

Related

Oracle Sql : unable to use 'With' clause when using group by 'Cube'

I have the following sample code as reference:
Create table wait_weekly as select XXX
.....;
Create table wait_weekly_prev as select
....
from
Wait_weekly
group by
cube(var1, var2);
This works fine.
However, if I put the table wait_weekly either in the 'With' clause or directly in a subquery, like
with wait_weekly as (select XXX)
Create table wait_weekly_prev as select
....
from
(select XXX...)
group by
cube(var1, var2);
`
it will give me the same error message and won't recognize a variable is an invalid identifier.. Any suggestion? thanks.
Here is the sample code, but the reality is that it works in this sample code. Just imagine it's not working, and the error message is that 'invalid identifier for one of the variables in table Test". I did remove the cube( lastname, sex) and replaced with nvl(Lastname,'All_lastname') as Lastname,
nvl(Sex,'All_sex') as Sex, and the codes work; but I do need cube( ..., ...).
Hope this is clear enough, thanks.
create table test (
Lastname VARCHAR2(12),
Sex VARCHAR2(12),
Age NUMBER);
insert all
into test (Lastname, Sex, Age) values ('Sun', 'M', 8)
into test (Lastname, Sex, Age) values ('Thomas','M',12)
into test (Lastname, Sex, Age) values ('Sun','F',13)
into test (Lastname, Sex, Age) values ('Thomas','F',15)
into test (Lastname, Sex, Age) values ('Berg','F',18)
SELECT 1 FROM DUAL;
Example queries:
with test_1 as
(select * from test where lastname <> 'Berg')
select
nvl(Lastname,'All_lastname') as Lastname,
nvl(Sex,'All_sex') as Sex,
sum(Age) as Age
from test_1
group by cube(Lastname, Sex);
select
nvl(Lastname,'All_lastname') as Lastname,
nvl(Sex,'All_sex') as Sex,
sum(Age) as Age
from (select * from test where lastname <> 'Berg')
group by cube(Lastname, Sex);

How to use a function that returns a table within an INSERT statement?

I have a function that returns 5 columns (address) as a table. I need to use the result to insert into another table:
INSERT INTO mytable
(col1, address1, address2, address3, address4, address5)
SELECT
Id, HouseNo, Street, PostCode, City, Country
FROM #someTableType
JOIN dbo.GetAddress(#customerId);
The Id column is from #someTableType. The rest of the columns are returned by the function.
But the above doesn't work.
What is the correct way?
change your query to this
INSERT INTO mytable
(col1, address1, address2, address3, address4, address5)
SELECT
someTableType.Id, adress.HouseNo, adress.Street, adress.PostCode, adress.City,
adress.Country
FROM #someTableType
Cross Apply dbo.GetAddress(#customerId) as adress;
but you have change function beacause it always return same resault because of #customerId always have one value

SQL command using parameters syntax error

Hi i am new to sql and c# queries. I am using a select statement and puttin # parameters in using SQL.
string cmd = "select (FirstName, LastName, Address, City, PostalCode, Country, Username) values (#FirstName, #LastName, #Address, #City, #PostalCode, #Country, #Username) from [Customer];";
It doesnt like my sql can anyone tell me why? it says incorrect syntax but i can not spot where?
Thanks
Dom
The SELECT statement is used to select data from a database.
If you want to retrieve data you don' t have to add the values keyword:
SELECT column_name,column_name
FROM table_name;
With your query:
string cmd = "SELECT FirstName, LastName, Address, City, PostalCode,
Country, Username FROM Customer;";
In case you wanted to retrieve data with the parameters this is the syntax:
SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;
With your query:
string cmd = "SELECT FirstName, LastName, Address, City, PostalCode,
Country, Username FROM Customer WHERE FirstName=#FirstName AND
LastName=#LastName AND Address=#Address AND City=#City AND
PostalCode=#PostalCode AND Country=#Country AND Username=#Username;";
Altough this is not really correct because you can just check for the id you set on the customer table.
string cmd = "SELECT FirstName, LastName, Address, City, PostalCode,
Country, Username FROM Customer WHERE *yourID*=*#yourID*;";
You can have a try here:
http://www.w3schools.com/sql/sql_select.asp
http://www.w3schools.com/sql/sql_where.asp
Look at this link, you should be using a SELECT INTO statement.
http://www.w3schools.com/sql/sql_select_into.asp
SELECT column_name(s)
INTO newtable [IN externaldb]
FROM table1;

Inserting column data from another table together with more value

How to insert few columns from TableA to tableB together with some additional values.
Following is one way I tried and failed, but it shows clearly what I want to achive:
Insert into
TableA (UserID, FirstName, Lastname,EmailAddress,IsActive,IsOnline,IsLockedOut,Comment)
values
(Select distinct UserID, FirstName, LastName, EmailAddress from TableB,0,0,0,'Imported')
You cannot use values when you use select keyword, also you should include constant/static values in your select statement itself, try this
Insert into
TableA
(
UserID, FirstName, Lastname,
EmailAddress,IsActive,IsOnline,
IsLockedOut,Comment
)
Select distinct UserID, FirstName, LastName,
EmailAddress ,
0,0,0,'Imported'
FROM TableB
You need to include the hard code values along with the columns before the FROM part of your query. SO, change your query to this:
Select distinct UserID, FirstName, LastName, EmailAddress, 0, 0, 0, 'Imported'
from TableB

Select from multiple tables, remove duplicates

I have two tables in a SQLite DB, and both have the following fields:
idnumber, firstname, middlename, lastname, email, login
One table has all of these populated, the other doesn't have the idnumber, or middle name populated.
I'd LIKE to be able to do something like:
select idnumber, firstname, middlename, lastname, email, login
from users1,users2 group by login;
But I get an "ambiguous" error. Doing something like:
select idnumber, firstname, middlename, lastname, email, login from users1
union
select idnumber, firstname, middlename, lastname, email, login from users2;
LOOKS like it works, but I see duplicates. my understanding is that union shouldn't allow duplicates, but maybe they're not real duplicates since the second user table doesn't have all the fields populated (e.g. "20, bob, alan, smith, bob#bob.com, bob" is not the same as "NULL, bob, NULL, smith, bob#bob.com, bob").
Any ideas? What am I missing? All I want to do is dedupe based on "login".
Thanks!
As you say union will remove duplicate records (note that union all won't!). Two records are considered duplicates when all their column values match. In the example you considered in your question it is clear that NULL is not equal to 20 or 'alan' so those records won't be considered duplicates.
Edit:
[...] the only way I can think would be creating a new table [...]
That is not necessary. I think you can do the following:
select login, max(idnumber), max(firstname), max(middlename), max(lastname),
max(email) from (
select idnumber, firstname, middlename, lastname, email, login from users1
union
select idnumber, firstname, middlename, lastname, email, login from users2
) final
group by login
However, if you're sure that you only have different values on idnumber and middlename you can max only those fields and group by all the rest.
You could left join the incomplete table to the complete one via the login. Then programmatically manipulate the resulting set.