I have a MySQL statement which when executed in MySQL workbench, successfully returns a result. The statement in question is below:
SELECT report.*, client.`Client`, 3rdpartyreport.`Status` FROM report
LEFT JOIN client ON report.ClientID = client.ID
LEFT JOIN 3rdpartyreport ON (report.`Serial No` = 3rdpartyreport.`Serial No` AND report.`AssignedTo` = 3rdpartyreport.`Received By`)
WHERE (report.Status <> 'Deleted')
ORDER BY report.`Serial No` DESC
I am joining a column from the client table and the 3rdparty table to the report table. However, when I input this exact query into Visual Studio 2013 Dataset designer, it gives the error:
Error in join expression. Unable to parse query text.
As soon as I remove one of the LEFT JOIN lines, it works ok. Why doesn't Visual Studio allow multiple left joins? Am I doing something wrong, and if not then is there another way I can achieve the same result?
Figured it out. I needed to have 3rdpartyreport written as '3rdpartyreport', because it starts with a digit. Workbench doesn't need this apparently.
Related
I am attempting to build the following tableadapter query in Visual Studio 2019
SELECT * FROM Vendors
ORDER BY VendorID
OFFSET 5 ROWS
FETCH NEXT 5 ROWS ONLY
And it gives the error "Unable to Parse Query Text" at the offset command
I know this query works on the database itself as i can run it successfully on the sql server (2019 Express).
Will Visual Studio tableadapter queries not recognize the offset command? Or is the syntax different in some way?
TableAdapters are pretty old these days, but they do remain a serviceable data access strategy. The designer attempts to parse the query you entered and doesn't like it much with the extra offset parts.
I recommend you try:
right click your dataset surface
add >> tableadapter
"select that downloads rows"
put the query in as SELECT * FROM Vendors
finish the wizard
click the fill,getdata() line of the adapter
in the properties grid, paste the extra clause onto the end of the query text
say "no" to "do you want to update the other queries?"
You should be left with a usable adapter. See the footnote though
You have other options if you want to use a lot of syntax that designer doesn't like; most notably you can say "Create New Stored Procedures" when going through the wizard, put a base query of SELECT * FROM Vendors, VS will make the sprocs, and then you can just edit the OFFSET etc into the sproc command in SSMS
Footnote: I personally still use TAs a lot and have never needed such syntaxes, but generally I make the first query in a TA of the form SELECT * FROM x WHERE ID = #y so it'll only ever pull one row anyway and is very "plain SQL" - the datatable schema is driven from it and it "just works":
Then other queries have defined uses such as SELECT * FROM x WHERE Name LIKE ... - If you adopt this approach of making the firt query in a "normal" one that it can cope with, then you can certainly add another query with unsupported syntax - you get an error "unable to parse query text":
but you can ignore it and finish the wizard anyway, and it will work fine
I want to create a Crystal report that takes values from 3 MySQL tables but I want to write my custom query because when I select tables in visual studio it shows a query that doesn't meet what I expected.
I tested query on localhost phpMyAdmin and it worked.
note 1: I am using visual studio 2019 and installed Crystal report SP26
note 2: when visual studio wrote the query it added "1" to the table name
like boq_table make it boq_table1.
code below represents what query I want to execute:
SELECT
ubc.boq_table.itemNum,
ubc.boq_table.descriptionOfWork,
ubc.boq_table.unit,
ubc.boq_table.contractualQuantity,
ubc.boq_table.priceNum,
ubc.summary.executedQuantLastSummary,
ubc.summary.priceLastWorks,
ubc.summary.executedQuantBetw2Sum,
ubc.submittal.priceCurrentWorks
FROM
ubc.boq_table
LEFT OUTER JOIN ubc.summary ON
ubc.boq_table.itemNum = ubc.summary.itemNum
LEFT OUTER JOIN ubc.submittal ON
ubc.boq_table.itemNum = ubc.submittal.itemNum
WHERE
ubc.boq_table.projectName ='proj'
UNION
SELECT
ubc.boq_table.itemNum,
ubc.boq_table.descriptionOfWork,
ubc.boq_table.unit,
ubc.boq_table.contractualQuantity,
ubc.boq_table.priceNum,
ubc.summary.executedQuantLastSummary,
ubc.summary.priceLastWorks,
ubc.summary.executedQuantBetw2Sum,
ubc.submittal.priceCurrentWorks
FROM
ubc.summary
LEFT OUTER JOIN ubc.boq_table ON
ubc.summary.itemNum = ubc.boq_table.itemNum
LEFT OUTER JOIN ubc.submittal ON
ubc.summary.itemNum = ubc.submittal.itemNum
WHERE
ubc.summary.projectName = 'proj'
UNION
SELECT
ubc.boq_table.itemNum,
ubc.boq_table.descriptionOfWork,
ubc.boq_table.unit,
ubc.boq_table.contractualQuantity,
ubc.boq_table.priceNum,
ubc.summary.executedQuantLastSummary,
ubc.summary.priceLastWorks,
ubc.summary.executedQuantBetw2Sum,
ubc.submittal.priceCurrentWorks
FROM
ubc.submittal
LEFT OUTER JOIN ubc.boq_table ON
ubc.submittal.itemNum = ubc.boq_table.itemNum
LEFT OUTER JOIN ubc.summary ON
ubc.submittal.itemNum = ubc.summary.itemNum
WHERE
ubc.submittal.projectName = 'proj'
ORDER BY
itemNum;
and this code was written by the visual studio after I selected tables
SELECT boq_table1.itemNum, boq_table1.descriptionOfWork, boq_table1.unit, boq_table1.contractualQuantity, boq_table1.priceNum, submittal1.priceCurrentWorks, summary1.executedQuantLastSummary, summary1.executedQuantBetw2Sum, summary1.priceLastWorks
FROM (ubc.boq_table boq_table1 INNER JOIN ubc.submittal submittal1 ON boq_table1.ID=submittal1.ID) INNER JOIN ubc.summary summary1 ON boq_table1.ID=summary1.ID
ORDER BY boq_table1.itemNum
the last code read-only I can't edit
You can do 2 things if you want to get data to crystal reprots, you look again in the database selector, there is an option for adding a command or use the connector and a datareader to build a dataset that you can use.
The first is ok, if you don't need to compute the data heavily.
I am issuing an SQL query in my visual studio application to search a database. I am having trouble joining two tables into one query search. I am looking to combine the First and Last name of an employee and match it up to a phone number. In one table I have an abbreviated name, and on the other table I have the full name.
Example
Code
cmdTxt.Append("SELECT partner.staffid, staff.Forename,staff.surname FROM tblpartner LEFT JOIN tblstaff ON staff.StaffID = partner.staffid ORDER BY staff.forename , staff.surname ");
I would Like to have a field that shows the Forename , surename, and phone number in one query search
You are using "partner" and "staff" as table aliases, but you aren't assigning those aliases to the table names. If you want to use those instead of the full table names, you need:
SELECT partner.staffid, staff.Forename,staff.surname, partner.phone
FROM tblpartner partner
LEFT JOIN tblstaff staff ON staff.StaffID = partner.staffid
ORDER BY staff.forename , staff.surname
As general advice, get your query working outside of your string literal; enter it into SQL Server Management Studio or something similar. Had you done that, it should have clearly showed you what the problem was. Then when you have your query set how you want, you can copy it into your Command Text in your Visual Studio application.
Using IDataReader in c# to pull a list of host names from a database that correspond to a specific business unit, but can't figure out what is wrong. I am guessing that there are too many arguments in the SQL statement, or perhaps the ExecuteReader method here should not be used. I cannot create a SP in the target DB so I am left with running the query in some other manner. Any help will be greatly appreciated.
public static IDataReader GetCETHostsList()
{
string mySQL = #"SELECT distinct(dbo.Infrastructure.Hostname) dbo.Application_Infrastructure
INNER JOIN dbo.Applications ON dbo.Application_Infrastructure.ID = dbo.Applications.ID
INNER JOIN dbo.Infrastructure ON dbo.Application_Infrastructure.InfrastructureID = dbo.Infrastructure.InfrastructureId
WHERE Unit is not null
AND dbo.Applications.Unit like '%Terminal%'
AND (dbo.Infrastructure.Hostname like '%ST%' or dbo.Infrastructure.Hostname like '%TR%' )";
return DatabaseFactory.CreateDatabase("myDB").ExecuteReader(mySQL);
}
You're missing the FROM keyword.
Generally, if you use SQL embedded in C# programs, it's really a lot easier if you run the SQL in Query Analyzer / SQL Management Studio or something first. MS SQL Express is free; or VS Pro can run sql queries too.
I am trying to update my mysql table "upproj" using Visual Studio 2010 (c#).
I want to execute the following query :
string Query = " update tlog.upproj set summ=(select sum(grade) from tlog.upproj group by ams having count(*) =2); ";
But I get error
"You can't specify target table 'upproj' for update in FROM clause".
When I execute the previous query on Mysql Query browser I don't get any error, but my project needs to execute this query from visual studio.
Is there any way to fix that?
As the error hints at, you can't specify the table you're updating in the UPDATE statement's FROM clause (in the sub-query); the same is true if you were to try to use it in a WHERE clause.
You can accomplish the task of "using the table you're updating" if you use a JOIN though.
Based on the schema inferred by your existing query, the ams field is unique to a set grouping and you can, therefore, add that as part of your clause.
Try the following (untested):
UPDATE
tlog.upproj AS u1
JOIN (SELECT ams, SUM(grade) AS grade_sum FROM tlog.upproj GROUP BY ams HAVING COUNT(*) = 2) AS u2
ON u1.ams = u2.ams
SET
summ = u2.grade_sum;
This should update each record for ams with the total sum of grade for that same ams. If a specific ams doesn't have 2 entries, it isn't updated.