Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
SQLExec() SQL Select from 2 DB
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Divers
Thread ID:
01654292
Message ID:
01654301
Vues:
48
>>>>>
select Table1.fielda from Table1 left join db2.dbo.Table2 on Table1.pk = Table2.pk where table2.field2 = '1234' 
>>>-- assumming the schema is dbo and the database is db2 for table 2
>>>>>
>>>>>if is not in the same instance then you will need to create a linked server on the first instance and add the linked server in a four part naming [linked server].[database].[schema].[table]
>>>>
>>>>I think when you are saying "instance" you mean instance of SQL Server. In my case, it is the same instance of the SQL Server but different databases. As in your code, 'db2' is a different database on the same SQL Server.
>>>>And I suppose that since SQLEXEC uses the handle for one database, the code won't work. Am I understanding correctly?
>>>
>>>It will work. The dbname.dbo.table.field allows you to use tables from other databases on the same instance. For that matter, you don't really need two handles if you use that syntax.
>>
>>What confuses me is that how does SQLEXEC() connects to the second database? Doesn't it need credentials to connect to the second database?
>
>When you use full qualified name of the table you can use it, if they are on the same instance of SQL Server.
>The instance and SQL Server is not the same, you may have 2-3 or more instances of SQL Server installed on the same computer.
>The only thing that you can break your query if that the user you use to connect to SQL Server may have no access to one of the databases:
>So this is a query you can use:
>
>TEXT TO lcSQL NOSHOW 
>SELECT * 
>FROM TableFromDefaultDataBaseForConneection
>INNER JOIN DataBaseName.schemaname.TableName ON ....
>ENDTEXT
>? SQLEXEC(handle, lcSQL,[AliasHere])
>
Thank you. I may break the query just to be sure that the user has access to the second database. Also, I don't like to use the DB name in the query (it would be hard-coding it). I prefer to use a handle.
"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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform