SQL Server 2008 JOIN with Oracle 11g - sql

I have successfully linked an Oracle 11g database to SQL Server 2008. I can run a simple query in SQL Server which displays Oracle data:
SELECT PRODUCT_CODE
FROM [ORACLE-LINK]..ORACLE_SCHEMA.PRODUCTS_TABLE
This does exactly what I would expect it to do.
The problem comes when I try to do a simple join:
SELECT ProductName, [ORACLE-LINK]..ORACLE_SCHEMA.PRODUCTS_TABLE.PRODUCT_NAME
FROM SqlServer_table
INNER JOIN [ORACLE-LINK]..ORACLE_SCHEMA.PRODUCTS_TABLE
ON SqlServer_table.Product_ID=[ORACLE-LINK]..ORACLE_SCHEMA.PRODUCTS_TABLE.PRODUCT_CODE
This causes a 'The multi-part identifier could not be bound' error on the Oracle part of the JOIN statement. I know from research that the syntax has to be exact, and I think I've tried almost every permutation. Perhaps there's something in the SQL Server settings/registry...

Following #shiva's helpful suggestion I reformatted the query with aliases. This threw a server collation mismatch error, which proved much more illuminating than the 'The multi-part identifier' message I had been getting earlier. It turns out the joined columns had different collations (SQL_Latin1_General_CP1_CI_AS vs Latin1_General_CI_AS). Adding a simple COLLATE to the end of the JOIN AS part of the query sorted this out.

Have you tried alias-ing the table names? Like so
SELECT SqlSvr.ProductName AS Sql_ProductName
, Orcl.PRODUCT_NAME AS Orcl_ProductName
FROM SqlServer_table AS SqlSvr
INNER JOIN [ORACLE-LINK]..ORACLE_SCHEMA.PRODUCTS_TABLE AS Orcl
ON SqlSvr.Product_ID = Orcl.PRODUCT_CODE

Related

Understanding of difference between MariaDB and SQL Server for a query

I am using MS SQL Server Version 2008 and MariaDB 10.5.
My table and data :
Following is the query i am running on both the databases.
select distinct distributor from market order by city
Found this query is failing in MS SQL Server database with error
SQL Error [145] [S0001]: ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
Understood the reason why it failing
Reason for - ORDER BY items must appear in the select list if SELECT DISTINCT is specified
when i ran the same query run with MariaDB it ran successful without any error and gave me below output.
So i have doubt why MariaDB is behave different here? ideally it should fail right?
Thanks in advance.
Please try this query
Select distinct col from (
Select col from table order by col
)

Compare two CLOB columns in Oracle SQL Developer

I am trying to compare two columns of CLOB datatype in Oracle SQL Developer. Below is the query used,
select *
from t1
join t2 on t1.reference_no = t2.reference_no
where dbms_lob.compare (t1.text, t2.t_col_clob) = 0;
Getting error message as
ORA-00904: : invalid identifier.
This means I don't have access to dbms_lob.compare
How comparison can be done with using dbms_lob.compare?
Please note raised this as separate because all the information available is using dbms_lob.compare, Found nothing how this can be done without using dbms_lob.compare and also I won't be getting access to dbms_lob.compare. Please suggest a SQL way to compare this.

How to use GROUP BY for multiple columns in SQL server

I am working on existing project which is developed on Laravel + Mysql. Now, I have migrated my database from Mysql to Sql server. So all the queries written in project using Mysql syntax.
Now, I have an query given below:
SELECT table_1.*, table_3.date as sem1date
FROM table_1, table_2, table_3
WHERE table_3.ID=table_2.DID AND block_datelu.BID=table_1.final_exam_date AND table_1.centreid=1234 AND table_1.course=1
GROUP BY table_1.start_date, table_1.end_date
When I am converting above query into sql server syntax, then sql server giving me error msg "Column 'table_1.Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
Can anyone tell me how can I use group by for multiple columns having joins?

Bigquery type cast on JOIN Clause

I'm trying to join two tables on two columns
-- query to join two tables
SELECT
*
FROM
[raw.raw_sales] AS game_records
JOIN
[facebook_aggregate.avg_aggregate] AS avg_aggregate
ON
(game_records.Away_team = avg_aggregate.team_name)
AND (game_records.game_date = avg_aggregate.time_update)
it gives me this error Error: 10.30 - 10.56: Timestamp literal or explicit conversion to timestamp is required. because game_records.game_date is type STRING and avg_aggregate.time_update is type DATE.
but if I do the conversion within the JOIN..ON.. clause
-- query to join two tables
SELECT
*
FROM
[raw.raw_sales] AS game_records
JOIN
[facebook_aggregate.avg_aggregate] AS avg_aggregate
ON
(game_records.Away_team = avg_aggregate.team_name)
AND (DATE(game_records.game_date) = DATE(avg_aggregate.time_update))
It gives me this error:
Error: ON clause must be AND of = comparisons of one field name from each table, with all field names prefixed with table name. .
Is there any way to do this without creating an intermediate table? Thanks!
Try using standard SQL (uncheck "Use Legacy SQL" under "Show Options"). You shouldn't need to do anything aside from remove the brackets around the table names:
SELECT
*
FROM
raw.raw_sales AS game_records
JOIN
facebook_aggregate.avg_aggregate AS avg_aggregate
ON
game_records.Away_team = avg_aggregate.team_name
AND game_records.game_date = avg_aggregate.time_update;
BigQuery Standard SQL (see Enabling Standard SQL) does not have this limitation for ON clause. Try running your query in Standard SQL.
As Elliott mentioned - make sure you are not using square brackets around tables references. In Standard SQL - when you need to escape special chars - yo should use back-ticks
Also check Migrating from legacy SQL if you will follow above direction

SQL Joins Not Returning All Field Values; Different Field Values Returned When Order Changed

I'm using the following SQL to query 3 simple tables and pull back results. When I run this query in SQL Server 2005 EM (database that's being used), the correct recordset results are returned. When I execute this from my webpage (ASP) via an SQL statement or a stored procedure, I get blank values for some of the columns. Also, I noticed that when I change the order of the tables being selected in my FROM clause, some of the previous columns then return blank values. Is there something wrong with my SQL for it not to work when calling it? I developed my app locally and it works fine. When I deployed it to the client's network, the problems started... The client is running Win 2000 SP4 as their application server and my app is developed in ASP 3.0 with a SQL Server 2005 datastore.
SELECT
Scorecard_Measure.Measure,
Scorecard_Measure.Target,
Scorecard_Measure.YTD,
Scorecard_Measure.Status,
Scorecard_Measure.Explanation,
Scorecard_Measure.Division,
Scorecard_Measure.ZIndex,
Scorecard_Measure.LastUpdated,
Scorecard_Measure.ID,
Scorecard_Objectives.Details,
Scorecard_Objectives.ZIndex,
Scorecard_ObjectiveCats.IdentityLetter
FROM
[Scorecard_Measure],
[Scorecard_Objectives],
[Scorecard_ObjectiveCats]
WHERE
Scorecard_Measure.ObjID=Scorecard_Objectives.ID
AND Scorecard_Objectives.ObjCatID = Scorecard_ObjectiveCats.ID
AND Scorecard_Measure.FiscalYear = '2011'
AND Scorecard_Measure.Publish='Y'
ORDER BY Scorecard_Measure.LastUpdated DESC
Scorecard_Objectives.ID is a foreign key in the Scorecard_Measure table, Scorecard_ObjectiveCats.ID is a foreign key in the Scorecard_Objectives table.
Also, a weird occurence. I have two column names that are the same and when I reference those columns, the server isn't throwing an error like I've seen before saying "your results have columns with the same name, reference them using the tables they come from"... e.g. rs("Scorecard_Objectives.ZIndex") and rs("Scorecard_Measure.ZIndex") -- when I use these references, I get an error from IIS.
Any points is appreciated. Thanks in advance!
Access Both ZINDEX with different variables. Try this join query.
SELECT
Scorecard_Measure.Measure,
Scorecard_Measure.Target,
Scorecard_Measure.YTD,
Scorecard_Measure.Status,
Scorecard_Measure.Explanation,
Scorecard_Measure.Division,
Scorecard_Measure.ZIndex as MZIndex,
Scorecard_Measure.LastUpdated,
Scorecard_Measure.ID,
Scorecard_Objectives.Details,
Scorecard_Objectives.ZIndex as OZIndex,
Scorecard_ObjectiveCats.IdentityLetter
FROM
Scorecard_Measure INNER JOIN Scorecard_Objectives
ON Scorecard_Measure.ObjID=Scorecard_Objectives.ID
AND Scorecard_Measure.FiscalYear = '2011'
AND Scorecard_Measure.Publish='Y'
INNER JOIN Scorecard_ObjectiveCats
ON Scorecard_Objectives.ObjCatID = Scorecard_ObjectiveCats.ID
ORDER BY Scorecard_Measure.LastUpdated DESC