Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

query between 2 servers Expand / Collapse
Author
Message
Posted Friday, August 17, 2012 12:53 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, September 9, 2013 7:19 AM
Points: 4, Visits: 20
Hi everyone!
I'm self-taught in SQL, so my knowledge is fragmented, but I'm not a complete novice.
I'm migrating an Access db to SQL2005 (that part went well) and now I'm trying to retool all the queries that were in the Access db. One of the original Access "make-table" queries used linked tables from another server.
Now, the server where my "new" SQL db resides already has a link to the other database. How would I change the Access SQL statement (that knows where the linked tables are located) to work in my new SQL db? there are 3 tables linked from the other server/db.
Here's the Access code (and I apologize in advance if I didn't link it correctly):
SELECT admin_lookup_contract.contract_id, admin_lookup_contract.contract_name, admin_lookup_contract.contract_eff_date, admin_lookup_contract.contract_exp_date, admin_lookup_program_line.program_line_desc, admin_lookup_vendor.vendor_name, admin_lookup_contract.source_set_related_contracts INTO CAMS_Snapshot
FROM (admin_lookup_contract INNER JOIN admin_lookup_program_line ON admin_lookup_contract.program_line_id = admin_lookup_program_line.program_line_id) INNER JOIN admin_lookup_vendor ON admin_lookup_contract.vendor_key = admin_lookup_vendor.vendor_key
WHERE (((admin_lookup_contract.contract_exp_date)>Date()+30));


Thanks in advance for any help for this not-so-newbie!
Leta
Post #1346717
Posted Friday, August 17, 2012 12:58 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:08 PM
Points: 12,927, Visits: 32,333
this is creating a table dynamically, i think; or what access might call a query?

I'm weak on Access formatting:
this is what i'm talking about:

INTO
CAMS_Snapshot


this is your query reformatted for readability:
SELECT
admin_lookup_contract.contract_id,
admin_lookup_contract.contract_name,
admin_lookup_contract.contract_eff_date,
admin_lookup_contract.contract_exp_date,
admin_lookup_program_line.program_line_desc,
admin_lookup_vendor.vendor_name,
admin_lookup_contract.source_set_related_contracts
INTO
CAMS_Snapshot
FROM (admin_lookup_contract
INNER JOIN admin_lookup_program_line
ON admin_lookup_contract.program_line_id = admin_lookup_program_line.program_line_id)
INNER JOIN admin_lookup_vendor
ON admin_lookup_contract.vendor_key = admin_lookup_vendor.vendor_key
WHERE (( ( admin_lookup_contract.contract_exp_date ) > DATE() + 30 ));



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1346719
Posted Friday, August 17, 2012 1:03 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, September 9, 2013 7:19 AM
Points: 4, Visits: 20
That's correct. The "INTO CAMSSnapshot" creates a table in Access.
And I want to replicate that in SQL as well.
I realize that I will have to delete the table in my new sql db each time just prior to running this query. and, no, I don't want an "update" to the SQL table, as the other db has very dynamic data that changes, if not daily, then at least monthly.
And, also, sorry about the double-post earlier, my system locked up and I accidentally posted twice.
Post #1346723
Posted Tuesday, August 21, 2012 7:09 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, July 28, 2014 12:04 PM
Points: 35, Visits: 98
Hi am new to this forum but found the question intersting....so trying....

From your latest response, it seemd like you want to create some kind of a stg/tmp table where you get the results as a parking spot and do rpocessing on that. The next day, again clean the table and get fresh data.
For that, you need to create an sp where first you truncate / delete your table and then get the fresh data in.
So everyday, the workflow will be.....truncate data and then data in again.

But from your first question it seemed you had a linked tables(in the access world) and that your new sql db also has a linked server setup. In that case the syntax to access tables across a linked server is as follows :
[linkedserver].[dbname].[ownernm].[tablenm]

Hope this helps

Regards
Post #1348150
Posted Tuesday, August 21, 2012 9:10 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, September 9, 2013 7:19 AM
Points: 4, Visits: 20
After digging in various places on the 'Net, I found what I wanted.
For the sake of anyone else who may be still learning like myself, here's the main portion of what worked for me:
use DATABASENAME
drop table newtable
select * into DATABASENAME.dbo. newtable
from openquery(connecteddbname,'
SELECT table1.field1,table1.field2')


there was actually more than that, as my code required two other tables, and their joins AND a delimiter of a date field of the equivalent of "today()+30"
Hopefully, the italics help highlight the fields that apply to individual db's/tables/fields.
Hope this helps someone else.

As a side note, I've come back to this forum and browsed for solutions to other questions as I've been migrating this Access program, and I have to say I HAVE found some of what I've been needing here in this forum.
Thank you to the replies that actually triggered other ideas that led to the solution.
Post #1348156
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse