I am trying to export the data from a Firebird store procedure using a loop. I am using Flamerobin tool to do this. I get the results but the columns and rows are all messed up when I export them to csv file! All headers will be in one cell and values are all over the place! Is there any way to export the result to excel or csv file?
set term!!;
EXECUTE BLOCK RETURNS (
SOD_AUTO_KEY Integer,
CURRENCY_CODE Char(3),
SO_CATEGORY_CODE Char(10),
SO_NUMBER Char(12),
INVC_NUMBER Char(12),
ENTRY_DATE Timestamp,
SHIP_DATE Timestamp,
NEXT_SHIP_DATE Timestamp,
CONDITION_CODE Varchar(10),
QTY_ORDERED Double precision,
QTY_PENDING_INVOICE Double precision,
QTY_INVOICED Double precision,
UNIT_PRICE Double precision,
EXCHANGE_RATE Double precision,
UNIT_COST Double precision,
ITEM_NUMBER Integer,
CONSIGNMENT_CODE Char(10),
NOTES Blob sub_type 1,
STOCK_LINE Integer,
STM_AUTO_KEY Integer,
SERIAL_NUMBER Varchar(40),
REMARKS Varchar(50),
PN Varchar(40),
PNM_AUTO_KEY Integer,
GR_CODE Varchar(10),
CUSTOMER_PRICE Double precision,
OPEN_FLAG Char(1),
ROUTE_CODE Char(1),
ROUTE_DESC Varchar(20),
COMPANY_CODE Varchar(10),
SITE_CODE Varchar(10),
COMPANY_NAME Varchar(50),
COMPANY_REF_NUMBER Varchar(30),
CUST_REF Varchar(15),
HOT_PART Char(1)
)
AS
declare i integer;
BEGIN
i=0;
while ( i <= 2 ) do
BEGIN
for SELECT SOD_AUTO_KEY,CURRENCY_CODE,SO_CATEGORY_CODE, SO_NUMBER,INVC_NUMBER,ENTRY_DATE, SHIP_DATE, NEXT_SHIP_DATE, CONDITION_CODE, QTY_ORDERED,QTY_PENDING_INVOICE, QTY_INVOICED, UNIT_PRICE, EXCHANGE_RATE, UNIT_COST,ITEM_NUMBER, CONSIGNMENT_CODE, NOTES, STOCK_LINE, STM_AUTO_KEY, SERIAL_NUMBER,REMARKS, PN, PNM_AUTO_KEY, GR_CODE, CUSTOMER_PRICE, OPEN_FLAG, ROUTE_CODE,ROUTE_DESC, COMPANY_CODE, SITE_CODE, COMPANY_NAME, COMPANY_REF_NUMBER, CUST_REF, HOT_PART
FROM SPB_SALESHISTORY (i)
into :SOD_AUTO_KEY, :CURRENCY_CODE, :SO_CATEGORY_CODE, :SO_NUMBER, :INVC_NUMBER,
:ENTRY_DATE, :SHIP_DATE, :NEXT_SHIP_DATE, :CONDITION_CODE, :QTY_ORDERED,:QTY_PENDING_INVOICE,
:QTY_INVOICED, :UNIT_PRICE, :EXCHANGE_RATE, :UNIT_COST, :ITEM_NUMBER, :CONSIGNMENT_CODE, :NOTES, :STOCK_LINE,
:STM_AUTO_KEY, :SERIAL_NUMBER, :REMARKS, :PN, :PNM_AUTO_KEY, :GR_CODE, :CUSTOMER_PRICE, :OPEN_FLAG, :ROUTE_CODE,:ROUTE_DESC,
:COMPANY_CODE, :SITE_CODE, :COMPANY_NAME, :COMPANY_REF_NUMBER, :CUST_REF,:HOT_PART
DO
suspend;
i = i + 1;
end
END!!
SET TERM;!!
I ended up using IBEXPERT according to Ryno's response.
I often use 2 SQL comands into FlameRobin SQL editor:
output filename_to_export.txt;
select field1, field2 from tablename;
First you say command what file you whant the result of your query will be stored, second you say the query itself. Do not forget, they are two separated (distinct) commands, note the ; after each one. The order doesn't matter.
The output remains opened for append results if you perform more queries.
Related
there is a problem with writing this function. I counted the number of works with the same name, but then I got stuck. The task itself sounds like this: Creating a function that generates a new cost of work. If the work has the same names, reduce their cost in proportion to their number
CREATE OR ALTER FUNCTION Новая_стоимость_работ
()
RETURNS #NewCoast TABLE(Код_работы INT,Наименование nvarchar(50),Стоимость_работы DECIMAL(18,2))
BEGIN
INSERT #NewCoast
SELECT Наименование, COUNT(*) AS Количество
FROM Работы
GROUP BY Наименование
HAVING COUNT(*) > 1
RETURN
END
Table
CREATE TABLE Работы
(Код_работы INT,
Наименование nvarchar(50) NOT NULL,
Стоимость_работы DECIMAL(18,2) NOT NULL,
CONSTRAINT PK2 PRIMARY KEY(Код_работы)
)
I have created a table and would like to import the data in my csv file over to pgAdmin. The following were the queries executed in the query editor :
create table offline_trx
(sale_key varchar(50),
order_id varchar(50),
country_code varchar(10),
qty_ordered integer,
qty_refunded integer,
extended_gmv_sgd DECIMAL(19,9),
extended_net_amount_sgd float DECIMAL(19,9),
product_sku varchar(50),
product_type varchar(20),
category varchar(20),
created_at_time_ist date
);
...and the below query to copy data from csv file ...
COPY public.offline_trx(
sale_key, order_id, country_code, qty_ordered, qty_refunded, extended_gmv_sgd, extended_net_amount_sgd, product_sku, product_type, category, created_at_time_ist)
FROM 'C:\Users\Sam\Downloads\offline_trx.csv'
DELIMITER ','
CSV HEADER;
After executing the above copy query, it showed that the query returned successfully. But when I select the table appears to be empty. So I'm not sure what am I missing. Is there a way that I can inspect the logs somewhere to better understand what exactly happened that resulted in it appearing to be "missing"?
I have a table valued function as below
CREATE FUNCTION ABELIBLE.TVFBOEGETSHIPMENTS (STARTDATE DATE, ENDDATE DATE, ADDRESSCODE CHAR(9))
RETURNS TABLE (ID INT, JOBNUMBER CHAR(9), CUSTOMERREFERENCE CHAR(18),
CONSIGNEENAME CHAR(30), CREATEDDATE DATE, AIRPORTOFORIGIN CHAR(3), AIRPORTOFARRIVAL CHAR(3),
AIRPORTOFDESTINATION CHAR(3), COUNTRYOFDESTINATION CHAR(3), ADDRESSCODE CHAR(9), CONSIGNMENTNUMBER CHAR(25))
LANGUAGE SQL
NOT DETERMINISTIC
READS SQL DATA
RETURN
SELECT
ROW_NUMBER() OVER(ORDER BY EMJOBN DESC),
A.EMJOBN,
A.EMCREF,
A.EMOSNM,
DATE(TIMESTAMP_FORMAT(DIGITS(A.EMCRTD), 'DDMMYY')),
A.EMAOFO,
A.EMAOFA,
A.EMAOFD,
A.EMCOFD,
A.EMUKCD,
A.EMRPRT
FROM DTALIBLE.EMASTER A WHERE A.EMPSFT = 'Y' AND A.EMUKCD = ADDRESSCODE AND
DATE(TIMESTAMP_FORMAT(DIGITS(A.EMCRTD), 'DDMMYY')) >= DATE(STARTDATE) AND DATE(TIMESTAMP_FORMAT(DIGITS(A.EMCRTD), 'DDMMYY')) <= DATE(ENDDATE)
However I am not getting any results back when I run the following query
SELECT * FROM TABLE(ABELIBLE.TVFBOEGETSHIPMENTS(DATE('06/06/2019'), DATE('06/07/2020'),'MUL0044')) AS XXX
I am using DBeaver to run the query on AS400.
For my own sanity I tired the select statement with literals and i got the following result
Then again when I tried with below dates it does not fetch any results and i get this error.
any help is much appreciated. Thank you in advance.
The only thing that jumps out at me is that in your invocation,
SELECT * FROM TABLE(ABELIBLE.TVFBOEGETSHIPMENTS(DATE('06/06/2019'), DATE('06/07/2020'),'MUL0044')) AS XXX
'MUL0044' as a literal is treated as varchar, and your UDTF has the parm defined as char.
That mismatch usually gives a Function not found error however. You can cast the literal to char or change the parm type to varchar. Which to chose depends on how it will normally be invoked. If you're just testing with a literal and the function would normally be passed a char then just cast the literal to char.
If you use the Run SQL Scripts component of IBM ACS, you'll have the ability to debug the function.
I want to save my adjusted query results into a table. For example I have codes 350.8, 351.94 and I have T-SQL code to remove the decimal points resulting in the adjusted results 350,351 etc. I want to save the results into a table, not Excel. Is this possible?
I see you can create a new table but with the same columns, not the new adjusted results. The below doesn't work as SQL Server doesn't recognise adjusted1, 2 and 3.
CREATE TABLE DiagAdj
(
encounter_id NUMERIC,
di_1 INT,
di_2 INT,
di_3 INT,
adjusted1 INT,
adjusted2 INT,
adjusted3 INT,
);
INSERT INTO DiagAdj (encounter_id, adjusted1, adjusted2, adjusted3)
SELECT encounter_id, adjusted1, adjusted2, adjusted3
FROM dbo.Encounters
Decimal places removed. I want to save down adjusted3 results into a table
SELECT
encounter_id, di_3, -- now, try to cast to int the remainder, ending right before the decimal
adjusted3 = TRY_CONVERT(int,LEFT(di_3, COALESCE(NULLIF(CHARINDEX('.', di_3) - 1, -1), 255)))
FROM
dbo.Encounters;
Why don't you just cast each decimal column to integer:
INSERT INTO DiagAdj (encounter_id, adjusted1, adjusted2, adjusted3)
SELECT
encounter_id,
CAST(diag1 AS DECIMAL(10,0)),
CAST(diag2 AS DECIMAL(10,0)),
CAST(diag3 AS DECIMAL(10,0))
FROM dbo.Encounters;
I am created an online staff rota application. I have a stored procedure for each day of the week with an auto increment primary key for the Rota table. Each day within the selected week will have the same rota ID as a foreign key.
I have no problem with the first selected day but the following days return NULL for the foreign key RotaID as I don't know how to pass the RotaID into the other stored procedures to ensure the RotaID remains the same for the 7 days of the weekly Rota.
CREATE PROCEDURE [usp_RotaDay1]
#Week_Begin datetime,
#Week_End datetime,
#1ShiftDate datetime,
#1Day nchar (10),
AS
BEGIN
SET NOCOUNT ON;
DECLARE #RotaID int
IF NOT EXISTS
(
SELECT * FROM Rota
WHERE Week_Begin = #Week_Begin
AND
Week_End = #Week_End
)
BEGIN
INSERT INTO Rota
(
[Week_Begin],
[Week_End]
)
VALUES
(
#Week_Begin,
#Week_End
)
END
SELECT #RotaID = SCOPE_IDENTITY()
IF NOT EXISTS
(
SELECT * FROM DayShift
WHERE ShiftDate = #1ShiftDate
)
BEGIN
INSERT INTO DayShift
(
[RotaID],
[ShiftDate],
[Day]
)
VALUES
(
#RotaID,
#1ShiftDate,
#1Day
)
END
SET NOCOUNT OFF;
END
As you can see I have the RotaID declared in the Rota table and then passed as the foreign key in the DayShift table.
I would like to know if it is possible to pass this through to my other stored procedures which are similar to this one.
I'm not sure exactly what you are trying to do, but if you want to return the #RotaID parameter value to the calling program for use in a future procedure call, you can specify the parameter as OUTPUT:
CREATE PROCEDURE [usp_RotaDay1]
(
#Week_Begin datetime,
#Week_End datetime,
#1ShiftDate datetime,
#1Day nchar (10),
#RotaID int OUTPUT
)
AS
...
In each of your secondary stored procedures, you need to retrieve the primary key id using a select statement. You know weekbegin and weekend for all your secondary procedures right? so you know which row to pull to retrieve the id. Hope I made sense.