SQL insert into - from different server and credentials - sql-server

We have one database existing on server A. Server A also hosts our program code which will be calling the SQL statement.
We have another database VMIntranetTest existing on server B VMC-MMS
Server A and server B have different logon user credentials. Server A and server B both exist on our internal network.
Using PHP, I have the following SQL statement defined.
$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.
INSERT INTO -> VMC-MMS.VMIntranetTest.dbo.TestTable <- is the different server.database.dbo.table (Server B).
How do I specify the user credentials to be used for the INSERT INTO portion of the statement? The secondary portion containing the SELECT FROM statement should already be covered by my initial connection string.
Thank you,
Edit 1 in regards to Paul's answer.
I've attempted to use the OPENROWSET as mentioned, and have the following SQL statement.
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". My SQL statement "select" portion, uses the OPENROWSET to open a connection to Server A "192.168.1.2".
However, I am getting this error message:
SQLSTATE[HY000]: General error: 1 near "OPENROWSET": syntax error
Edit 2 i needed to put the entire OPENROWSET portion inside a VALUES ( ) clause. Now I'm getting a message:
SQLSTATE[HY000]: General error: 1 no such table: VMIntranetTest.TestTable
Edit 3
I've now got the following 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

You just need the SELECT statement in there. 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
http://msdn.microsoft.com/en-us/library/ms190312.aspx

Related

How do you copy data from a DB on one SQL server to a DB on another SQL server?

I'm not trying to do a straight copy of the data from one server to the other. I have to clean up/rename the source data before inserting it into the destination DB, and check if values exist in various look up tables and insert them if they don't.
Basically something like this:
SELECT EmployeeID, FirstName, LastName, Region
INTO #Temp
FROM server1.db1.dbo.Employees
INSERT INTO server2.db2.dbo.Region
(Region)
SELECT DISTINCT Region FROM #Temp
WHERE Region NOT IN (SELECT Region FROM server2.db2.dbo.Region)
INSERT INTO server2.db2.dbo.Employees
(EmployeeID, FullName, Region)
SELECT EmployeeID, FirstName + ' ' + LastName AS FullName, Region FROM #Temp
DROP TABLE #Temp
However, using a fully qualified name like serverName.DBname.DBTable doesn't seem to work.
I'm using SQL Server 2012.
EDIT -- I don't believe I can get the client to create a linked server as per https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-addlinkedserver-transact-sql?view=sql-server-2017.
If you can't set up a linked server (which I recommend you do), you could use OPENROWSET. This assumes you have an AD account on both instances, with the relevant permissions. You will also need to connect to the instance you want to insert the data into, not the server the data is coming from. This should give you the idea:
USE db2;
GO
INSERT INTO dbo.Employees (EmployeeID, FullName, Region)
SELECT EmployeeID, FirstName + ' ' + LastName AS FullName, Region
FROM OPENROWSET('SQLNCLI','SERVER=server2;Trusted_Connection=yes',
N'SELECT EmployeeID, FirstName, LastName, Region FROM db1.dbo.Employees');
If you don't have an AD login on both, you'll need to use SQL Authentication instead. if that isn't an option, then you'll have to go down a different path entirely.
Another option would be to use SSIS.
Create connection managers to each server
Create a data flow task.
Add sources and destinations to the data flow tasks for each table.
Put your transformation queries into the sources, connected to your source server.
Map your columns in your destinations, connected to your destination server.
Execute the package.
Bob's your uncle.
This also assumes you have rights on both boxes, but you shouldn't need to ask anything of your client to get it done.

MDX permission error when I call a table in SQL Server instance

I am trying to execute this query in MDX
INSERT INTO MINING STRUCTURE [People1]
([CustID], [Name], [Gender], [Age], [CarMake],[CarModel])
OPENQUERY(Chapter3Data,
'SELECT [Key], Name, Gender, Age, CarMake, CarModel
FROM People')
I have a database Chapter3Data with people table in my SQL Server instance, but I get this error :
Executing the query ...
Either the 'Ehsan\ehsan akbar' user does not have permission to access the 'Chapter3Data' object, or the object does not exist.
Execution complete
This is not valid mdx:
SELECT [Key], Name, Gender, Age, CarMake, CarModel
FROM People
You need to specify which axis you would like the hierarchies ON - so generally ROWS or COLUMNS - an example might be:
SELECT
[Key].[Key].MEMBERS ON ROWS,
[Gender].[Gender].MEMBERS ON COLUMS
FROM People;
In SSMS you should be able to open with a connection to the cube server - then you can open an mdx query and test your mdx to ensure it is valid - a good idea before attempting the linked server OPENQUERY function.
The problem is that you should be linking to the name of a Data Source defined on that instance of Analysis Services, not a relational database, or the name of a linked server, or a data source view (DSV). I made all three of those mistakes before finally getting it right, after stumbling on to Raymond Li's answer to the post titled DMX INSERT Openquery Permission Problem at MSDN. It's definitely counterintuitive of Microsoft to implement it this way, since you'd plug in a database or linked server any other time you use OPENQUERY, but that's what works.

Why nested SQL inside OLE DB Source not work?

I'm developing a SSIS package to perform extractions from SQL Server 2008 to an Excel file.
This is my data flow:
"Extraction of concepts" is an OLE DB Source. It executes this SQL statement:
SELECT
Id,
Name,
Surname,
(
SELECT
CI.Interest + '; '
FROM
CustomerInterests CI
WHERE
CI.CustomerId = C.ID
FOR XML PATH ('')
) AS Interest
FROM
Customer C
WHERE Id = ?
When I try to save my query I get this error:
If I modify my SQL Statement like following, the error does not appear:
SELECT
Id,
Name,
Surname,
NULL AS Interest
FROM
Customer C
WHERE Id = ?
Can you help me?
Thanks
In order to achieve what you want, follow the below steps:
1) Add variable in SSIS called ID
2) In your WHERE Statement
Use : WHERE #Id = ?
3) Assign the #ID varible in Data flow with ID SSIS varibale.
Updated:

Migrate data from one db to another

I have two different databases. I have tried using an UPDATE query, but it comes back with an unrecognised error .
How can I reference the DB in the SQL query.
The location is like this for both:
SERVER01\ABC.DB1
And
SERVER01.DB2
EDIT 1:
I have tried
insert into [DB1].[dbo].[table1]
select col1 from [ABC.DB2].[dbo].[table2]
But, I get this error,
Invalid object name 'ABC.DB2.dbo.table2'.
You can specify the database name in the query:
insert into [DbName1].[dbo].[Table1]
select * from [ABC.DB2].[dbo].[Table1]

MSSQL linked server supplied inconsistent metadata for a column

I'm facing a difficult to run 'SUM' syntax from a linked server(PostgreSQL) in MSSQL, the connection created via ODBC driver.
If I only retrieve the data, there isn't any problem,
select * from OpenQuery([192.168.1.145],'
select party_code, amount_forex from biv_so_main
')
If I try to sum the value, it throws an error,
select * from OpenQuery([192.168.1.145],'
select party_code, sum(amount_forex) from biv_so_main group by party_code
')
Error message
The OLE DB provider "MSDASQL" for linked server "192.168.1.145" supplied inconsistent metadata for a column. The column "sum" (compile-time ordinal 2) of object "
select party_code, sum(amount_forex) from biv_so_main group by party_code
" was reported to have a "SCALE" of 6 at compile time and 2 at run time.
I googled a lot, seems nobody has the same issue like me, anybody can help?
Thanks.
add an alias to your sum column and cast into a datatype (into whatever type amount_forex is).
select * from OpenQuery([192.168.1.145],'
select party_code
, sum(amount_forex)::int amount_forex_sum
from biv_so_main group by party_code
')

Resources