Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SQL Select with the second DB
Message
 
 
To
30/01/2022 11:39:20
Lutz Scheffler (Online)
Lutz Scheffler Software Ingenieurbüro
Dresden, Germany
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
01683370
Message ID:
01683395
Views:
29
>>>>Hi,
>>>>
>>>>I am working on modifying a SQL Select that pulls data from two databases.
>>>>The first one is the default.
>>>>The second database is specified in the variable cSecondDb. For example,
>>>>
>>>>cSecondDb = "SQLDbNo2"
>>>>
>>>>
>>>>In the SQL select I put the variable cSecondDb as a prefix to the table that is in the second DB. Example:
>>>>Table 1 is in the 1st DB
>>>>Table 2 is in the 2nd DB
>>>>
>>>>select column1 from Table1 where column2 in (select Table2.pk_field from " + cSecondDb + "Table2" +  where 
>>>>Table2.field3 = 'xyz'
>>>>
>>>>I know that the above is probably not well formed SQL Select. But my question is,
>>>>Do I need to put the variable cSecondDb before EACH and every time the table in the second DB is used? Or just in the FROM clause?
>>>>That is, do I need the cSecondDb before "select table2.pk_field" or by saying that I am selecting FROM cSecondDb + "table2" is enough?
>>>>TIA
>>>
>>>IIRC, try this:
>>>
>>>select column1 from Table1 where column2 in (select T2.pk_field from " + cSecondDb + ".Table2 as t2 where 
>>>t2.field3 = 'xyz'
>>>
>>
>>Thank you. The important thing is that you used cSecondDb only once, in the FROM clause. This answers my question.
>
>Sorry for jumping in late.
>To put in simple terms:
>Every table or view not open that will be opened by your query need to be prefaced with the DBC name if used in FROM or JOIN. This works even for tables with same name in different databases. Then you give them a ALIAS inside the query - this is the FROM .. AS .. part and use this inside the query only. Always use the alias, never assume a field without.
>This is true for VFP calls as well, if you use two or more DBCs in a SELECT SQL, or query a DBC not active with SET DATABASE, but open. In VFP USE .. ALAIS .. may play havoc, but this is not the question here.
>
>I consider it good practice to include the database name in every FROM / JOIN, but one of my first jobs with SELECT SQL was two databases, so I was forced to it.
>
>Note that this all is true most SQL commands.

Thank you.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Reply
Map
View

Click here to load this message in the networking platform