Insert return records from stored procedure - SQL - sql

The script below return a select records:
EXEC [LINK_SERV].[DB_SAMPLE].[dbo].[SP_SAMPLE] '1235123'
The I want to insert the return records to a temp table so I wrote the script below (assumed temp table is already created):
INSERT INTO #TempTable
EXEC [LINK_SERV].[DB_SAMPLE].[dbo].[SP_SAMPLE] '1235123'
But I get this error:
OLE DB provider "SQLNCLI11" for linked server "LINK_SERV" returned message "The partner transaction manager has disabled its support for remote/network transactions.
Msg 7391, Level 16, State 2, Line 25
The operation could not be performed because OLE DB provider "SQLNCLI11" for linked server "LINK_SERV" was unable to begin a distributed transaction.
Please advise the specific config to enable this. I tried the same code in some other server and it work. Thank you in advance.

This is where the beauty of OPENQUERY() comes into great use.
i.e. something like this...
INSERT INTO dbo.MyTable2
SELECT Col1, Col2, etc
FROM dbo.MyTable1
LEFT JOIN OPENQUERY(<<LINKEDSERVER>>,
'SELECT BLAHBLAH
FROM dbo.BLAHBLAH WHERE <something> = <something>);

Related

Get list of SQL server databases which files have been deleted

My goal is getting list of SQL server databases which files have been deleted.
In other words. I attach a db from mount, then close the mount so actually I have the attached db without files.
At first it seemed to be easy. Just pretty easy select:
SELECT
'DB_NAME' = db.name,
'FILE_NAME' = mf.name,
'FILE_TYPE' = mf.type_desc,
'FILE_PATH' = mf.physical_name
FROM
sys.databases db
INNER JOIN sys.master_files mf
ON db.database_id = mf.database_id
WHERE
--and specific condition here
But it turned out differently. Sql server has almost the same information about a regular database and a database which doesn't have files. So I had to try something else.
Further I tried to use state of database. And it was quite strange.
Unfortunately the following query gives me wrong(or not actual information):
SELECT state
FROM sys.databases
WHERE name = N'TestDB'
state
-----
0
And 0 means ONLINE according to this link
But actually the database has RECOVERY_PENDING state. It looks like that sql server information about my TestDB us out of date and should be refreshed. But I have no idea how to achieve this. But after executing any of following query this info(db state) is being refreshed:
EXEC sp_helpdb N'TestDB'
ALTER DATABASE N'TestDB' SET SINGLE_USER WITH ROLLBACK IMMEDIATE
USE N'TestDB'
--etc
--all requests are terminated with the same error
Msg 5120, Level 16, State 101, Line 10
Unable to open the physical file "C:\MOUNT\b4c059e8-3ba6-425f-9a2a-f1713e7719ca\TestDB.mdf". Operating system error 3: "3(The system cannot find the path specified.)".
File activation failure. The physical file name "C:\MOUNT\b4c059e8-3ba6-425f-9a2a-f1713e7719ca\TestDB_log.ldf" may be incorrect.
File activation failure. The physical file name "C:\MOUNT\b4c059e8-3ba6-425f-9a2a-f1713e7719ca\TestDB_log-2.ldf" may be incorrect.
Msg 5181, Level 16, State 5, Line 10
Could not restart database "TestDB". Reverting to the previous status.
Msg 5069, Level 16, State 1, Line 10
ALTER DATABASE statement failed.
So do you have any idea ?
And also I've asked the question looks like this here differently.
Finally, I've found what i actually need.
I can chech whether the specific file exists or not by sql server:
CREATE FUNCTION dbo.fn_FileExists(#path varchar(512))
RETURNS BIT
AS
BEGIN
DECLARE #result INT
EXEC master.dbo.xp_fileexist #path, #result OUTPUT
RETURN cast(#result as bit)
END;
GO
So i just need to execute the function above for each file which i can get by executing for example following query:
SELECT
DISTINCT 'FILE_PATH' = physical_name
FROM sys.master_files

Informix - error -387 when executing a procedure on remote DB

I have a problem when executing a procedure on remote database with DBA user (informix). The error which I receive is -387 No connect permission. I write down very simple procedure just to show you the problem. The procedure does insert into select from a view on a remote DB. When I execute the SQL from the procedure in command line or in DB client I don't have any problems, when I run the procedure the error is thrown.
Here is an example to be more clear:
Database1:
create table delme (aa int);
insert into delme values (1);
create view delme_view as select * from delme;
Database2:
create table bbb (aa int);
CREATE procedure delme_test()
begin
insert into bbb
select *
from Database1:delme_view;
end
end Procedure;
When I execute procedure delme_test() on Database2, the error -387 is return, when I execute the SQL only through DB Client (connected to Database2)
insert into bbb
select *
from Database1:delme_view;
The insert completes successfully.
Do you have any idea what could be the problem and how can I fix it?
The version of Informix is: IBM Informix Dynamic Server Version 12.10.FC4W1
Both databases are on the same server and have DBA user informix.

SQL Server : Remote Table insert fails

Here is my setting: within my schema I have a stored procedure (shown below) which will insert a dummy row (for testing) into a remote table. The table MY_REMOTE_TABLE is a synonym pointing to the correct remote table.
I can successfully query it like
SELECT * FROM MY_REMOTE_TABLE;
on my local system. This does work.
So for testing I created the procedure with a dummy test value in it, which should be inserted into the remote table if the row is new to the remote table. Hint: the remote table is empty at the time of insertion, so it really should perform the insert.
But it always fails giving me a return value of -6. I have no clue what -6 stands for or even what the error could be. All I have is -6 and I know that nothing will be inserted into the remote table. Interestingly when I copy the insert statement to the remote server, replace the synonym with the real table name on the remote machine and execute, it works all fine.
So I'm really lost here seeking for your help!
CREATE PROCEDURE [dbo].[my_Procedure]
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
BEGIN DISTRIBUTED TRANSACTION
-- LEFT JOIN AND WHERE NULL WILL ONLY FIND NEW RECORDS -> THEREFORE INSERT INTO TARGET REMOTE TABLE
INSERT INTO MY_REMOTE_TABLE
(--id is not needed because it's an IDENTITY column
user_id,
customer_id,
my_value, year,
Import_Date, Import_By, Change_Date, Change_By)
SELECT
Source.user_id,
Source.customer_id,
Source.my_value,
Source.year,
Source.Import_Date,
Source.Import_By,
Source.Change_Date,
Source.Change_By
FROM
(SELECT
null as id,
126616 as user_id,
17 as customer_id,
0 as my_value,
2012 as year,
GETDATE() AS Import_Date,
'test' AS Import_By,
GETDATE() AS Change_Date,
'test' AS Change_By) AS Source
LEFT JOIN
MY_REMOTE_TABLE AS Target ON Target.id = Source.id
AND Target.user_id = Source.user_id
AND Target.customer_id = Source.customer_id
AND Target.year = Source.year
WHERE
Target.id IS NULL; -- BECAUSE OF LEFT JOIN NEW RECORDS WILL BE NULL, SO WE ONLY SELECT THEM FOR THE INSERT !!!
IF (##TRANCOUNT > 0 AND XACT_STATE() = 1)
BEGIN
COMMIT TRANSACTION
END
END TRY
BEGIN CATCH
IF (##TRANCOUNT > 0AND XACT_STATE() = -1)
ROLLBACK TRANSACTION
END CATCH;
END
another question related to this one. if my insert would violate an FK constraint on my remote table, how could I manage to promote the error message from the remote DB server to my local procedure to capture it?
Look here: http://msdn.microsoft.com/de-de/library/ms188792.aspx
Short version:
XACT_ABORT must be set ON for data modification statements in an
implicit or explicit transaction against most OLE DB providers,
including SQL Server. The only case where this option is not required
is if the provider supports nested transactions.
So insert a SET XACT_ABORT ON at the start of the stored procedure.

How to use OpenRowSet to insert data into a blank file?

How to use OpenRowSet to insert data into a blank file?
I need to insert into a txt file (say to D:\TDB) some select output (say select * from sys.tables) from the database
INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Text;Database=D:\TDB;HDR=Yes;', 'SELECT * FROM sys.tables.txt')
select * from sys.tables;
I get
OLE DB provider "MICROSOFT.JET.OLEDB.4.0" for linked server "(null)"
returned message "The Microsoft Jet database engine could not find the
object 'sys.tables.txt'. Make sure the object exists and that you spell its
name and the path name correctly.".
Msg 7350, Level 16, State 2, Line
1 Cannot get the column information from OLE DB provider
"MICROSOFT.JET.OLEDB.4.0" for linked server "(null)".
What is wrong?
PS. please do not propose the bcp solution, cause already tested and does not work everytime, so I would test openrowset now..
#serhio , I tested your sql below:
INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Text;Database=D:\TDB;HDR=Yes;', 'SELECT * FROM sys.tables.txt')
select * from sys.tables;
I got a few test results
The filename should not include "." in it.
(sys.tables.txt→systables.txt)
HDR(Header Row) can not be used here.(Remove it)
The txt file must exist.(Create it)
The first line in the txt file should be the all column name of your
source data.
sql
INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Text;Database=D:\TDB;', 'SELECT * FROM systables.txt')
select * from sys.tables;
systables.txt
name,object_id,principal_id,schema_id,parent_object_id,type,type_desc,create_date,modify_date,is_ms_shipped,is_published,is_schema_published,lob_data_space_id,filestream_data_space_id,max_column_id_used,lock_on_bulk_load,uses_ansi_nulls,is_replicated,has_replication_filter,is_merge_published,is_sync_tran_subscribed,has_unchecked_assembly_data,text_in_row_limit,large_value_types_out_of_row,is_tracked_by_cdc,lock_escalation,lock_escalation_desc

Using 4-part naming to do insert to DB2 from SQL Server 2005?

We're using DB2 9.1 on Linux and SQL Server 2005, and the IBM db2 odbc driver. We have a linked server set up to DB2. From SQL Server, I can do the following:
-- I can create the new table fine
exec ('create table dev.TestSylviaB (field1 int) in TS_DEV_USER_XXXX')
at LinkDB2
-- I can use 4 part naming to select from it, as long as I skip the second part and put everything in upper case
select * from LINKDB2..DEV.TESTSYLVIAB
-- I can insert using Exec (which does pass through)
exec ('insert into DEV.TestSylviaB (field1 ) values (1)') at LinkDB2
-- HOWEVER I cannot insert via the standard 4 part naming, like this:
insert into LINKDB2..DEV.TESTSYLVIAB values (1)
I get a "driver not capable" error message:
OLE DB provider "MSDASQL" for linked server "LINKDB2" returned message "[IBM][CLI Driver] CLI0150E Driver not capable. SQLSTATE=S1C00".
Msg 7343, Level 16, State 2, Line 1
The OLE DB provider "MSDASQL" for linked server "LINKDB2" could not INSERT INTO table "[LINKDB2]..[DEV].[TESTSYLVIAB]".
Any thoughts?
thanks!
Sylvia
The 4 part name insert is a distributed transaction and the DB2 driver needs to enroll into it. See Registering the IBM DB2 Driver for ODBC and CLI with the Microsoft DTC.