AFTER INSERT trigger on local table inserting to Azure table fails - sql

I have a really simple trigger on a SQL table in a local database. The trigger looks something like this:
ALTER TRIGGER [dbo].[trg_UpdateAzureDB]
ON [dbo].[my_local_table]
AFTER INSERT,DELETE,UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for trigger here
INSERT INTO [myazuresvr].[myazuredb].[dbo].[myazuretable]([ImageId], [PimObjectId], [Relation], [ObjectType])
SELECT * FROM inserted
END
So, as you can see what I want to do is push the data that has been inserted into an Azure SQL database table. However, when I insert to the table, I get the following error:
Msg 7399, Level 16, State 1, Procedure trg_UpdateAzureDB, Line 16 [Batch Start Line 10]
The OLE DB provider "SQLNCLI11" for linked server "myazuresvr" reported an error. One or more arguments were reported invalid by the provider.
Msg 7391, Level 16, State 2, Procedure trg_UpdateAzureDB, Line 16 [Batch Start Line 10]
The operation could not be performed because OLE DB provider "SQLNCLI11" for linked server "myazuresvr" was unable to begin a distributed transaction.
The Server is defined as a linked server within the local server.
The really weird thing is that if I do:
INSERT INTO [myazuresvr].[myazuredb].[dbo].[myazuretable]([ImageId], [PimObjectId], [Relation], [ObjectType])
SELECT * FROM [mylocaltable]
This completes fine. It would appear to be something to do with the fact I am inserting into an Azure table from within a trigger...
Local SQL version is 12.0.6108
Azure version shows as 12.0.2000
Any suggestions?

The error message says it's related to distributed transaction. Setting "Enable promotion of distributed transaction" flag to false should work.

Related

SQL server Find/delete hidden trigger

So I was playing around with triggers and stored procedures.
For the life of me I cannot find or delete this trigger I setup.
This trigger now runs on any table i create. The message I get is
Msg 2812, Level 16, State 62, Procedure tr_test, Line 6 Could not find
stored procedure 'sp_test'.
I cannot find tr_test for the life of me. I try and drop the trigger and it says it doesn't exist or I don't have permissions. I am signed in with SA.
Msg 3701, Level 11, State 5, Line 4 Cannot drop the trigger 'tr_test',
because it does not exist or you do not have permission.
I've ran several queries to see if i can figure out where this trigger may be but none have returned any results.
select * from sysobjects where xtype = 'TR'
I've even restarted the server thinking it may be held in memory etc.
Any idea how I can find this hidden trigger.
I am guessing this in reference to your recent question about creating ddl triggers. You have to use a slightly different syntax to drop ddl triggers. https://msdn.microsoft.com/en-us/library/ms173497.aspx

Database Recovery Model alter script error

I am trying to deploy one of our server via teamcity (db project in VS 2012), i am getting an error on Staging server
Error SQL72014: .Net SqlClient Data Provider:
Msg 5069, Level 16, State 1, Line 5
ALTER DATABASE statement failed.
Error SQL72045: Script execution error.
The executed script:
IF EXISTS (SELECT 1
FROM [master].[dbo].[sysdatabases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
ALTER DATABASE [$(DatabaseName)]
SET RECOVERY SIMPLE
WITH ROLLBACK IMMEDIATE;
END
Any ideas are appreciated.
Don't just gloss over the problem: understand it.
It's because you have some sort of HA (you said mirroring in the comment above, but availability groups (for sure) or log shipping (I think) would also cause this) that requires that the database be in full recovery. When you try to set it to simple recovery, it fails because you can't set the recovery model to anything but full for such a database. You could guard against this by checking what environment you're deploying to from VS and act accordingly.

Error while enabling CDC on table level

I am enabling Change data capture (CDC) on SQL server 2012 Enterprise edition(11.0.2100.60)
. I am able to enable it on Database level with below SQL, but failed to enable on Table level.
Use DatabaseName
GO
Exec sys.sp_cdc_enable_db
GO
EXEC sys.sp_cdc_enable_table #source_schema = N'dbo'
,#source_name = N'TableName'
, #role_name = NULL
GO
Got Error like,
'msg 22832, Level 16, State 1, Procedure sp_cdc_enable_table_internal,
Line 623
Could not update the metadata that indicates table [dbo].[TableName]
is enabled for Change Data Capture. The failure occurred when
executing the command '[sys].[sp_cdc_add_job] #job_type = N'capture''.
The error returned was 22836: 'Could not update the metadata for
database DatabaseName to indicate that a Change Data Capture job has
been added. The failure occurred when executing the command
'sp_add_jobstep_internal'. The error returned was 14234: 'The
specified '#server' is invalid (valid values are returned by
sp_helpserver).'. Use the action and error to determine the cause of
the failure and resubmit the request.'. Use the action and error to
determine the cause of the failure and resubmit the request.'
Would anyone help me to out of this?
Thanks in advance..!!
I had the same problem recently.
In my opinion, it is highly probable that the name of the computer has changed.
If it is true, read on ...
Name Changing the computer is automatically recognized by SQL. However, system metadata must be updated manually.
(Source: http://msdn.microsoft.com/en-us/library/ms143799.aspx)
SQL Statement
sp_dropserver old_name\instancename;
GO
sp_addserver new_name\instancename, local;
GO
Example
sp_dropserver name-PC\SQLEXPRESS;
GO
sp_addserver CLT55\SQLEXPRESS, local;
GO
Reference problem and solution

SQL Server 2012 using SELECT in trigger breaks table

So let me first admit that I am a SQL Server newbie.
Here's the deal: I'm trying to create a trigger on a table in SQL Server 2012, and whenever I try any kind of SELECT statement in the trigger, the table quits working (as in NOTHING can be inserted until the trigger is deleted). As soon as I drop the trigger, everything starts working again. If I don't do any SELECTs, everything is peachy. Is there a permission or something somewhere that I'm missing?
Example:
CREATE TRIGGER sometrigger
ON sometable
FOR INSERT
AS
BEGIN
SELECT * FROM inserted
END
GO
Command completes successfully, but the table becomes frozen as described above.
CREATE TRIGGER sometrigger
ON sometable
FOR INSERT
AS
BEGIN
EXEC msdb.dbo.sp_send_dbmail
#recipients = N'someaddress#somedomain.com',
#subject = 'test',
#body = 'test body',
#profile_name = 'someprofile'
END
GO
Works like a charm.
You're may be falling foul of the disallow results from triggers option being set to 1, as it should be.
Note the warning on that page:
This feature will be removed in the next version of Microsoft SQL Server. Do not use this feature in new development work, and modify applications that currently use this feature as soon as possible. We recommend that you set this value to 1.
I suspect that wherever you're running your inserts from is hiding an error message or exception, since you should get:
Msg 524, Level 16, State 1, Procedure , Line
"A trigger returned a resultset and the server option 'disallow_results_from_triggers' is true."
Or, in the alternative, you're working with a database layer that wraps all inserts in a transaction and will roll the transaction back if anything unexpected happens - such as receiving a result set or even just an extra information message saying (x rows affected).
But all of this is dancing around the main issue - you shouldn't be issuing a select that attempts to return results from inside of a trigger. I might have been able to offer more help if you'd actually told us what you're trying to achieve.
If it's the second case, and it's something tripping over the (x rows affected) messages, that can be cured by placing SET NOCOUNT ON at the top of the trigger.
You should never return data from a trigger anyway, mainly for simplicity and maintenance reasons. It's confusing: I did an INSERT but get a resultset back.
If you need to get the values you just inserted, you'd use the OUTPUT clause
INSERT sometable (...)
OUTPUT INSERTED.*
VALUES (...);
This at least tells you that the INSERT gives results.
And it is nestable too as per, say, SQL Server concurrent transaction issue

Call update Stored procedure in SQL Linked server?

I have SQL server and It has another SQL server Linked server [DEV]. Now I want call a stored procedure in Linked server and get the result value. It is always giving error. I tried below both statements none of the did not worked.
ALTER PROCEDURE [dbo].[UpdateMemebership_Lock_Count]
#v_constit_id_in as varchar(100)
AS
BEGIN
Exec ('Call [Members_Directory_Search_DEV].[dbo].[update_dirsearch_notes]
(?)',#v_constit_id_in) AT [DEV]
--Exec [DEV]..[Members_Directory_Search_DEV].[update_dirsearch_notes]
#v_constit_id_in
END
Error
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '.'.
Not sure what syntax you are used to using (CALL is used for Analysis Services, not SQL Server), but linked servers have a four part notation:
ServerName.DatabaseName.owner.object
Try:
Exec [DEV].[Members_Directory_Search_DEV].dbo.[update_dirsearch_notes] #v_constit_id_in