Multiple 'Inner Join' with openquery - inner-join

Im trying to translate an SQL query to OPENQUERY. I have this code that works well:
Select
F_ART.CODART,
F_ART.REFART,
F_ART.DESART,
F_LTA.PRELTA,
F_STO.DISSTO,
F_PRO.NOCPRO,
F_FAM.DESFAM,
F_SEC.DESSEC
From
[LINKED]...F_ART Inner Join
[LINKED]...F_FAM On F_FAM.CODFAM = F_ART.FAMART Inner Join
[LINKED]...F_LTA On F_LTA.ARTLTA = F_ART.CODART Inner Join
[LINKED]...F_PRO On F_PRO.CODPRO = F_ART.PHAART Inner Join
[LINKED]...F_SEC On F_SEC.CODSEC = F_FAM.SECFAM Inner Join
[LINKED]...F_STO On F_STO.ARTSTO = F_ART.CODART
I want to transform it to use OPENQUERY so I have tried this code:
Select
CODART,
REFART,
DESART,
IMGART,
DISSTO
From
OPENQUERY ([LINKED], 'SELECT * FROM
F_ART Inner Join
F_STO On (F_STO.ARTSTO = F_ART.CODART)
')
But when I add the next Inner Join sentences it fails.
Can I use multiple Inner Join with openquery?
Im using this code from MS ACCESS to query a Linked SQL Server.

I have solved this creting a 'View' in the SQL server.
That 'View' contains all the 'Joins' needed.
Then I create the query pointing to the 'View'.

Related

How can I get 3 tables INNER JOIN in MS SQL Server

From the specialist table, retrieve the first name, last name and contact number for the people that provide care to penguins from the species table.
There are 3 tables: tbl_specialist, tbl_species, tbl_care
I need help trying to INNER JOIN the tables to display First, Last, And Contact for penguins.
SELECT specialist_fname, specialist_lname, specialist_contact
FROM ((tbl_specialist
INNER JOIN tbl_species ON species_care = tbl_species.species_care)
INNER JOIN tbl_care ON care_id = tbl_care.care_id)
WHERE species_name = 'penguin'
;
It's a bit difficult without seeing the exact schema of the tables, but your syntax for the subquery is a bit off and you need to alias columns that are found in multiple tables in a JOIN statment. Try rewriting your SQL like this:
SELECT spl.specialist_fname, spl.specialist_lname, spl.specialist_contact
FROM tbl_specialist spl
INNER JOIN tbl_species s
ON spl.species_care = s.species_care
INNER JOIN tbl_care c
ON s.care_id = c.care_id
WHERE s.species_name = 'penguin'
I'm obviously inferring which tables certain columns come from in the join, but hopefully you get the idea.
I figured it out thank you.
SELECT specialist_fname, specialist_lname, specialist_contact
FROM ((tbl_specialist
INNER JOIN tbl_care ON tbl_care.care_specialist = tbl_specialist.specialist_id)
INNER JOIN tbl_species ON tbl_species.species_care= tbl_care.care_id)
WHERE species_name = 'penguin'
;

Inner join + set max date + naming in ACCESS SQL

I am migrating a query from tables held in MS SQL Server to MS Access 2016. I can't seem to figure out how to use the very common max_date inner join from my SQL query and import to my Access query.
I have searched the site and seen many explanations for how to do it in SQL, and my query in SQL works just fine. My Access query will not work.
INNER JOIN (
SELECT dbo_LTS_TRANSACTION_HISTORY.[RFIDTAGID], max(dbo_LTS_TRANSACTION_HISTORY.[TRANSCATION_DATE]) as [max_trxn_date]
FROM dbo_LTS_TRANSACTION_HISTORY
GROUP BY dbo_LTS_TRANSACTION_HISTORY.[RFIDTAGID]) as b
ON dbo_LTS_TRANSACTION_HISTORY.[RFIDTAGID] = b.[RFIDTAGID] AND (dbo_LTS_TRANSACTION_HISTORY.[TRANSCATION_DATE] = b.[max_trxn_date]))
I expect my [TRANSCATION_DATE] to only hold the [max_trxn_date] so only one max date record is pulled for each [RFIDTAGID]. Without naming the join "as b" I don't know how to make this work. Access doesn't seem to take the naming structure I'm used to in SQL. I am getting a compile error for syntax.
Full code below
SELECT dbo_LTS_TRANSACTION_HISTORY.PROD_ORDER, dbo_LTS_TRANSACTION_HISTORY.PART_NUMBER, dbo_LTS_PACKAGE.QUANTITY, dbo_LTS_TRANSACTION_HISTORY.LOCATION, dbo_LTS_TRANSACTION_HISTORY.TRANSCATION_DATE, dbo_LTS_TRANSACTION_HISTORY.CONTAINER_RFIDTAGID, dbo_LTS_TRANSACTION_HISTORY.RFIDTAGID, dbo_LTS_DISCRETE_JOB_SUMMARY.MFG_DELIVERY_DATE, dbo_LTS_DISCRETE_JOB_SUMMARY.EXTENSION_DATE, dbo_LTS_DISCRETE_JOB_SUMMARY.STATUS, dbo_LTS_DISCRETE_JOB_SUMMARY.PBG, dbo_LTS_LOCATION.TYPE
FROM (dbo_LTS_LOCATION
INNER JOIN (dbo_LTS_DISCRETE_JOB_SUMMARY
INNER JOIN dbo_LTS_TRANSACTION_HISTORY
ON (dbo_LTS_DISCRETE_JOB_SUMMARY.[PROD_ORDER] = dbo_LTS_TRANSACTION_HISTORY.[PROD_ORDER])
AND (dbo_LTS_DISCRETE_JOB_SUMMARY.[DISCRETE_JOB_NUMBER] = dbo_LTS_TRANSACTION_HISTORY.[JOB_NUMBER]))
ON dbo_LTS_LOCATION.[DESIGNATION] = dbo_LTS_TRANSACTION_HISTORY.[LOCATION])
INNER JOIN dbo_LTS_PACKAGE ON dbo_LTS_TRANSACTION_HISTORY.[RFIDTAGID] = dbo_LTS_PACKAGE.[RFIDTAGID]
INNER JOIN (
SELECT dbo_LTS_TRANSACTION_HISTORY.[RFIDTAGID], max(dbo_LTS_TRANSACTION_HISTORY.[TRANSCATION_DATE]) as [max_trxn_date]
FROM dbo_LTS_TRANSACTION_HISTORY
GROUP BY dbo_LTS_TRANSACTION_HISTORY.[RFIDTAGID]) as b
ON dbo_LTS_TRANSACTION_HISTORY.[RFIDTAGID] = b.[RFIDTAGID] AND (dbo_LTS_TRANSACTION_HISTORY.[TRANSCATION_DATE] = b.[max_trxn_date]))
WHERE (((dbo_LTS_TRANSACTION_HISTORY.PROD_ORDER)="123456"));

How can I search for stored procedures executing other stored procedures?

I have a list of about 350 stored procedures like this:
usp_SP1,
usp_SP2
...
I want to search through each one looking to see if any of them call other stored procedures or other databases?
I guess I would look for a line like 'exec something' in each one or a specific name of a database. ex. some_other_database
How would I do this to give me a list of the stored procedures that call other stored procedures or contain some specific string? ex. "some other database name"
I can run this below but it finds just the text. is there any way I can ensure it's a exec call and not just text?
USE [Your_DB];
GO
SELECT
ROUTINE_NAME, ROUTINE_DEFINITION
FROM
INFORMATION_SCHEMA.ROUTINES
WHERE
ROUTINE_DEFINITION LIKE '%exec %'
AND ROUTINE_TYPE = 'PROCEDURE'
AND ROUTINE_NAME IN ('usp_SP1', 'usp_SP2')
GO
You can query the sys.sql_dependencies view, like this:
SELECT o1.name AS CallerSP, o2.name AS CalledSP
FROM sys.sql_dependencies sd
INNER JOIN sys.objects o1 ON o1.object_id = sd.object_id
INNER JOIN sys.objects o2 ON o2.object_id = sd.referenced_major_id
WHERE o1.type='P' AND o2.type='P'
You may need to call sp_refreshsqlmodule for all objects before executing this query, if the called SP was created after the caller.
Other options could be to query the sys.sql_expression_dependencies view or the sys.dm_sql_referenced_entities function.
Not to compete, Razvan Socol is correct. Adding the ways to do it in earlier version back to 2000. All the old tables and views are still query-able even if not visible in ssms.
select distinct /* I believe the sys.sys views were added in 2012 or so, still works in 2017 */
od.name caller_procedure_name
,o.name called_procedure_name
from sys.sysdepends d
inner join sys.sysobjects o on o.id = d.depid and o.type = 'P'
inner join sys.sysobjects od on od.id = d.id and od.type = 'P'
select distinct /* should work all the way back to sql 2000, still works in 2017 */
od.name caller_procedure_name
,o.name called_procedure_name
from dbo.sysdepends d
inner join dbo.sysobjects o on o.id = d.depid and o.type = 'P'
inner join dbo.sysobjects od on od.id = d.id and od.type = 'P'

SQL Server to SalesForce Linked Server Nightmare

I am getting inconsistent results from joining Linked Servers. It's something that should be relatively simple... but has taken me hours to get this figured out. I am using SQL Server 2014 and the CData ODBC Driver to Join to SalesForce. I'm not doing anything fancy just trying to perform standard CRUD operations but again and again it seems that when ever I filter these linked server tables that sometimes results do not produce.
My current and main issue right now is I am having difficulty JOINING two Linked tables to two Local tables. If I remove one of the Linked tables from the join results are produced. But whenever I add two linked tables to the joins it produces and empty record set.
And yes all the related identifiers exist so it really is an issue with the Linked Server. Here are the three variations that I've tried:
SELECT * FROM Offer_Interest oi
INNER JOIN Offer o ON oi.Offer_ID_SQL = o.Offer_ID_SQL
INNER JOIN OPENQUERY([TR-SF-PROD], 'SELECT Id, OFFER_ID_SQL__C FROM Offer__c') osf ON o.Offer_ID_SQL =osf.OFFER_ID_SQL__C
INNER JOIN Interest i ON oi.Interest_ID_SQL = i.Interest_ID_SQL
INNER JOIN OPENQUERY([TR-SF-PROD], 'SELECT INTEREST_ID_SQL__C, Id FROM Interest__c') isf ON i.Interest_ID_SQL =isf.Interest_ID_SQL__c
WHERE o.PrimaryContact_ID_SQL = 2803
I've also tried without OPENQUERY:
SELECT * FROM FROM Offer_Interest oi
INNER JOIN Offer o ON oi.Offer_ID_SQL = o.Offer_ID_SQL
INNER JOIN [TR-SF-PROD].[CDataSalesforce].[Salesforce].[Offer__c] osf ON o.Offer_ID_SQL =osf.OFFER_ID_SQL__C
INNER JOIN Interest i ON oi.Interest_ID_SQL = i.Interest_ID_SQL
INNER JOIN [TR-SF-PROD].[CDataSalesforce].[Salesforce].[Interest__c] isf ON i.Interest_ID_SQL =isf.Interest_ID_SQL__c
WHERE o.PrimaryContact_ID_SQL = 2803
And Lastly I've also created Synonyms to the Linked Server tables. All of these work using the same filter or WHERE CLAUSE if I run them seperately although the linked server tables seem buggy if I filter them without OPENQUERY.
This is my first experience Linking a Server to SQL Server so anyone with experience in this or what the issue may be would be greatly appreciated!
Not the answer I was hoping as it should just work. But a temp fix I came up with is stuffing the linked server values I need into Temp Tables then joining on the temp tables, which worked... but of course this adds time onto the execution of the overall stored procedure so it is definitely not the ideal solution. If anyone has a better idea please still answer!!
IF EXISTS(SELECT [NAME] FROM tempdb.sys.tables WHERE [NAME] like '#TempOffer%') BEGIN
DROP TABLE #TempOffer;
END;
SELECT * INTO #TempOffer FROM OPENQUERY([TR-SF-PROD], 'SELECT Id, OFFER_ID_SQL__C FROM Offer__c')
IF EXISTS(SELECT [NAME] FROM tempdb.sys.tables WHERE [NAME] like '#TempInterest%') BEGIN
DROP TABLE #TempInterest;
END;
SELECT * INTO #TempInterest FROM OPENQUERY([TR-SF-PROD], 'SELECT INTEREST_ID_SQL__C, Id FROM Interest__c')
SELECT * FROM Offer_Interest oi
INNER JOIN Offer o ON oi.Offer_ID_SQL = o.Offer_ID_SQL
INNER JOIN #TempOffer osf ON o.Offer_ID_SQL =osf.OFFER_ID_SQL__C
INNER JOIN Interest i ON oi.Interest_ID_SQL = i.Interest_ID_SQL
INNER JOIN #TempInterest isf ON i.Interest_ID_SQL =isf.Interest_ID_SQL__c
WHERE o.PrimaryContact_ID_SQL = 2803

Finding all tables and fields on "many" side of relations with a certain table

in Sql Server 2005, I have a master table, and several other tables which are related to
this master through several one to many relations.
How can I find all tables and fields which are in relation with the
primary key in the master table, on "many" side?
I know I can extract this by querying views from INFORMATION_SCHEMA,
but I don't know where exactly I can find this info.
Thank you
Check out:
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE
I found the answer with some help on sql server groups
I use the following query, which returns me schema name, table and field name on one and many side of the relations:
SELECT
SchemaParent.name AS ParentSchemaName,
TableParent.name AS ParentTableName,
ColumnParent.name AS ParentColumnName,
SchemaChild.name AS ChildSchemaName,
TableChild.name AS ChildTableName,
ColumnChild.name AS ChildColumnName
FROM
sys.foreign_key_columns AS kc INNER JOIN
sys.objects AS TableChild ON kc.parent_object_id = TableChild.object_id INNER JOIN
sys.schemas AS SchemaChild ON TableChild.schema_id = SchemaChild.schema_id INNER JOIN
sys.objects AS TableParent ON kc.referenced_object_id = TableParent.object_id INNER JOIN
sys.schemas AS SchemaParent ON TableParent.schema_id = SchemaParent.schema_id INNER JOIN
sys.columns AS ColumnParent ON kc.referenced_object_id = ColumnParent.object_id AND kc.referenced_column_id = ColumnParent.column_id INNER JOIN
sys.columns AS ColumnChild ON kc.parent_object_id = ColumnChild.object_id AND kc.parent_column_id = ColumnChild.column_id
ORDER BY ParentTableName, ChildTableName

Resources