I have a strange problem here, I want to create an emtpy trigger:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER dbo.TestTrigger
ON _TestDB.dbo.test
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
END
GO
If I execute this within SSMS it works perfectly, but if I create a new SQL file within Visual Studio and click “Execute SQL”, then I just get an error message:
Msg 2108, Level 15, State 1, Procedure TestTrigger, Line 13 Erstellen
Trigger kann nicht für ‘_TestDB.dbo.test’ ausgeführt werden, da sich
das Ziel nicht in der aktuellen Datenbank befindet.
Translation:
Msg 2108, Level 15, State 1, Procedure TestTrigger, Line 13 Create
Trigger cannot be executed for ‘_TestDB.dbo.test’, because
the target doesn’t exist in the current database.
SSMS and VS2010 are both connected to the same databaseserver as the same user.
A simple Select * From _TestDB.dbo.test does work within Visual Studio, so the connection to the database should work. But why is it not working for Create Trigger?
As the error implies, triggers must be created in the same database as the underlying target table.
So ensure that you create the trigger in the same DB (_TestDB) as your dbo.test table
Edit : Just to clarify – a connection is at server instance level, not at database level. By qualifying your table as
_TestDB.dbo.testyou will be able to access the table even if your current database (catalogue) is currently pointing to a different database.Edit : OIC – yes, it means that your current db isn’t “_TestDB”. You’ve also picked up an inconsistency in SQL’s DDL execution. Most of the DDL commands can be successfully executed from a remote database (Including CREATE INDEX), but for some reason, not a trigger. The following highlights the inconsistency (SQL 2008 Express)