Trying to input user information to 2 tables at once sql - android-studio

I am trying to add data to 2 tables at the same time via user input, however all its doing is inserting into the first table and not the 2nd.The code is for an azure sql database
String query = "BEGIN TRANSACTION;
INSERT INTO cc_customer (customer_id,customer_first_name,customer_surname,customer_tel_number,customer_cell_number,customer_status,employee_number)
VALUES ('"+id.toString()+"','"+ name.toString()+"','"+ Lname.toString()+"','"+ Telnum.toString()+"','"+Cellnum.toString()+"','"+Status.toString()+"','"+Empnum.toString()+"');
"+"
INSERT INTO cc_customer_address (customer_address_id,customer_building_number,customer_street,customer_suburb,customer_city,customer_zip_code)
VALUES ('"+Cusnum.toString()+"','"+ Cusbuild.toString()+"','"+ Cusstr.toString()+"','"+ Cussub.toString()+"','"+Cuscity.toString()+"','"+Cuszip.toString()+"')COMMIT;";

this worked!!!!
String query = "BEGIN TRANSACTION INSERT INTO cc_customer(customer_id,customer_first_name,customer_surname,customer_tel_number,customer_cell_number,customer_status,employee_number) VALUES ('"+id.toString()+"','"+name.toString()+"','"+Lname.toString()+"','"+Telnum.toString()+"','"+Cellnum.toString()+"','"+Status.toString()+"','"+Empnum.toString()+"')"+"INSERT INTO cc_customer_address(customer_number,customer_building_number,customer_street,customer_suburb,customer_city,customer_zip_code) VALUES ('"+Cusnum.toString()+"','"+Cusbuild.toString()+"','"+Cusstr.toString()+"','"+Cussub.toString()+"','"+Cuscity.toString()+"','"+Cuszip.toString()+"') COMMIT";

Related

insert table from excel into MongoDB and query it

I have this table in excel (shown in fig1), that I want to insert into MongoDB. I am using the following code which gives me the result as in fig 2, but that is not enough for me as I cannot query any value.
df = pd.read_excel(r'path.xlsx')
# Connect to the MongoDB instance
client = MongoClient("mongodb://localhost:27017/")
db= client['test']
mycollection= db['mycollection']
data = df.to_dict('records')
data = [{str(k): v for k, v in row.items()} for row in data]
db.mycollection.insert_many(data)
I can use a for loop to set all the headers of the ROWS as the ObjectID and then do a query, but I am looking for an easier way to do so.
So either please help me to query a value according to the header of the ROWS based on the code that I have
OR
suggest me a better way to insert the table in MongoDB that will help with the query based on '_id': 'all first values of each row' field.

update and insert into Azure data warehouse using Azure data factory pipelines

I'm trying to run an adf copy pipeline with and update and insert statements that is supposed to replace merge statement. basically a statement like:
UPDATE TARGET
SET ProductName = SOURCE.ProductName,
TARGET.Rate = SOURCE.Rate
FROM Products AS TARGET
INNER JOIN UpdatedProducts AS SOURCE
ON TARGET.ProductID = SOURCE.ProductID
WHERE TARGET.ProductName <> SOURCE.ProductName
OR TARGET.Rate <> SOURCE.Rate
INSERT Products (ProductID, ProductName, Rate)
SELECT SOURCE.ProductID, SOURCE.ProductName, SOURCE.Rate
FROM UpdatedProducts AS SOURCE
WHERE NOT EXISTS
(
SELECT 1
FROM Products
WHERE ProductID = SOURCE.ProductID
)
If the target is an azure sql db I would use this way: https://www.taygan.co/blog/2018/04/20/upsert-to-azure-sql-db-with-azure-data-factory
but if the target is an adw a stored procedure option doesn't exist! any suggestion? do I have to have a staging table first then I run the update and insert statements from stg_table to target_table? or maybe there is any possibility to do it directly from adf?
If you can't use a stored procedure, my suggestion would be to create a second copy data transform. Run the pre-script on the second transform and drop the table since its a temp table that you created on the first.
BEGIN
MERGE Target AS target_sqldb
USING TempTable AS source_tblstg
ON (target_sqldb.Id= source_tblstg.Id)
WHEN MATCHED THEN
UPDATE SET
[Name] = source_tblstg.Name,
[State] = source_tblstg.State
WHEN NOT MATCHED THEN
INSERT([Name], [State])
VALUES (source_tblstg.Name, source_tblstg.State);
DROP TABLE TempTable;
END

Is it possible to chain subsequent queries's where clauses in Dapper based on the results of a previous query in the same connection?

Is it possible to use .QueryMultiple (or some other method) in Dapper, and use the results of each former query to be used in the where clause of the next query, without having to do each query individually, get the id, and then .Query again, get the id and so on.
For example,
string sqlString = #"select tableA_id from tableA where tableA_lastname = #lastname;
select tableB_id from tableB WHERE tableB_id = tableA_id";
db.QueryMultiple.(sqlString, new {lastname = "smith"});
Is something like this possible with Dapper or do I need a view or stored procedure to accomplish this? I can use multiple joins for one SQL statement, but in my real query there are 7 joins, and I didn't think I should return 7 objects.
Right now I'm just using object.
You can store every previous query in table parameter and then first perform select from the parameter and query for next, for example:
DECLARE #TableA AS Table(
tableA_id INT
-- ... all other columns you need..
)
INSERT #TableA
SELECT tableA_id
FROM tableA
WHERE tableA_lastname = #lastname
SELECT *
FROM #TableA
SELECT tableB_id
FROM tableB
JOIN tableA ON tableB_id = tableA_id

inserting a row to oracle table with a sequence in one query

I do not know which is the right way to inserting a new row to ORACLE 11g table with a new ID SEQUENCE. Should I write a trigger to my table something like this before insert operation :
CREATE OR REPLACE TRIGGER "myTableTrigger"
BEFORE INSERT ON "myTable" FOR EACH ROW
WHEN (NEW."Id" IS NULL OR NEW."Id" = 0) BEGIN
SELECT "myTable_SEQ".nextval INTO :NEW."Id" FROM dual;
END;
or should I get the new sequence ID with executescalar() then insert with a new ID? But this way executes 2 different query.
I do not know if there is any other option to do it? Can I do it in a one query?
It is common to use a trigger to do this, but in 11G you don't need the SELECT from DUAL:
CREATE OR REPLACE TRIGGER "myTableTrigger"
BEFORE INSERT ON "myTable" FOR EACH ROW
WHEN (NEW."Id" IS NULL OR NEW."Id" = 0) BEGIN
:NEW."Id" := "myTable_SEQ".nextval;
END;
Of course you don't have to use a trigger, you can just use the sequence in your insert statement:
insert into "MyTable" ("Id", ...) values ("myTable_SEQ".nextval, ...);

Cannot link MS Access query with subquery

I have created a query with a subquery in Access, and cannot link it in Excel 2003: when I use the menu Data -> Import External Data -> Import Data... and select the mdb file, the query is not present in the list. If I use the menu Data -> Import External Data -> New Database Query..., I can see my query in the list, but at the end of the import wizard I get this error:
Too few parameters. Expected 2.
My guess is that the query syntax is causing the problem, in fact the query contains a subquery. So, I'll try to describe the query goal and the resulting syntax.
Table Positions
ID (Autonumber, Primary Key)
position (double)
currency_id (long) (references Currency.ID)
portfolio (long)
Table Currency
ID (Autonumber, Primary Key)
code (text)
Query Goal
Join the 2 tables
Filter by portfolio = 1
Filter by currency.code in ("A", "B")
Group by currency and calculate the sum of the positions for each currency group an call the result: sumOfPositions
Calculate abs(sumOfPositions) on each currency group
Calculate the sum of the previous results as a single result
Query
The query without the final sum can be created using the Design View. The resulting SQL is:
SELECT Currency.code, Sum(Positions.position) AS SumOfposition
FROM [Currency] INNER JOIN Positions ON Currency.ID = Positions.currency_id
WHERE (((Positions.portfolio)=1))
GROUP BY Currency.code
HAVING (((Currency.code) In ("A","B")));
in order to calculate the final SUM I did the following (in the SQL View):
SELECT Sum(Abs([temp].[SumOfposition])) AS sumAbs
FROM [SELECT Currency.code, Sum(Positions.position) AS SumOfposition
FROM [Currency] INNER JOIN Positions ON Currency.ID = Positions.currency_id
WHERE (((Positions.portfolio)=1))
GROUP BY Currency.code
HAVING (((Currency.code) In ("A","B")))]. AS temp;
So, the question is: is there a better way for structuring the query in order to make the export work?
I can't see too much wrong with it, but I would take out some of the junk Access puts in and scale down the query to this, hopefully this should run ok:
SELECT Sum(Abs(A.SumOfPosition)) As SumAbs
FROM (SELECT C.code, Sum(P.position) AS SumOfposition
FROM Currency As C INNER JOIN Positions As P ON C.ID = P.currency_id
WHERE P.portfolio=1
GROUP BY C.code
HAVING C.code In ("A","B")) As A
It might be worth trying to declare your parameters in the MS Access query definition and define their datatypes. This is especially important when you are trying to use the query outside of MS Access itself, since it can't auto-detect the parameter types. This approach is sometimes hit or miss, but worth a shot.
PARAMETERS [[Positions].[portfolio]] Long, [[Currency].[code]] Text ( 255 );
SELECT Sum(Abs([temp].[SumOfposition])) AS sumAbs
FROM [SELECT Currency.code, Sum(Positions.position) AS SumOfposition
FROM [Currency] INNER JOIN Positions ON Currency.ID = Positions.currency_id
WHERE (((Positions.portfolio)=1))
GROUP BY Currency.code
HAVING (((Currency.code) In ("A","B")))]. AS temp;
I have solved my problems thanks to the fact that the outer query is doing a trivial sum. When choosing New Database Query... in Excel, at the end of the process, after pressing Finish, an Import Data form pops up, asking
Where do you want to put the data?
you can click on Create a PivotTable report... . If you define the PivotTable properly, Excel will display only the outer sum.

Resources