Firebird 2.5 insert row to existing table - sql

I have table TABLE1 in a Firebird 2.5 database and want to insert multiple rows.
script:
INSERT INTO TABLE1 (ID, IDPREDEK, ICO, DIC, FIRMA, MISTO, ULICE, PSC, CISSML, CISEVID, PLATOD, PLATDO, JMENO, PRIJMENI, TITUL, FUNKCE, TELEFON, TELEFON2, FAX, EMAIL, ODP_JMENO, ODP_PRIJMENI, ODP_TITUL, ODP_FUNKCE, ODP_TELEFON, ODP_TELEFON2, ODP_FAX, ODP_EMAIL, D_INIDOP, D_INISETR, D_KATPRAC, POCETMUZI, POCETZENY, HASCHILD, HASCHILD1, HASCHILD2, POZNAMKA)
VALUES (91, 89, NULL, NULL, 'CLY0010702 - PHM-LPH_DEPO / PRG/RSM/FSB/PHM', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'N', 'N', 'N', NULL);
And problem is this error :
Error while importing to table TABLE1:
Engine Error (code = 335544665)
violation of PRIMARY or UNIGUE KEY constraint
"PK_TABLE1" on table "FIRMY".
Problematic key value is ("ID=95).
SQL Error (code= -803):
Invalid insert or update value(s): object columns are constrained - no 2 table rows can have duplicate column values.
In TABLE1 is last ID number 94, I don't have two same rows with ID 95.
Any ideas what to do?

Check is your ID identity. If so just leave it out of insert into() and values().

i found solution, replace ID :
... VALUES (GEN_ID(GEN_TABLE1 , 1), 91, NULL, NULL, ...

Related

Merging multiple rows into one row by Grouping and checking for non null

I have data in this format. I want to merge multiple rows into one-row grouping by ID column. each row will have only one non null value and one non null value for each column when grouped by ID.
[ID], [foo], [foo1], [foo2], [foo3], [foo4], [foo5]
1, data1, null, null, null, null, null
1, null, data2, null, null, null, null
1, null, null, data3, null, null, null
1, null, null, null, data4, null, null
1, null, null, null, null, data5, null
1, null, null, null, null, null, data6
2, data1, null, null, null, null, null
2, null, data2, null, null, null, null
2, null, null, data3, null, null, null
2, null, null, null, data4, null, null
2, null, null, null, null, data5, null
2, null, null, null, null, null, data6
Desired Output:
[ID], [foo], [foo1], [foo2], [foo3], [foo4], [foo5]
1, data1, data2, data3, data4, data5, data6
2, data1, data2, data3, data4, data5, data6
The aggregate functions max and min (as well as most others) will just ignore nulls. You could group by the ID and query the max of the other columns, which would return the single non-null value this column has:
SELECT id, MAX(foo), MAX(foo1), MAX(foo2), MAX(foo3), MAX(foo4), MAX(foo5)
FROM mytable
GROUP BY id
In your case, you can use max():
select id, max(foo) as foo, max(foo1) as foo1, . . .
from t
group by id;
I should note that your original data structure is often produced by a query that is a bit awry. Sometimes it is easier to fix the code that generates that result.

SQL multiply 2 columns 2 different tables

What i am trying to do is calculate diner_payment_due (DINER TABLE), by multiplying food_serve_cost (FOOD_SERVE TABLE) by food_serve_size (FS_DINER TABLE)
CREATE TABLE diner (
diner_no NUMBER(8) NOT NULL,
diner_payment_due NUMBER(6,2) NOT NULL,
diner_seat_no NUMBER(2) NOT NULL,
diner_seated DATE NOT NULL,
diner_completed DATE,
table_no NUMBER(2) NOT NULL
);
CREATE TABLE food_serve (
food_item_no NUMBER(4) NOT NULL,
food_serve_size CHAR(2) NOT NULL,
food_serve_kilojoules NUMBER(4) NOT NULL,
food_serve_cost NUMBER(5,2) NOT NULL
);
CREATE TABLE FS_DINER (
DINER_NO NUMBER(8) NOT NULL,
FOOD_ITEM_NO NUMBER(4) NOT NULL,
FOOD_SERVE_SIZE CHAR(2 BYTE) NOT NULL,
FS_DINER_NO_SERVES NUMBER(1) NOT NULL,
FS_DINER_ITEM_SERVED CHAR (1) NOT NULL,
CONSTRAINT pk_fs_diner PRIMARY KEY (DINER_NO, FOOD_ITEM_NO,FOOD_SERVE_SIZE)
);
I just need to return a single values and then insert into the diner table, i'm thinking that I need to do a Inner Join, then simply sum (food_serve_cost * NO_SERVES) then group by DINER_NO ?
INSERT INTO DINER ( DINER_NO, DINER_PAYMENT_DUE, DINER_SEAT_NO, DINER_SEATED, DINER_COMPLETED, TABLE_NO) INSERT INTO DINER VALUES (1, SELECT SUM (FOOD_SERVE.FOOD_SERVE_COST * FS_DINER..., , 1, 1/05/2017, 1 /05/2017, 1);
Am updating diner table based on the value from other two tables.
INSERT INTO DINER
(SELECT fs.diner_no,
(fs.food_serve_cost * fd.FOOD_SERVE_SIZE),
11,sysdate,sysdate,11 /*Am assuming, Due to Not null constraint, there will be error*/
FROM food_serve fs,
FS_DINER fd
WHERE fs.food_item_no = fd.food_item_no)

How to insert records using a SELECT statement?

Background
I currently have a table named Parts_list that is structured as shown below:
_id is autoincrementing so I always provide NULL which works fine.
I would like to use a SELECT statement to populate this table but have difficulties how to write the INSERT SELECT statement.
What I have tried
For testing I have tried this SQL statement:
INSERT INTO Parts_list VALUES
(null, 'My Name',CURRENT_TIMESTAMP, null, null, null, null,
null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null,
null, null, null,null, null, null)
and it inserts a new line without any problem.
Problem
However when I try something like this:
INSERT INTO Parts_list VALUES
(null, 'My Name',CURRENT_TIMESTAMP, Part_ID, null, null, null,
null, null, null,null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null, null, null,
null, null)
SELECT Part_ID FROM tracking_vehicles
where I am selecting one column and trying to insert this column, nothing happens. Do I have something wrong with my syntax? In my example "Part_ID" is VARCHAR, which matches the data type that it is being inserted to. Any help or guidance is appreciated.
PS I also tried this:
INSERT INTO Parts_list
(null, 'My Name',CURRENT_TIMESTAMP, Part_ID, null,
null, null, null, null, null,null, null, null, null,
null, null, null, null, null, null, null, null, null,
null, null, null,null, null, null)
SELECT Part_ID FROM tracking_vehicles
without the word "VALUES" as I saw many examples not use that, but it doesn't work. Even when I try my first example without "VALUES" it doesn't work either so I am guessing that I need that.
Using the example provided here I suggest you use the following syntax:
INSERT INTO Parts_list (_id, table_owner, etc)
SELECT null, 'My Name', etc
FROM tracking_vehicles
List out all your columns as I've started to and then pass through all the values in the SELECT.

I need to create a view in the database

I need to create a view in the database when two columns are refer from same table. I can create a view like this:
CREATE VIEW [dbo].[ViewJournal]
AS
SELECT
J. d, J.date, J.drac, L.name as draccount, J.crac,
L.name as craccount, J.dramt, J.cramt, J.lf,
J.description, J.voucherType, J.reg_date, J.last_update,
J.active
FROM
Journal J, Ledger L
WHERE
J.drac = L.Id
But the result doesn't not show actual result.
Here, crac and drac are refered to from Ledger table.
Journal table:
CREATE TABLE [dbo].[Journal]
(
[Id] DECIMAL (18) IDENTITY (1, 1) NOT NULL,
[date] DATETIME NULL,
[drac] DECIMAL (18) NULL,
[crac] DECIMAL (18) NULL,
[dramt] DECIMAL (18, 2) NULL,
[cramt] DECIMAL (18, 2) NULL,
[reg_date] DATETIME NULL,
[last_update] DATETIME NULL,
[active] INT NULL,
[lf] VARCHAR (50) NULL,
[description] NVARCHAR (150) NULL,
[voucherType] VARCHAR (50) NULL,
[sales] VARCHAR (50) NULL,
[purchase] VARCHAR (50) NULL,
[cash_paymentno] VARCHAR (50) NULL,
[cash_receiptno] VARCHAR (50) NULL,
[expense] VARCHAR (50) NULL,
[income] VARCHAR (50) NULL,
[advance] VARCHAR (50) NULL,
[remunaration] VARCHAR (50) NULL,
CONSTRAINT [PK_Journal] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Ledger] FOREIGN KEY ([drac]) REFERENCES [dbo].[Ledger] ([Id]) ON DELETE CASCADE ON UPDATE CASCADE
);
Ledger table:
CREATE TABLE [dbo].[Ledger]
(
[Id] DECIMAL (18) IDENTITY (1, 1) NOT NULL,
[name] NVARCHAR (50) NULL,
[type] NVARCHAR (50) NULL,
[classification] VARCHAR (50) NULL,
[realornominal] VARCHAR (50) NULL,
[reg_date] DATETIME NULL,
[last_update] DATETIME NULL,
[active] DECIMAL (2) NULL,
[depree] VARCHAR (50) NULL,
CONSTRAINT [PK_Ledger] PRIMARY KEY CLUSTERED ([Id] ASC)
);
In your current query, you're joining on J.drac = L.Id, which means that L will always be the record which is referenced in J.drac, regardless of the value of J.crac.
The way I understand it, you want to reference two different records in the Ledger table. You need two joins for that.
SELECT
J.Id, J.date, J.drac, D.name as draccount, J.crac,
C.name as craccount, J.dramt, J.cramt, J.lf,
J.description, J.voucherType, J.reg_date, J.last_update,
J.active
FROM Journal J
INNER JOIN Ledger D ON J.drac = D.Id
INNER JOIN Ledger C ON J.crac = C.Id

Update multiple column from another table using sql server 2005

I have one master table named table1 where i store or update mobileNo data daily for a month.
;WITH table1 AS (SELECT * FROM (VALUES
(9999999999, '01/10/2013', NULL, NULL, NULL, NULL),
(9999999999, NULL, '02/10/2013', NULL, NULL, NULL),
(9999999999, NULL, NULL, '03/10/2013', NULL, NULL),
(9999999999, NULL, NULL, NULL, '04/10/2013', NULL),
(9999999999, NULL, NULL, NULL, NULL, '30/10/2013'),
(9999999999, NULL, NULL, NULL, NULL, NULL),
(8888888888, '01/10/2013', NULL, NULL, NULL, NULL),
(8888888888, NULL, '02/10/2013', NULL, NULL, NULL),
(8888888888, NULL, NULL, '03/10/2013', NULL, NULL),
(8888888888, NULL, NULL, NULL, '04/10/2013', NULL),
(8888888888, NULL, NULL, NULL, NULL, '30/10/2013'))
as t(mobileno,date1,date2,date3,date4,date30))
And i have another table named table2 where i keep unique mobileNo against table1. Now i want to update table2 against table1 if any data exists in table1.
mobileno date1 date2 date3 date4 date30
--------------- ---------- ---------- ---------- ---------- ----------
8888888888 01/10/2013 02/10/2013 03/10/2013 04/10/2013 30/10/2013
9999999999 01/10/2013 02/10/2013 03/10/2013 04/10/2013 30/10/2013
However i tried the query like this
UPDATE table1
set table1.date1 =
(SELECT date1 from table2 where table2.mobileno = table1.mobileno)
Where table2.mobileno = table1.mobileno
How do i update in a single query without repeating to update the 30 nos. of date columns, please help me. Thanks in advance.
Perhaps NULL values from TABLE2 can be avoided to UPDATE NOT NULL values in TABLE1 using following UPDATE statement.
Only ISNULL function is added to previous post
update table1
set
date1 = ISNULL(t2.date1, date1),
date2 = ISNULL(t2.date2, date2)
from table2 t2
where table1.mobileno = t2.mobileno