SQL SERVER 2008 - Select query OPEN ROWSET - sql-server-2008

I have a select statement where I'm pulling data from Database1 that looks something like this...
g_SQL = "SELECT 'ID' = companyID, 'Name' = (CompanyName + CompanyAddress)
g_SQL = " FROM tblCompanies WHERE CompanyStatus='InBusiness'"
Now what I need to do is go into another database and select data from tblCompanyinventory, Database2 where I need to do a...
sum(inventory) for each CompanyID
And then I'd want it to be displayed in the 'Name' like this 'Name'= (CompanyName + CompanyAddress + sum(inventory))
I will use the 2 fields: ID and NAME and I pass it on to a combobox loader. This is a vb6 application I'm working on.
Any ideas how I'd go about it? Open Rowset? I've never done anything of this nature before.
I have connections set for both - for Database1 it's g_CN1, and Database2 it is g_CN2 - as ADODB.Connection. These databases are on the same server.

Based on OP's comments some SQL like this may be what is required.
g_SQL = "SELECT 'ID' = C.companyID, 'Name' = (CompanyName + CompanyAddress), I.InvSum "
g_SQL = g_SQL + " FROM Database1.dbo.tblCompanies C JOIN (SELECT SUM(inventory) As InvSum from"
g_SQL = g_SQL + " Database2.dbo.tblCompanyInventory GROUP BY CompanyId) I "
g_SQL = g_SQL + " ON I.CompanyId = C.companyId WHERE CompanyStatus='InBusiness'"
without knowing how the connection is established I can't say whether there will be an authentication issue or not.

Related

MySQL Native Query not working in Spring Data JPA. What could I be doing wrong?

For some reasons my query is running perfectly in MySQL WorkBench but when added to the repository I am getting syntax error
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 'as e1, profile_history as e2 WHERE e2.profile_id =
e1.profile_id and' at line 1.
#Query(value = "( SELECT e2.* "
+ " FROM (select ph1.profile_id, max(ph1.last_updated_on) as last_updated_on2 "
+ " FROM profile_history as ph1 "
+ " GROUP BY ph1.profile_id ) as e1, profile_history as e2 "
+ " WHERE e2.profile_id = e1.profile_id and e2.last_updated_on = e1.last_updated_on2 )", nativeQuery = true)
Page<ProfileHistory> getAllProfileHistoryByLastestRow(Pageable paging);
Honestly expecting to get a db hit without any error and retrieve results.
Maybe you need to specify dialect in the properties file. Don’t know your MySQL version, so can’t give you concrete example, but it should be something like this:
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
I figured out the issue after fighting for 02 days. The jdbc driver is not interpreting the query correctly. I had to escape the columns and tables in the native query. Normally I shouldn't have to do this.
#Query(value = " SELECT `e2`.`*` "
+ " FROM (select `eph1`.`employee_id`, max(`eph1`.`last_updated_on`) as `last_updated_on2` "
+ " FROM `employee_profile_history` `eph1` "
+ " GROUP BY `eph1`.`employee_id` ) `e1`, `employee_profile_history` `e2` "
+ " WHERE (`e2`.`employee_id` = `e1`.`employee_id` and `e2`.`last_updated_on` = `e1`.`last_updated_on2` )",
countQuery = "SELECT distinct count(*) FROM `employee_profile_history`",
nativeQuery = true)

How do I get the value of a Form into an SQL query?

I am trying to get an Access SQL query that does this (semi-pseudocode below)
UPDATE SignIn SET SignIn.Complete=True, CompletedBy=(Select [FirstName] & " " & [LastName] AS EmployeeName From UserList where POid = Forms!HiddenUserCheck!txtPOid), CompletedDateTime=Now()
So after the query would run, the data in the database would look like
Complete EmployeeName CompletedDateTime
True John Smith 3/23/2017 8:34:10 AM
THe update query doesn't work because of syntax and not sure how to fix it.
The exact error message is
Invalid Memo, OLE, or HyperLink Object in subquery '[FirstName] & " "
& [LastName]'.
The query could be throwing a fit because of the Double Exclamation marks. Instead of
Forms!HiddenUserCheck!txtPOid
Try
Forms!HiddenUserCheck.txtPOid
You also have an extra ) at the end of your WHERE Statment
OK, then your issue may be that the subquery may return more than one record:
UPDATE
SignIn
SET
SignIn.Complete=True,
CompletedBy =
(Select First([FirstName] & " " & [LastName]) AS EmployeeName
From UserList
Where POid = Forms!HiddenUserCheck!txtPOid),
CompletedDateTime = Now()
If your name fields are Memo/LongText fields, that may be the source of the error. If so, try:
UPDATE
SignIn
SET
SignIn.Complete=True,
CompletedBy =
(Select First(Left([FirstName], 255) & " " & Left([LastName], 255)) AS EmployeeName
From UserList
Where POid = Forms!HiddenUserCheck!txtPOid),
CompletedDateTime = Now()
Edit.
You may try using DLookup for the subquery:
UPDATE
SignIn
SET
SignIn.Complete=True,
CompletedBy =
DLookup("[FirstName] & " " & [LastName]", "UserList", "POid = " & Forms!HiddenUserCheck!txtPOid & ""),
CompletedDateTime = Now()

NonUniqueDiscoveredSqlAliasException for MySQL query that works in phpMyAdmin

I have a table with four foreign keys and an image:
+--------------+--------------+---------------------+---------------------+-------+
| dataset_1_fk | dataset_2_fk | attribute_type_1_fk | attribute_type_2_fk | image |
+--------------+--------------+---------------------+---------------------+-------+
Using the name of two datasets, I would like to select the image and names of the two attributes. Here is my Hibernate code:
HibernateUtil
.getCurrentSession()
.createSQLQuery(
"SELECT ddm.image, at1.name, at2.name " +
"FROM dataset_dataset_matrices AS ddm " +
" JOIN dataset AS d1 ON d1.id = ddm.dataset_1_fk " +
" JOIN dataset AS d2 ON d2.id = ddm.dataset_2_fk " +
" JOIN attribute_type AS at1 ON at1.id = ddm.attribute_type_1_fk " +
" JOIN attribute_type AS at2 ON at2.id = ddm.attribute_type_2_fk " +
"WHERE d1.name = :dataset_1 AND d2.name = :dataset_2"
)
.setString("dataset_1", dataset_1)
.setString("dataset_2", dataset_2)
.list();
I receive this error when it executes:
org.hibernate.loader.custom.NonUniqueDiscoveredSqlAliasException: Encountered a duplicated sql alias [name] during auto-discovery of a native-sql query
at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:594)
But when I issue the same query from phpMyAdmin, it works fine. Also, if I remove either at1.name or at2.name from the SELECT statement, the query works in Hibernate. How can I fix this?
Try giving different alias name to your select columns like
"SELECT ddm.image as col1, at1.name as col2, at2.name as col3"
It seems hibernate doesn't generate the different alias automatically. If the query results has same values this issue may happens.
Reference : https://github.com/querydsl/querydsl/issues/80

with-clause not allowed on fetched associations

I have a name query like the one below - but keep getting an error from Hibernate that it will not allow my 'with'-clause since I'm doing join fetch.
I need to do join fetch - because it there is a related item then I wan't it to be present on the object.
#NamedQuery(name = "Item.findItem",
query = "SELECT DISTINCT(c) FROM Item c " +
"JOIN FETCH c.storeRelations as childStoreRelation " +
"LEFT JOIN FETCH c.relatedItems as relatedRelations WITH relatedRelations.store.id = childStoreRelation.store.id " +
"WHERE c.id = :itemId " +
"AND childStoreRelation.store.id = :storeId " +
"AND childStoreRelation.deleted <> 'Y' " +
"ORDER BY c.partnumber "),
Suggestion would be to move the 'with' part to my where clause - but this will cause invalid results.
Consider item A which might have to items related to it - but for some stores the relations are not valid.
If put in where clause then the store without relations will not shown the main item, since SQL will be build by Hibernate so it requires that if any relations exist, then they must be valid for the store - otherwise nothing is fetched :-(
So the classic fix found many places (store.id is null OR store.id = :storeId) will not work.
Does someone have another work around?
I'm running Hibernate 4.3.11
Thanks in advance
You cannot use LEFT JOIN FETCH with clause.
Please remove WITH clause and move them to WHERE clause.
It should like this:
#NamedQuery(name = "Item.findItem",
query = "SELECT DISTINCT(c) FROM Item c " +
"JOIN FETCH c.storeRelations as childStoreRelation " +
"LEFT JOIN FETCH c.relatedItems as relatedRelations " +
"WHERE c.id = :itemId " +
"AND relatedRelations.store.id = childStoreRelation.store.id" +
"AND childStoreRelation.store.id = :storeId " +
"AND childStoreRelation.deleted <> 'Y' " +
"ORDER BY c.partnumber "),
You need the ON keyword rather than the WITH keyword.

mysql: issue with the demonstration of sql injection

according to this tutorial:http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php
Below codes will demonstrate SQL Injection:
<?php
// a good user's name
$name = "timmy";
$query = "SELECT * FROM customers WHERE username = '$name'";
echo "Normal: " . $query . "<br />";
// user input that uses SQL Injection
$name_bad = "' OR 1'";
// our MySQL query builder, however, not a very safe one
$query_bad = "SELECT * FROM customers WHERE username = '$name_bad'";
// display what the new query will look like, with injection
echo "Injection: " . $query_bad;
In front end, it shows:
Injection: SELECT * FROM customers WHERE username = '' OR 1''
So I just did a test, in phpmyadmin->sql, I run below codes:
SELECT * FROM users WHERE fname = '' OR 1''
And it shows:
#1064 - 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 ''' LIMIT 0, 30' at line 1
Qustion:
supposed it will show every single entry in the "users" table, but not, why? if I want to demonstrate sql injection, how to do it?
A more typical SQL injection would be: $name_bad = "' OR 1=1 -- ";. This would lead to the following SQL:
SELECT * FROM customers WHERE username = '' OR 1=1 -- '
SELECT * FROM users WHERE fname = '' OR 1 = 1
is likely what they meant
edit just looked
SELECT * FROM users WHERE fname = '' OR 1
1 evaluates to true, so just remove the '' after it
also that link is way out of date, Look at mysqli or pdo instead