简体   繁体   English

SQL插入-来自不同的服务器和凭据

[英]SQL insert into - from different server and credentials

We have one database existing on server A. Server A also hosts our program code which will be calling the SQL statement. 我们在服务器A上存在一个数据库。服务器A还托管我们的程序代码,该程序代码将调用SQL语句。

We have another database VMIntranetTest existing on server B VMC-MMS 我们在服务器B VMC-MMS上存在另一个数据库VMIntranetTest

Server A and server B have different logon user credentials. 服务器A和服务器B具有不同的登录用户凭据。 Server A and server B both exist on our internal network. 服务器A和服务器B都存在于我们的内部网络中。

Using PHP, I have the following SQL statement defined. 使用PHP,我定义了以下SQL语句。

$strSql = 'INSERT INTO VMC-MMS.VMIntranetTest.dbo.TestTable (FirstName, LastName, Age) ' .
                    'SELECT FNAME, LNAME, AGE ' .
                    'FROM BSLIB.SQLTSTF ';

FROM -> BSLIB.SQLTSTF <- is on our local server (A), so my connection string used to execute the statement will have the user credentials to connect to server A. FROM -> BSLIB.SQLTSTF <-位于我们的本地服务器(A)上,因此用于执行该语句的连接字符串将具有连接到服务器A的用户凭据。

INSERT INTO -> VMC-MMS.VMIntranetTest.dbo.TestTable <- is the different server.database.dbo.table (Server B). INSERT INTO -> VMC-MMS.VMIntranetTest.dbo.TestTable <-是不同的server.database.dbo.table(服务器B)。

How do I specify the user credentials to be used for the INSERT INTO portion of the statement? 如何指定用于语句的INSERT INTO部分的用户凭据? The secondary portion containing the SELECT FROM statement should already be covered by my initial connection string. 包含SELECT FROM语句的第二部分应该已经包含在我的初始连接字符串中。

Thank you, 谢谢,

Edit 1 in regards to Paul's answer. 关于保罗的答案, 编辑1

I've attempted to use the OPENROWSET as mentioned, and have the following SQL statement. 我尝试使用如上所述的OPENROWSET ,并使用以下SQL语句。

INSERT INTO VMIntranetTest.TestTable (FirstName, LastName, Age)
OPENROWSET('vmas400',
           'Server=192.168.1.2;Trusted_Connection=yes;user_id=INTRAIS;password=****',
           'SELECT FNAME, LNAME, AGE FROM BSLIB.SQLTSTF' ) as a 

As you can see, I changed things around a little bit. 如您所见,我稍微改变了一些地方。 My connection string through the code opens the connection to Server B "VMC-MMS". 我通过代码的连接字符串打开了与服务器B“ VMC-MMS”的连接。 My SQL statement "select" portion, uses the OPENROWSET to open a connection to Server A "192.168.1.2". 我的SQL语句“选择”部分使用OPENROWSET打开到服务器A“ 192.168.1.2”的连接。

However, I am getting this error message: 但是,我收到此错误消息:

SQLSTATE[HY000]: General error: 1 near "OPENROWSET": syntax error SQLSTATE [HY000]:常规错误:“ OPENROWSET”附近为1:语法错误

Edit 2 i needed to put the entire OPENROWSET portion inside a VALUES ( ) clause. 编辑2我需要将整个OPENROWSET部分放在VALUES()子句中。 Now I'm getting a message: 现在我收到一条消息:

SQLSTATE[HY000]: General error: 1 no such table: VMIntranetTest.TestTable SQLSTATE [HY000]:常规错误:1没有这样的表:VMIntranetTest.TestTable

Edit 3 编辑3

I've now got the following SQL 我现在有以下SQL

 INSERT INTO VMIntranetTest.TestTable (FirstName, LastName, Age)
 select a.FNAME, a.LNAME, a.AGE FROM 
 OPENROWSET('vmas400',
            'Server=192.168.1.2;Trusted_Connection=yes;user_id=INTRAIS;password=****',
            'SELECT FNAME, LNAME, AGE FROM BSLIB.SQLTSTF' ) as a

And am getting this error: 并收到此错误:

SQLSTATE[HY000]: General error: 1 near "(": syntax error SQLSTATE [HY000]:常规错误:“(”附近的1:语法错误

You just need the SELECT statement in there. 您只需要其中的SELECT语句。 Try this: 尝试这个:

INSERT INTO VMIntranetTest.dbo.TestTable (a.FirstName, a.LastName, a.Age)
SELECT a.FirstName, a.LastName, a.Age FROM
OPENROWSET('vmas400',
           'Server=192.168.1.2;Trusted_Connection=yes;user_id=INTRAIS;password=****',
           'SELECT FNAME, LNAME, AGE FROM BSLIB.dbo.SQLTSTF' ) as a

EDIT: Try the query now. 编辑:现在尝试查询。 You had the table specified from the database without specifying the schema. 您已从数据库中指定了表,但未指定架构。 If you have appropriate permissions, the above query will now work. 如果您具有适当的权限,上面的查询现在将起作用。 Otherwise, you will need to specify the schema. 否则,您将需要指定架构。 Sorry for not catching that! 对不起,没发现!

You could use OPENROWSET to select data from the different database 您可以使用OPENROWSET从其他数据库中选择数据

http://msdn.microsoft.com/en-us/library/ms190312.aspx http://msdn.microsoft.com/en-us/library/ms190312.aspx

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM