简体   繁体   English

如何在Internet Explorer中使用javascript和ADO联接两个SQL表

[英]How to join two SQL tables using javascript and ADO in Internet Explorer

I'm using Javascript and Internet Explorer's ActiveXObjects to connect to a SQL database. 我正在使用Javascript和Internet Explorer的ActiveXObjects连接到SQL数据库。 I can read out results from a single table without a problem. 我可以从单个表中读出结果,而不会出现问题。 Unfortunately, I can't figure out the syntax to join two tables that are in the same database. 不幸的是,我无法弄清楚连接同一数据库中两个表的语法。 Here's what I'm trying: 这是我正在尝试的:

//create object
var conn = new ActiveXObject("ADODB.Connection");

//store credentials
var connectionstring = "Provider=sqloledb; Data Source={omitted}; Initial Catalog={omitted}; User ID={omitted};Password={omitted}";

//open the db connection with credentials
conn.Open(connectionstring);

//create a new record set
var rs = new ActiveXObject("ADODB.Recordset");

//search the record set with SQL call
rs.Open("SELECT * FROM [order] INNER JOIN OrderItems ON (order.Id = OrderItems.Id)", conn);

If I delete "INNER JOIN" and everything after it, I'm fine. 如果删除“ INNER JOIN”及其后的所有内容,那很好。 Note that I had to put brackets around the first table's name ("order") because I think it's a protected keyword... Anyone know the right syntax for this sort of thing in Javascript? 请注意,我必须在第一个表的名称(“顺序”)前加上方括号,因为我认为这是一个受保护的关键字...任何人都知道Java语言中此类内容的正确语法吗?

This is probably the SQL syntax you are looking for: 这可能是您要查找的SQL语法:

SELECT Orders.Id
FROM [Orders] 
INNER JOIN OrderItems ON [Orders].Id = OrderItems.Id

Note that the ( and ) is unnecessary. 请注意, ()是不必要的。

If you want to use aliases, then you must use those aliases in the rest of the JOIN clause: 如果要使用别名,则必须在JOIN子句的其余部分中使用这些别名:

SELECT o.Id 
FROM [Orders] o
INNER JOIN OrderItems oi ON o.Id = oi.Id

Once you have the SQL nailed down, you should be able to pass it into the Open method: 一旦确定了SQL,就应该可以将其传递给Open方法:

var sql = 
    'SELECT Orders.Id ' +
    'FROM [Orders] ' +
    'INNER JOIN OrderItems ON [Orders].Id = OrderItems.Id';

rs.Open(sql, conn);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Internet Explorer中使用JavaScript设置Cookie - How to set cookie using javascript in Internet Explorer 如何通过Internet Explorer中运行的JavaScript连接到SQL CE数据库? - How to connect to a SQL CE database from JavaScript running in Internet Explorer? javascript 的两个元素在 Internet Explorer 中发生冲突 - Two elements of javascript are clashing in Internet Explorer 如何在Internet Explorer中使用javascript或jquery显示图像预览 - How to show Image Preview using javascript or jquery in Internet Explorer 如何使用Internet Explorer中的复选框隐藏和取消隐藏Javascript和CSS - How to hide & unhide with Javascript & CSS using a checkbox in Internet Explorer 如何使用JavaScript在Internet Explorer 9中定位div? - How do you position a div in Internet Explorer 9 using JavaScript? 如何检测是否已使用JavaScript安装Internet Explorer插件 - How to detect if an Internet Explorer plugin is already installed using JavaScript 如何使用javascript或php禁用Internet Explorer缓存 - How to disable internet explorer cache using javascript or php 如何使用Javascript在Internet Explorer中禁用“ F4”快捷键? - How to disable “F4” shortcut key in internet explorer using Javascript? 如何使用javascript为Internet Explorer获取鼠标指针位置? - How to get mouse pointer position using javascript for internet explorer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM