VB.NET and Program Architecture - vb.net

I would like to write a program like sales in which there's two table to insert i.e: SaleHeader Table and SaleDetails Table. These two tables have (one to many) relationship. When I insert to SaleDetails Table I need to get SaleHeaderID to insert at SaleDetails.
Now I am using a stored procedure to insert SaleHeader information and to get back SaleHeaderID and I use query statements to insert SaleDetail information.
Any one can advise me for more effective way to insert to those two tables (having one to many relationship) ? Help me! Thanks in advance all.

One approach is to use a Guid for the SaleHeaderID. Then you can generate the new id in your stored procedure (using NEWID()) before inserting the header record, and then you will also know the id so you can use it for the details.

Related

SQL: Insert certain records from one table into another and also add few other fields using query

I have two tables say TABLE1 and TABLE2. And say the field id is common in both. Rest of field are different.
I now select all distinct id from TABLE1 and want to insert them into TABLE2 while also writing its other attributes. Like the pseudocode below.
for each distinct id (i) in TABLE1:
INSERT in TABLE2 (i, false, unix_timestamp())
end
Now I for some reason cannot use a programming language to do this. Is it possible to do this in SQL using Apache Drill?
What you could do is write a query that produces the output you're looking for and then save that as a table. Drill is really a query engine and doesn't support INSERT operations the way a database does.
So a pseudo query migth look like this:
CREATE TABLE <your file> AS
SELECT ...
Then you could query that file. I don't know if that helps or not. You can also create views and temporary tables, but Drill itself doesn't really implement INSERT commands.

Nothing from a table shows up in SQL

I have several table created by recently i can get anything to come from one of them.
The table is called REP and i'm currently using Live SQL from Oracle as i'm quite new to SQL
The select command i'm trying to use.:
select * from REP
I go to my schema and the table is definitely there. No commands for this table work.:
If anyone could help i'd very much appreciate it. Don't be too hard on me i'm quite new to SQL.
Your table is empty. First insert the values into this table via
INSERT INTO REP VALUES(...)
and
then use your query Select * from rep
As in your image, the table is there thats the ddl view meaning table's structure not data i.e. you have only columns but no data.

How should i INSERT new data with TablePerType table structure?

To 'emulate' inheritance in database i decided to use so called Table-Per-Type pattern (lerned recently from StackOverflow). I have the a Users with the most common colums for all users and i have a table Developers with additional info. This table is used to store some additional information about users whose role is developer.
There is Primary key ID for Users, which is AUTO_INCREMENT and also ID column in Developers table which is PK itself and also a FK referencing to Users.ID.
I am writing an SQL Query to populate my table with some test data. Trying to add new Developer i faced the problem: i do not understand how to INSERT new deverloper-user in both tables Users and Developers, because Users.ID is automatically generated and it must be equal to Developers.ID.
Not an answer, long comment
#Derp this functionality is especific to each vendor, even implamentation. Doing some generic sql procedure in standart t-sql to fit all won't be efficient, and kind of imposible.
Try somthing generic as this, only work insertin one by one:
begin tran
insert table1
select top 1 #newId=IdentityColumn from table1 order by IdentityColumn desc
insert table2
commit

How do I concatenate fields into a single field when populating a SQL database (Oracle)

I need to plug a load of data from a separate program into a single table (in Oracle SQL Developer). This transfer of data is going to be in one direction, meaning the system will just occasionally dump a load of data in the table, replacing what was there before. I therefore don't have to worry about being able to update individual fields. I also can't modify how this system transfers the data into my table, which means I am stuck with mapping its fields to my column headers (it's just sending the data using INSERTs behind the scenes).
I want the table to have a unique TRANSACTION_ID column. However, each TRANSACTION_ID might have multiple TRANSACTION_TYPEs, so I will receive multiple rows for each ID with a different TRANSACTION_TYPE. e.g:
INSERT INTO TEST_TABLE (TRANSACTION_ID, TRANSACTION_TYPE) VALUES (1000, TT35)
INSERT INTO TEST_TABLE (TRANSACTION_ID, TRANSACTION_TYPE) VALUES (1000, TT40)
INSERT INTO TEST_TABLE (TRANSACTION_ID, TRANSACTION_TYPE) VALUES (1000, TT12)
INSERT INTO TEST_TABLE (TRANSACTION_ID, TRANSACTION_TYPE) VALUES (1001, TT12)
......etc.
I want to concatenate these into a single field separated by commas, so the final table would look like:
TRANSACTION_ID TRANSACTION_TYPES
-----------------------------------------
1000 TT35,TT40,TT12
1001 TT12
1002 TT40,TT23
I realise that this is de-normalising the data, but since I do not need to update it I am not overly concerned.
I understand the way to do this usually is by using a MERGE, but since I am stuck with the INSERT actions of the source system I cannot use this. Is it possible to do this using a trigger? I've run into mutating table errors etc. in my previous attempts.
The last resort might be to store the TRANSACTION_TYPEs in a separate table, treat the data, and then delete the second table, but that seems ridiculously over-complicated.
Is there a straight-forward way of doing this that I'm missing?
Thanks
This is too long for a comment.
You probably could do this with a trigger, but I wouldn't recommend it. The trigger would need to replace the insert, sometimes doing an insert and sometimes concatenating the values.
Two other options. First, load the data into a staging table and then create a new table that your process.
The second is to just ignore the problem and use list_agg() to bring the data together when you are querying it.

Looping Horizontal Data and Creating Multiple Records per Column

I have a stored procedure called dbo.SalesmanAccts that returns the following data:
I want to update a header and a detail table, namely SalesmanHeader and SalesmanDetail, with the results of dbo.SalesmanAccts.
I want to INSERT INTO SalesmanHeader (SalesmanID, SalesmanName, SalesmanAddress) which is no problem.
The tricky part is the following.
I want to insert MULTIPLE records per salesman in the SalesmanDetail table for each account. That is it say, I want to have the SalesmanDetail table to look like the following:
Notice that account1, account 2, and account3 are all stored as separate records in the SalesmanDetail table.
Is there a way to loop the results of dbo.SalesmanAccts and have it do both INSERT INTO statements in one new stored procedure without creating a separate one for inserting the header records and another one for inserting the detail records?
Is this even possible?
I am working on SQL server 2008R2
Thanks in advance!
INSERT SalesmanDetail
(SalesmanID, SalesmanName, Account)
SELECT SalesmanID, SalesmanName, Account
FROM dbo.SalesmanAccts
UNPIVOT (Account FOR AccountNumber IN (Account1,Account2,Account3) ) p