Well, I was watching a video tutorial on youtube about making a program with c# and using a database. The guy was using SQL commands while I was using MySQL through phpmyadmin (XAMPP is love).
I've managed to keep going until I reached one part where the guy was creating stored procedures and input this code:
create procedure datosfactura #NumFac int
as
select
F.*, D.PrecioVen, D.CanVe, C.Nom_cli, A.Nom_pro, D.PrecioVen * D.CanVe as importe
from
Facturas F inner join Detalles D on F.NunFac = D.NumFac
inner join Articula A on D.CodPro = A.id_pro
inner join Cliente C on F.CodCli = C.id_clientes
where F.NumFac = #NumFac
I racket my brain and tried different ways but I can figure out what I'm doing wrong.
As it is, if I make a new MySQL command inputting that query it returns that there's an error in the first line.
What is the right way to convert this SQL query into MySQL? Thank you for your help.
Try this:
delimiter //
create procedure datosfactura(NumFac int)
begin
select F.*,
D.PrecioVen,
D.CanVe,
C.Nom_cli,
A.Nom_pro,
D.PrecioVen * D.CanVe as importe
from Facturas F
inner join Detalles D on F.NunFac = D.NumFac
inner join Articula A on D.CodPro = A.id_pro
inner join Cliente C on F.CodCli = C.id_clientes
where F.NumFac = NumFac;
end//
delimiter ;
Useful articles:
http://www.mysqltutorial.org/getting-started-with-mysql-stored-procedures.aspx
https://dev.mysql.com/doc/refman/8.0/en/create-procedure.html
Maybe something like this (i dont have access to mysql now)
delimiter $$
create function datosfactura(IN NumFac int)
BEGIN
select
F.*, D.PrecioVen, D.CanVe, C.Nom_cli, A.Nom_pro, D.PrecioVen * D.CanVe as
importe
from
Facturas F inner join Detalles D on F.NunFac = D.NumFac
inner join Articula A on D.CodPro = A.id_pro
inner join Cliente C on F.CodCli = C.id_clientes
where F.NumFac = NumFac
end $$
Related
I am trying to run this query between two linked servers, but it is returning the message ORA-00903 Invalid table.
--Code
SELECT * FROM OPENQUERY ([Database1],
'SELECT ''Simphony_ARCS-Operational'', ''Mar'', ''03'', ''2020'', ''DayPlaceholder''AS DAY,
SUM((MENU_ITEM_DAY_PART_TOTAL.SALESTOTAL) - (MENU_ITEM_DAY_PART_TOTAL.DISCOUNTTOTAL)) AS NETSALESTOTAL,
MENU_ITEM_DAY_PART_TOTAL.ORGANIZATIONID,
CONCAT(MENU_ITEM_DAY_PART_TOTAL.REVENUECENTERID, ''_REV'') AS REV_ID,
UPPER(CASE WHEN DAY_PART.NAME IS NULL THEN ''DINNER''
ELSE DAY_PART.NAME END) AS DAYPART,
COALESCE(sc.stringtext, scc.stringtext) as MenuItemClassName,
sm.stringtext as MenuItemName,
su.stringtext as ZoneORLocation
FROM
--LOCATION_ACTIVITY_DB.MAJOR_GROUP
[10.xxx.1.xxx].[Database2].[TRANS].menu_item_definition d
left join [10.xxx.1.xxx].[SimphonyTables].[TRANS].string_table sm
on d.name1id = sm.stringnumberid
left join [10.xxx.1.xxx].[SimphonyTables].[TRANS].menu_item_master m
on d.menuitemmasterid = m.menuitemmasterid
left join [10.xxx.1.xxx].[SimphonyTables].[TRANS].hierarchy_structure s
on d.hierstrucid = s.hierstrucid
left join [10.xxx.1.xxx].[SimphonyTables].[TRANS].hierarchy_unit u
on s.hierunitid = u.hierunitid
left join [10.xxx.1.xxx].[SimphonyTables].[TRANS].string_table su
on u.nameid = su.stringnumberid
left join [10.xxx.1.xxx].[SimphonyTables].[TRANS].menu_item_class c
on d.hierstrucid = c.hierstrucid and d.menuitemclassobjnum = c.objectnumber
left join [10.xxx.1.xxx].[SimphonyTables].[TRANS].menu_item_class cc
on s.parenthierstrucid = cc.hierstrucid and d.menuitemclassobjnum = cc.objectnumber
left join [10.xxx.1.xxx].[SimphonyTables].[TRANS].string_table sc
on c.nameid = sc.stringnumberid
left join [10.xxx.1.xxx].[SimphonyTables].[TRANS].string_table scc
on cc.nameid = scc.stringnumberid
left join [10.xxx.1.xxx].[SimphonyTables].[TRANS].sales_itemizer i
on c.hierstrucid = i.hierstrucid and c.slsitmzrindex = i.slsitmzrindex
left join [10.xxx.1.xxx].[SimphonyTables].[TRANS].sales_itemizer ii
on cc.hierstrucid = ii.hierstrucid and cc.slsitmzrindex = ii.slsitmzrindex
left join [10.xxx.1.xxx].[SimphonyTables].[TRANS].sales_itemizer pi
on s.parenthierstrucid = pi.hierstrucid and c.slsitmzrindex = pi.slsitmzrindex
left join [10.xxx.1.xxx].[SimphonyTables].[TRANS].string_table si
on i.nameid = si.stringnumberid
left join [10.xxx.1.xxx].[SimphonyTables].[TRANS].string_table spi
on pi.nameid = spi.stringnumberid
left join [10.xxx.1.xxx].[SimphonyTables].[TRANS].string_table sii
on ii.nameid = sii.stringnumberid
inner join LOCATION_ACTIVITY_DB.REVENUE_CENTER rc
on rc.revenuecenterposref = u.objectnumber
inner join LOCATION_ACTIVITY_DB.MENU_ITEM mi
on m.objectnumber = mi.MENUITEMPOSREF
right outer join LOCATION_ACTIVITY_DB.MENU_ITEM_DAY_PART_TOTAL midpt
on mi.menuitemid = midpt.menuitemid
left outer join LOCATION_ACTIVITY_DB.DAY_PART dp
on midpt.organizationid = dp.organizationid and midpt.daypartid = dp.daypartid
AND MENU_ITEM_DAY_PART_TOTAL.DAYPARTID = DAY_PART.DAYPARTID
WHERE (midpt.ORGANIZATIONID = ''10260'')
AND BUSINESSDATE BETWEEN (TO_DATE(''01-Mar-20'',''DD-MON-YY'')) AND (TO_DATE(''31-Mar-20'',''DD-MON-YY''))
AND mi.MENUITEMPOSREF = ''420000010''
AND rc.REVENUECENTERPOSREF LIKE ''304%''
GROUP BY midpt.ORGANIZATIONID
,COALESCE(sc.stringtext, scc.stringtext)
,sm.stringtext
,midpt.REVENUECENTERID
,dp.NAME
,su.stringtext')
Error Message...
OLE DB provider "OraOLEDB.Oracle" for linked server "FDMEE_SIMPHONY" returned message "ORA-00903: invalid table name".
Msg 7321, Level 16, State 2, Line 1
An error occurred while preparing the query "SELECT 'Simphony_ARCS-Operational', 'Mar', '03', '2020', 'DayPlaceholder'AS DAY,
SUM((MENU_ITEM_DAY_PART_TOTAL.SALESTOTAL) - (MENU_ITEM_DAY_PART_TOTAL.DISCOUNTTOTAL)) AS NETSALESTOTAL,
MENU_ITEM_DAY_PART_TOTAL.ORGANIZATIONID,
CONCAT(MENU_ITEM_DAY_PART_TOTAL.REVENUECENTERID, '_REV') AS REV_ID,
UPPER(CASE WHEN DAY_PART.NAME IS NULL THEN 'DINNER'
ELSE DAY_PART.NAME END) AS DAYPART,
COALESCE(sc.stringtext, scc.stringtext) as MenuItemClassName,
sm.stringtext as MenuItemName,
su.stringtext as ZoneORLocation
FROM
--LOCATION_ACTIVITY_DB.MAJOR_GROUP
[10.118.1.135].[SimphonyTables].[TRANS].menu_item_definition d
left join [10.118.1.135].[SimphonyTables].[TRANS].string_table sm
on d.name1id = sm.stringnumberid
left join [10.118.1.135].[SimphonyTables].[TRANS].menu_item_master m
on d.menuitemmasterid = m.menuitemmasterid
left join [10.118.1.135].[SimphonyTables].[TRANS].hierarchy_structure s
on d.hierstrucid = s.hierstrucid
left join [10.118.1.135].[SimphonyTables].[TRANS].hierarchy_unit u
on s.hierunitid = u.hierunitid
left join [10.118.1.135].[SimphonyTables].[TRANS].string_table su
on u.nameid = su.stringnumberid
left join [10.118.1.135].[SimphonyTables].[TRANS].menu_item_class c
on d.hierstrucid = c.hierstrucid and d.menuitemclassobjnum = c.objectnumber
left join [10.118.1.135].[SimphonyTables].[TRANS].menu_item_class cc
on s.parenthierstrucid = cc.hierstrucid and d.menuitemclassobjnum = cc.objectnumber
left join [10.118.1.135].[SimphonyTables].[TRANS].string_table sc
on c.nameid = sc.stringnumberid
left join [10.118.1.135].[SimphonyTables].[TR...
I suspect the problem is your square brackets (MSSQL-specific syntax; invalid for Oracle).
Take look at this link:
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/610dfd96-f9b3-41ee-948c-d5763599397e/oracle-linked-server-ole-db-provider-invalid-table-name-or-table-or-view-does-not-exist?forum=sqlintegrationservices
Oracle Linked Server Ole DB Provider invalid table name or table or view does not exist
Hi guys, I am having a big problem tying to connect Oracle by SSIS
using the OLE DB Provider for Oracle
Data Flow, Source Oracle_Connection, I fixed the AlwaysUseDefaultCode
as yes and I retrieved the table "[Oracle]"."[Tax]" (the apexes came
in automatic). I checked the mapping and created the Destination
Assistant as a SQL table. IT did works.
Data Flow, Source Oracle_Connection, I fixed the AlwaysUseDefaultCode
as yes and...I used the SQL Command writing
select * from [Oracle].[Tax] retrieves (ORA - 00903) invalid table name
select * from "[Oracle]"."[Tax]" retrieves (ORA - 00942) table or view does not exist
select * from LinkedSever.."[Oracle]"."[Tax]" retrieves (ORA - 00903) invalid table name
select * from "LinkedSever".."[Oracle]"."[Tax]" retrieves (ORA - 00903) invalid table name
Solution:
I got it.
select * from Oracle.Tax simply without brackets
Otherwise, try the troubleshooting tips here:
Set up and troubleshoot a linked server to an Oracle database in SQL Server
I am incorporating a SQL Server query in C# and using a RDLC report to show query results. My query produces the correct result when run on SQL Server, but I get an error
Conversion failed when converting date/time from character string
when using it from C# code. I could not figure out why got this error. Please help me
C# code:
DateTime Date =DateTime.Parse(d2.Value.Date.ToString());
string format = "yyyy-MM-dd";
MessageBox.Show(Date.ToString(format));
this.soldItemsDetailsTableAdapter.Fill(this.Daily_Sales_Report.SoldItemsDetails, Date.ToString(format));
The error occurs on the last line of this code.
Date stored in database in format 'yyyy-MM-dd' and the above code produces date format as required as confirmed by the following code:
MessageBox.Show(Date.ToString(format));
SQL Server query:
SELECT
b.CompanyID, b.Category, a.ModelNo, a.ProductCode, a.Qty
FROM
(SELECT
s.DateSold, p.ProductID, p.ModelNo, p.ProductCode,
SUM(sipl.SubTotal) AS Qty
FROM
SalesInvoiceProductsList AS sipl
INNER JOIN
SalesInvoice AS s ON sipl.SalesInvoiceID = s.SalesInvoiceID
INNER JOIN
Product AS p ON sipl.ProductID = p.ProductID
GROUP BY
p.ModelNo, p.ProductCode, p.ProductID, s.DateSold) AS a
INNER JOIN
(SELECT DISTINCT
p.ProductID, p.CompanyID, cat.Category
FROM
Product AS p
INNER JOIN
Category AS cat ON p.CatID = cat.CatID
INNER JOIN
Company AS c ON p.CompanyID = c.CompanyID) AS b ON a.ProductID = b.ProductID
WHERE
(a.DateSold = #Date)
ORDER BY
b.CompanyID
If I use simple query which also takes date input instead of this complex query then got NO error. However his complex query works fine on SQL Server.
I resolved the issue by changing nothing except the query. Both queries executes well on server but the integration of first query issue the error under discussion but the following query does not issue any error, although the date format is same in both queries. I could not understand the reason. Can anybody explain why?
SELECT com.CompanyID AS company, c.Category, p.ModelNo AS Model, (sipl.SubTotal) AS Qty
FROM SalesInvoiceProductsList AS sipl
INNER JOIN SalesInvoice AS s ON sipl.SalesInvoiceID = s.SalesInvoiceID
INNER JOIN Product AS p ON sipl.ProductID = p.ProductID
INNER JOIN Company AS com ON p.CompanyID = com.CompanyID
INNER JOIN Category AS c ON p.CatID = c.CatID
WHERE (s.DateSold = #Date)
GROUP BY com.CompanyID, c.Category, p.ModelNo,sipl.SubTotal
Hello! I tried to connect 4 tables form Access in Visual Basic c# and I got "Syntax error (missing operator) in query expression", can you please help me? Thanks!
string query = "Select e.Denumire_Ech, e.Descriere_Ech, e.UnitateMasura, e.Pret_Vanzare, o.Cantitate_EchOf From ECHIPAMENTE e INNER JOIN OFERTE o ON e.Cod_Echipament = o.Cod_Echipament INNER JOIN CONTRACTE c ON c.Cod_Oferta = o.Cod_Oferta INNER JOIN FACTURI f ON f.Nr_Contract = c.Nr_Contract WHERE Nr_Contract='" + CB_Contract.Text + "'";
You have multiple tables you are joining across but have not specified which table your WHERE clause is looking up on.
string query = "Select e.Denumire_Ech, e.Descriere_Ech, e.UnitateMasura, e.Pret_Vanzare, o.Cantitate_EchOf From ECHIPAMENTE e INNER JOIN OFERTE o ON e.Cod_Echipament = o.Cod_Echipament INNER JOIN CONTRACTE c ON c.Cod_Oferta = o.Cod_Oferta INNER JOIN FACTURI f ON f.Nr_Contract = c.Nr_Contract WHERE c.Nr_Contract='" + CB_Contract.Text + "'";
Also you should watch out for SQL injection and unusual characters in the CB_Contract.Text For example if there is a ' character then the sql will fail.
I create this function to calculate shortage material quantity
ALTER FUNCTION [dbo].[ReturnShortageByItemCodeLinePackage]
(#lineId int, #testpackId int, #MaterialDescriptionId int)
RETURNS float
AS BEGIN
DECLARE #shortageQuantity float
DECLARE #MIVQuantity float
DECLARE #totalQuantity float
DECLARE #spoolQuantity float
DECLARE #ExistInSiteQuantity float
DECLARE #BeforeDoneQuantity float
SELECT
#totalQuantity = Quantity,
#spoolQuantity = QuantitySpool,
#ExistInSiteQuantity = QuantityExistInSite,
#BeforeDoneQuantity = QuantityBeforeDone
FROM
[SPMS2].[dbo].Materials
WHERE
LineId = #lineId
AND TestPackageId = #testpackId
AND MaterialDescriptionId = #MaterialDescriptionId
SELECT
#MIVQuantity = SUM(QuantityDeliver)
FROM
MaterialIssueVoucherDetails miv
JOIN
MaterialRequestContractorDetails mrc ON miv.MaterialRequestContractorDetailId = mrc.Id
WHERE
TestPackageId = #testpackId
AND LineId = #lineId
AND miv.MaterialDescriptionId = #MaterialDescriptionId
IF #MIVQuantity IS NULL
BEGIN
SET #MIVQuantity = 0
END
SET #shortageQuantity = #totalQuantity - (#BeforeDoneQuantity + #ExistInSiteQuantity + #spoolQuantity + #MIVQuantity)
RETURN round(#shortageQuantity,3)
END
I use this function inside my stored procedure as you can see here:
ALTER PROCEDURE [dbo].[SPViewMTO]
AS
BEGIN
SELECT
dbo.Lines.Unit, dbo.Lines.LineNumber, dbo.Lines.DocumentNumber,
dbo.BaseMaterials.Name AS MaterialName,
dbo.MaterialDescriptions.Name AS MaterialDescription,
dbo.MaterialDescriptions.Description, dbo.MaterialScopes.ScopeName,
dbo.MaterialScopeObjectNames.ObjectName,
dbo.MaterialDescriptions.Size1, dbo.MaterialDescriptions.Size2,
dbo.MaterialDescriptions.ItemCode,
dbo.Materials.Quantity, dbo.Materials.Discipline, dbo.Materials.Id,
dbo.Lines.Id AS LineId, dbo.Materials.QuantitySpool,
dbo.Materials.QuantityExistInSite, dbo.Materials.QuantityBeforeDone,
dbo.TestPackages.PackageNumber, dbo.Materials.TestPackageId,
ISNULL(dbo.ReturnShortageByItemCodeLinePackage(Lines.Id, TestPackageId, MaterialDescriptionId), 0) AS Shortage
FROM
dbo.Materials
INNER JOIN
dbo.Lines ON dbo.Materials.LineId = dbo.Lines.Id
INNER JOIN
dbo.BaseMaterials ON dbo.Lines.BaseMaterialId = dbo.BaseMaterials.Id
INNER JOIN
dbo.MaterialDescriptions ON dbo.Materials.MaterialDescriptionId = dbo.MaterialDescriptions.Id
INNER JOIN
dbo.MaterialScopes ON dbo.MaterialDescriptions.MaterialScopeId = dbo.MaterialScopes.Id
INNER JOIN
dbo.MaterialScopeObjectNames ON dbo.MaterialDescriptions.MaterialScopeObjectId = dbo.MaterialScopeObjectNames.Id
INNER JOIN
dbo.TestPackages ON dbo.Materials.TestPackageId = dbo.TestPackages.Id
END
The time execution of the stored procedure is about 47 seconds inside my SQL Server Management Studio directly, but when I call the stored procedure` from my C# application I get this error:
timeout expired the timeout period elapsed SQL Server 2012
Here is my code to call the stored procedure:
lst = _ctx.Database.SqlQuery<ViewDomainClass.MaterialOffice.DAViewMTO>
("EXEC [dbo].SPViewMTO").ToList();
Timeout is a property of connection string in ADO.Net and other Frameworks like LINQ & EF.
Try to change timeout in your connection string as you want.
Also you can set time out on SQLCommand to override timeout property.
You use (NOLOCK) you can increase performance your query.
ALTER PROCEDURE [dbo].[SPViewMTO]
AS
BEGIN
SELECT dbo.Lines.Unit, dbo.Lines.LineNumber, dbo.Lines.DocumentNumber, dbo.BaseMaterials.Name AS MaterialName,
dbo.MaterialDescriptions.Name AS MaterialDescription, dbo.MaterialDescriptions.Description, dbo.MaterialScopes.ScopeName,
dbo.MaterialScopeObjectNames.ObjectName, dbo.MaterialDescriptions.Size1, dbo.MaterialDescriptions.Size2, dbo.MaterialDescriptions.ItemCode,
dbo.Materials.Quantity, dbo.Materials.Discipline, dbo.Materials.Id, dbo.Lines.Id AS LineId, dbo.Materials.QuantitySpool, dbo.Materials.QuantityExistInSite,
dbo.Materials.QuantityBeforeDone, dbo.TestPackages.PackageNumber, dbo.Materials.TestPackageId,isnull(dbo.ReturnShortageByItemCodeLinePackage(Lines.Id,TestPackageId,MaterialDescriptionId),0) As Shortage
FROM dbo.Materials(NOLOCK) INNER JOIN
dbo.Lines(NOLOCK) ON dbo.Materials.LineId = dbo.Lines.Id INNER JOIN
dbo.BaseMaterials(NOLOCK) ON dbo.Lines.BaseMaterialId = dbo.BaseMaterials.Id INNER JOIN
dbo.MaterialDescriptions(NOLOCK) ON dbo.Materials.MaterialDescriptionId = dbo.MaterialDescriptions.Id INNER JOIN
dbo.MaterialScopes(NOLOCK) ON dbo.MaterialDescriptions.MaterialScopeId = dbo.MaterialScopes.Id INNER JOIN
dbo.MaterialScopeObjectNames(NOLOCK) ON dbo.MaterialDescriptions.MaterialScopeObjectId = dbo.MaterialScopeObjectNames.Id INNER JOIN
dbo.TestPackages(NOLOCK) ON dbo.Materials.TestPackageId = dbo.TestPackages.Id
EnD
I got a query used by a Webservice which contains a subquery. When executing the query in SQL Server Management Studio, it works fine. However, using this in the webservice as a SqlCommand, I get:
System.Data.SqlClient.SqlException: Incorrect Syntax near the keyword 'JOIN'.
Incorrect Syntax near the keyword 'AS'.
Inserting extra characters let me figure out that the error refers to the position right after the subquery.
The query is as following:
SELECT H.H_ID AS ID, H.Name, TD.TDValue, A.A_ID, A_C.Value
FROM
H CROSS JOIN
A JOIN
(SELECT TD.A_ID, TD.H_ID, MAX(cast(TD.Version AS bigint)) AS Version FROM TD GROUP BY TD.A_ID, TD.H_ID) AS TData ON TData.H_ID = H.H_ID AND TData.A_ID = A.A_ID LEFT JOIN
TD2 ON TD2.H_ID = H.H_ID AND TD2.A_ID = A.A_ID AND TD2.Version = TData.Version LEFT JOIN
A_C ON A_C.A_ID = A.A_ID AND cast(A_C.R AS CHAR) = cast(TD.TDValue AS CHAR)
WHERE (H.Info = 1);
The Subquery is needed to get the last Version of an entry of the table TD.
If there are better ways to do that, feel free to let me know as well. :-)
Are there any limitations with the SqlCommand in C# that do not exist in MS SQL, or am I doing something wrong?
I would appreciate any help so much!
I would think that using a common table expression would be more efficient in this case. I'm not 100% that this is exactly what you need as I don't know your database structure and data needs, but this should help.
;with
cte_Tdata AS
(
SELECT
TD.A_ID
,TD.H_ID
,MAX(cast(TD.Version AS bigint)) AS Version
FROM
TD
GROUP BY
TD.A_ID
,TD.H_ID
),cte_TD AS
(
SELECT
TD.A_ID
,TD.H_ID
,TD.TDValue
FROM
TD
)
SELECT H.H_ID AS ID, H.Name, cte_TD.TDValue, A.A_ID, A_C.Value
FROM
H CROSS JOIN
A JOIN cte_Tdata ON cte_Tdata.H_ID = H.H_ID
AND cte_Tdata.A_ID = A.A_ID
INNER JOIN cte_TD ON cte_Tdata.H_ID = cte_TD.H_ID
AND cte_Tdata.A_ID = cte_TD.A_ID
LEFT JOIN A_C ON A_C.A_ID = A.A_ID
AND cast(A_C.R AS CHAR) = cast(cte_TD.TDValue AS CHAR)
LEFT JOIN TD2 ON TD2.H_ID = H.H_ID
AND TD2.A_ID = A.A_ID
AND TD2.Version = cte_Tdata.Version
WHERE
H.Info = 1;