Stored procedure not working as i want - mysql

I am trying to make a stored procedure like this
BEGIN
SELECT concat(stationsNavn,' ',city,' ',adresse,' ',postnummer)as navn,id
from AdresseListeBioAnlæg order by stationsNavn,city
inner join bioAlarmer on AdresseListeBioAnlæg.id = bioAlarmer.station;
end
With "as navn,id" i can create it, but i dont get anything, and with "order by stationsNavn,city" i get an error on create. It works fine without any of them like this.
BEGIN
SELECT concat(stationsNavn,' ',city,' ',adresse,' ',postnummer)
from AdresseListeBioAnlæg
inner join bioAlarmer on AdresseListeBioAnlæg.id = bioAlarmer.station;
end
What am i doing wrong?

try this out, I moved the order by and did it by navn and id
BEGIN
SELECT concat(stationsNavn,' ',city,' ',adresse,' ',postnummer)as navn,id
from AdresseListeBioAnlæg
inner join bioAlarmer on AdresseListeBioAnlæg.id = bioAlarmer.station
order by navn, id
end

Related

While loop for selecting field names?

I have a nasty, nasty data layout that I am forced to work with. I finally got a working query using C# and a for loop executing the same query over and over but adjusting which fields are called, but now I am wondering if it is possible to do it with a while loop. I am getting an error, and I am not sure if it is because I am using Faircom / C-tree as the database, or if there is something wrong with my query. I am normally a Mysql user.
the table has 20 fields I care about and want to extract into a csv list. They are codetype1-codetype20 and I want it to be something like value1, value2, value3... where as it is now I get them all back one at a time. Trouble is that codetype1 is dependent on another field to determine where I go look for the info on that code, which is why the case statements.
DROP PROCEDURE IF EXISTS proc_loop_test;
CREATE PROCEDURE proc_loop_test()
SET #index = 1;
WHILE(#index < 21) DO
SELECT Replace(Concat(To_char(apptid), To_char(.#index) ), ' ', '') AS reference_id,
apptid AS a_reference_id,
CASE
WHEN c.ee > 0 THEN d.amt
ELSE insfee.amt
END AS amount,
CASE
WHEN c.ee > 0 THEN Rtrim(e.moneyname)
ELSE insname.namefeecatid
END AS moneyschedule_name,
CASE codetype#index
WHEN 1 THEN rtrim(a.descript)
ELSE rtrim(b.descript0)
END AS description,
CASE codetype#index
WHEN 1 THEN rtrim(a.abbrevdescript)
ELSE rtrim(b.abbrev0)
END AS abbreviated_description,
CASE codetype#index
WHEN 1 THEN rtrim(a.thiscode)
ELSE rtrim(b.thiscode0)
END AS code
FROM meetings
LEFT JOIN
(
SELECT admin.table2.procid,
admin.table2.this_code_id,
admin.v_table1.descript,
admin.v_table1.abbrevdescript,
admin.v_table1.thiscode
FROM admin.table2
INNER JOIN admin.v_table1
ON admin.table2.this_code_id = admin.v_table1.this_code_id) AS a
ON meetings.codeid#index = a.procid
LEFT JOIN
(
SELECT admin.v_table1.descript AS descript0,
admin.v_table1.abbrevdescript AS abbrev0,
admin.v_table1.thiscode AS thiscode0,
admin.v_table1.this_code_id
FROM admin.v_table1) AS b
ON meetings.codeid#index = b.this_code_id
LEFT JOIN
(
SELECT patid AS id,
ee
FROM admin.customer) AS c
ON meetings.patid = c.id
LEFT JOIN
(
SELECT this_code_id AS redid,
eecategoryid,
amt
FROM admin.eeule) AS d
ON c.ee = d.eecategoryid
AND d.redid = b.this_code_id
LEFT JOIN
(
SELECT eecategoryid AS namefeecatid,
moneyname
FROM admin.eeulenames) AS e
ON d.eecategoryid = e.namefeecatid
LEFT JOIN (SELECT pi.customer_id,
pi.primarykk_id AS picid,
pi.primarykk_name,
pi.first_name,
pi.last_name,
i.groupname,
i.ee
FROM admin.v_pir AS pi
LEFT JOIN admin.money AS i
ON pi.primarykk_id = i.insid) AS
ins
ON ins.customer_id = c.id
LEFT JOIN (SELECT this_code_id AS redid,
eecategoryid,
amt
FROM admin.eeule) AS insfee
ON ins.ee = insfee.eecategoryid
AND insfee.redid = b.this_code_id
LEFT JOIN (SELECT eecategoryid AS namefeecatid,
moneyname
FROM admin.eeulenames) AS insname
ON insfee.eecategoryid = insname.namefeecatid
WHERE codeid#index >= 1
END WHILE;
END;
I have never used a while loop, and while I understand somewhat I am supposed to select this to go INTO something, do I need to create a temp table, or can it all just be stored in memory till the end of the loop and returned.
For what it is worth, the entire SELECT query works in C# when you replace the #index with concatenating format " . index . "
Based on the information that you have provided, it is strongly suggested that you reach out to your vendor for this. You're attempting to create a stored procedure, however, using a mySQL proprietary syntax. Stored procedure support is unique to each database vendor. FairCom's c-treeACE SQL actually uses Java for cross platform support and .NET for Windows.
https://docs.faircom.com/doc/jspt/#cover.htm
Stored procedure development requires a strong knowledge of the database layout which is highly application dependent. In this case, many legacy application dependencies may be involved. Again, your best source of information will be your application vendor.

Is this a t sql stored proceedure

I want to execute this from within Excel (SQL query window) where I would normally only execute the select statement. Should I make it into a stored proceedure and then execute tat from within Excel?
BEGIN TRY
drop table GlobalShop.dbo.v_order_hist_dtl_Quote
END TRY
BEGIN CATCH
END CATCH
BEGIN TRY
drop table GlobalShop.dbo.v_order_lines_Quote
END TRY
BEGIN CATCH
END CATCH
select * into GlobalShop.dbo.v_order_hist_dtl_Quote
from GlobalShop.dbo.v_order_hist_dtl where isnull(USER_3,'') <> ''
select * into GlobalShop.dbo.v_order_lines_Quote
from GlobalShop.dbo.v_order_lines where isnull(USER_3,'') <> ''
/* Quotes with Orders 1.0 */
select
QT.QuoteNum,
QT.custID,
QT.QCustNum,
QT.QCustName,
QT.QDate,
COUNT(OL.order_no) as #_Open_Orders,
COUNT(OH.order_no) as #_Closed_Orders
from
"Track Quotes".dbo.TrackQuotesHist as QT
left outer join GlobalShop.dbo.v_order_hist_dtl_quote as OH
on QT.QuoteNum = OH.USER_3
left outer join GlobalShop.dbo.v_order_lines_quote as OL
on QT.QuoteNum = OL.USER_3
where
QT.QDate between '03/01/2015' and '03/06/2015'
Group By
QT.QuoteNum,
QT.custID,
QT.QCustNum,
QT.QCustName,
QT.QDate
If you only want to run the select statements most of the time I would make the drops into one stored procedure (if you ever want to do it) and then the select into another.
In my world I assume an object that starts with v_ is a view but you seem to treat it as a table and if it is change it to something like this:
create proc dbo.procfiles
as
BEGIN
if object_id('GlobalShop.dbo.v_order_hist_dtl_Quote','U') is not null
drop table GlobalShop.dbo.v_order_hist_dtl_Quote
if object_id('GlobalShop.dbo.v_order_lines_Quote','U') is not null
drop table GlobalShop.dbo.v_order_lines_Quote
select * into GlobalShop.dbo.v_order_hist_dtl_Quote
from GlobalShop.dbo.v_order_hist_dtl where user_3 is not null
select * into GlobalShop.dbo.v_order_lines_Quote
from GlobalShop.dbo.v_order_lines where user_3 is not null
/* Quotes with Orders 1.0 */
select
QT.QuoteNum,
QT.custID,
QT.QCustNum,
QT.QCustName,
QT.QDate,
COUNT(OL.order_no) as #_Open_Orders,
COUNT(OH.order_no) as #_Closed_Orders
from
"Track Quotes".dbo.TrackQuotesHist as QT
left outer join GlobalShop.dbo.v_order_hist_dtl_quote as OH
on QT.QuoteNum = OH.USER_3
left outer join GlobalShop.dbo.v_order_lines_quote as OL
on QT.QuoteNum = OL.USER_3
where
QT.QDate between '03/01/2015' and '03/06/2015'
Group By
QT.QuoteNum,
QT.custID,
QT.QCustNum,
QT.QCustName,
QT.QDate
begin catch
select
ERROR_NUMBER(),
ERROR_SEVERITY(),
ERROR_STATE(),
ERROR_PROCEDURE(),
ERROR_LINE(),
ERROR_MESSAGE()
end catch
end
Unless you have a very compelling reason to update those tables every time this query is run, I would avoid them. Just use equivalent query without the use of the tables:
select
QT.QuoteNum,
QT.custID,
QT.QCustNum,
QT.QCustName,
QT.QDate,
COUNT(OL.order_no) as #_Open_Orders,
COUNT(OH.order_no) as #_Closed_Orders
from "Track Quotes".dbo.TrackQuotesHist as QT
left outer join GlGlobalShop.dbo.v_order_hist_dtl OH
on QT.QuoteNum = OH.USER_3
and isnull(QT.QuoteNum,'')<>''
left outer join GlobalShop.dbo.v_order_lines OL
on QT.QuoteNum = OL.USER_3
and isnull(QT.QuoteNum,'')<>''
where QT.QDate between '03/01/2015' and '03/06/2015'
Group By
QT.QuoteNum,
QT.custID,
QT.QCustNum,
QT.QCustName,
QT.QDate

SET invalid use of group function using AVG

I want to declare a variable called average, what should be the average of the completiondate-creationdate of the items in the todolist.
however, if I use these lines of code, mysql tells me that I have an error in my syntax.
Can somebody tell me what I should change to store the correct value in #average?
SET #average = AVG(todoitem.completiondate-todoitem.creationdate) from todolist
right join todoitem on todoitem.id=todolist.id
;
SELECT name FROM todolist
right join todoitem on todoitem.id=todolist.id
WHERE (todoitem.completiondate-todoitem.creationdate > average)
;
You are missing a select in the first query:
select #average := AVG(todoitem.completiondate - todoitem.creationdate)
from todolist right join
todoitem
on todoitem.id = todolist.id;
Although I'm leaving it in, the join seems unnecessary. Why are you doing the join if you are using only one table?
select #average := AVG(todoitem.completiondate - todoitem.creationdate)
from todoitem;
You have to add SELECT:
SET #average = (SELECT AVG(todoitem.completiondate-todoitem.creationdate))
from todolist
right join todoitem on todoitem.id=todolist.id
-- or
SELECT AVG(todoitem.completiondate-todoitem.creationdate) INTO #average
from todolist
right join todoitem on todoitem.id=todolist.id

The shared Dataset definition is not valid. The report definition element 'Dataset' is empty

SELECT PRODDTA.F4006.OADOCO, PRODDTA.F4006.OADCTO, PRODDTA.F4006.OAKCOO, PRODDTA.F4006.OAANTY, PRODDTA.F4006.OAMLNM, PRODDTA.F4006.OAADD1, PRODDTA.F4006.OAADD2, PRODDTA.F4006.OAADD3, PRODDTA.F4006.OAADD4, PRODDTA.F4006.OAADDZ, PRODDTA.F4006.OACTY1, PRODDTA.F4006.OACOUN, PRODDTA.F4006.OAADDS, PRODDTA.F4006.OACRTE, PRODDTA.F4006.OABKML, PRODDTA.F4006.OACTR, PRODDTA.F4006.OAUSER, PRODDTA.F4006.OAPID, PRODDTA.F4006.OAUPMJ, PRODDTA.F4006.OAJOBN, PRODDTA.F4006.OAUPMT, PRODDTA.F4006.OALNID
INTO #BAF4006ShipTo
FROM PRODDTA.F4006
WHERE (PRODDTA.F4006.OAANTY='2');
SELECT PRODDTA.F4211.SDKCOO, PRODDTA.F4211.SDDOCO, PRODDTA.F4211.SDDCTO, PRODDTA.F4211.SDAN8, PRODDTA.F4211.SDSHAN, PRODDTA.F4211.SDDOC, PRODDTA.F4211.SDDCT, PRODDTA.F4211.SDNXTR
INTO #BAFSumF4211byInvoice
FROM PRODDTA.F4211
GROUP BY PRODDTA.F4211.SDKCOO, PRODDTA.F4211.SDDOCO, PRODDTA.F4211.SDDCTO, PRODDTA.F4211.SDAN8, PRODDTA.F4211.SDSHAN, PRODDTA.F4211.SDDOC, PRODDTA.F4211.SDDCT, PRODDTA.F4211.SDNXTR
HAVING ((PRODDTA.F4211.SDKCOO='00035') AND (PRODDTA.F4211.SDNXTR='999'));
SELECT PRODDTA.F0018.TDCO, PRODDTA.F0018.TDDCTO, PRODDTA.F0018.TDDOCO, PRODDTA.F0018.TDAN8, PRODDTA.F0101.ABALPH, PRODDTA.F0116.ALADDS, (PRODDTA.F0018.TDAEXP/100) AS TDAEXP, (PRODDTA.F0018.TDATXN/100) AS TDATXN, (PRODDTA.F0018.TDATXA/100) AS TDATXA, (PRODDTA.F0018.TDSTAM/100) AS TDSTAM, PRODDTA.F0018.TDTXA1, PRODDTA.F0018.TDEXR1, PRODDTA.F0018.TDDGL, PRODDTA.F0018.TDDSVJ
INTO #BAFTaxQuery
FROM (PRODDTA.F0018 INNER JOIN PRODDTA.F0101 ON PRODDTA.F0018.TDAN8 = PRODDTA.F0101.ABAN8) INNER JOIN PRODDTA.F0116 ON PRODDTA.F0018.TDAN8 = PRODDTA.F0116.ALAN8
WHERE ((PRODDTA.F0018.TDCO='00035') AND (PRODDTA.F0018.TDDCTO='RI') AND (PRODDTA.F0018.TDDGL>114182 And PRODDTA.F0018.TDDGL<=114273));
SELECT #BAFTaxQuery.TDCO, #BAFTaxQuery.TDDCTO, #BAFTaxQuery.TDDOCO, #BAFSumF4211byInvoice.SDDOCO, #BAFSumF4211byInvoice.SDDCTO, #BAFTaxQuery.TDAN8, #BAFTaxQuery.ABALPH, #BAFTaxQuery.ALADDS, #BAFSumF4211byInvoice.SDSHAN, #BAFTaxQuery.TDAEXP, #BAFTaxQuery.TDATXN, #BAFTaxQuery.TDATXA, #BAFTaxQuery.TDSTAM, #BAFTaxQuery.TDTXA1, #BAFTaxQuery.TDEXR1, #BAFTaxQuery.TDDGL, #BAFTaxQuery.TDDSVJ
INTO #BAFTaxQuery2
FROM #BAFTaxQuery LEFT JOIN #BAFSumF4211byInvoice ON (#BAFTaxQuery.TDDOCO = #BAFSumF4211byInvoice.SDDOC) AND (#BAFTaxQuery.TDDCTO = #BAFSumF4211byInvoice.SDDCT);
SELECT #BAFTaxQuery2.TDCO, #BAFTaxQuery2.TDDCTO, #BAFTaxQuery2.TDDOCO, #BAFTaxQuery2.SDDOCO, #BAFTaxQuery2.SDDCTO, #BAFTaxQuery2.TDAN8, #BAFTaxQuery2.ABALPH as BAFTaxQuery2_ABALPH, #BAFTaxQuery2.ALADDS as BAFTaxQuery2_ALADDS, #BAFTaxQuery2.SDSHAN,PRODDTA.F0101.ABALPH as BAFTaxQuery3_ABALPH, PRODDTA.F0116.ALADDS as BAFTaxQuery3_ALADDS, #BAFTaxQuery2.TDAEXP, #BAFTaxQuery2.TDATXN, #BAFTaxQuery2.TDATXA, #BAFTaxQuery2.TDSTAM, #BAFTaxQuery2.TDTXA1, #BAFTaxQuery2.TDEXR1, #BAFTaxQuery2.TDDGL, #BAFTaxQuery2.TDDSVJ
INTO #BAFTaxQuery3
FROM #BAFTaxQuery2 INNER JOIN PRODDTA.F0101 ON #BAFTaxQuery2.SDSHAN = PRODDTA.F0101.ABAN8 INNER JOIN PRODDTA.F0116 ON #BAFTaxQuery2.SDSHAN = PRODDTA.F0116.ALAN8;
SELECT #BAFTaxQuery3.TDCO, #BAFTaxQuery3.TDDCTO, #BAFTaxQuery3.TDDOCO, #BAFTaxQuery3.SDDOCO, #BAFTaxQuery3.SDDCTO, #BAFTaxQuery3.TDAN8, #BAFTaxQuery3.BAFTaxQuery2_ABALPH, #BAFTaxQuery3.BAFTaxQuery2_ALADDS, #BAFTaxQuery3.SDSHAN, #BAFTaxQuery3.BAFTaxQuery3_ABALPH, #BAFTaxQuery3.BAFTaxQuery3_ALADDS,#BAFTaxQuery3.TDAEXP, #BAFTaxQuery3.TDATXN, #BAFTaxQuery3.TDATXA, #BAFTaxQuery3.TDSTAM, #BAFTaxQuery3.TDTXA1, #BAFTaxQuery3.TDEXR1, #BAFTaxQuery3.TDDGL, #BAFTaxQuery3.TDDSVJ, #BAF4006ShipTo.OAMLNM, #BAF4006ShipTo.OACTY1, #BAF4006ShipTo.OAADDS
INTO #BAFTaxReportQuery
FROM #BAFTaxQuery3 LEFT JOIN #BAF4006ShipTo ON (#BAFTaxQuery3.SDDCTO = #BAF4006ShipTo.OADCTO) AND (#BAFTaxQuery3.SDDOCO = #BAF4006ShipTo.OADOCO) AND (#BAFTaxQuery3.TDCO = #BAF4006ShipTo.OAKCOO);
DROP TABLE #BAF4006ShipTo
DROP TABLE #BAFTaxQuery
DROP TABLE #BAFTaxQuery2
DROP TABLE #BAFSumF4211byInvoice
DROP TABLE #BAFTaxQuery3
DROP TABLE #BAFTaxReportQuery
The Query runs fine in Sql Server.
I create shared dataset as 'Text' and I'm able to create successfully.
But in the report when I Add Dataset , there are no fields showing.
I tried adding it manually too but get this error. "THe shared Dataset definition is not valid. The report definition element 'Dataset' is empty at line 43,position 5. It is missing a mandatory chid elemnt of type 'Fields.'"

Operation with values in query inside stored procedure (mysql)

i have a stored procedure in mysql with a couple of queries and i need to perform some operations with that query.
This is some the code from the stored procedure:
BEGIN
SET ##session.collation_connection = ##global.collation_connection;
DROP TEMPORARY TABLE IF EXISTS innerContainers;
CREATE TEMPORARY TABLE `innerContainers` (
`id_container` INT(10) NOT NULL,
`display_name` VARCHAR(100) NOT NULL,
PRIMARY KEY (`id_container`)
)
ENGINE = memory;
INSERT INTO innerContainers(id_container, display_name)
(SELECT c1.id_container, c1.display_name
FROM container_presentation cp
LEFT JOIN presentation p USING(id_presentation)
LEFT JOIN container c1 ON p.id_container = c1.id_container
WHERE c1.isDeleted = 0 AND c1.isActive = 1 AND
cp.id_container = in_id_container)
UNION
(SELECT c1.id_container, c1.display_name
FROM container_assembly_item cp
LEFT JOIN presentation p USING(id_presentation)
LEFT JOIN container c1 ON p.id_container = c1.id_container
WHERE c1.isDeleted = 0 AND c1.isActive = 1 AND
cp.id_container = in_id_container);
SELECT mad.id_container,
mat.sign_stock,
ma.id_management_start_point,
ma.id_management_end_point,
mad.quantity
FROM management_activity ma
LEFT JOIN management_activity_type mat ON ma.id_management_activity_type = mat.id_management_activity_type
LEFT JOIN management_activity_detail mad ON ma.id_management_activity = mad.id_management_activity
LEFT JOIN management_stock_point msp ON ma.id_management_end_point = msp.id_management_stock_point
LEFT JOIN management_stock_point msp1 ON ma.id_management_start_point = msp1.id_management_stock_point
WHERE mad.id_container IN (SELECT id_container FROM innerContainers)
ORDER BY mad.id_container ASC;
END
Now, after the last query.. i need to do some operations and return a value for each id_container inside the temporary table depending on the values in the second query.
Something like this:
foreach id_container in the second query i have a resultValue and i need to:
if the sign_stock == 1 and some other conditions then resultValue -= quantity and if sign_stock == 2 and some other conditions then resultValue += quantity.
And the final resultValue after iterating over the id_container lines will be the one i want for that id_container in the temporary table.
I dont know how to do that operation.. can some one help me with that?
Don't create a temporary table unless you need the data after the procedure call. Either way, in order to iterate over the results of a SELECT query, use a CURSOR.
A simple example is provided in the linked manual page.