SQL Server Msg 2108 - sql-server

I created and altered a trigger and everything worked well, but when I started the SQL Server Management Studio later on, the following error appeared:
Msg 2108, Level 15, State 1, Procedure store_110, Line 43
Cannot create trigger on 'IT_ServerDB.dbo.Users' as the target is not in the current database.

Cannot create trigger on 'IT_ServerDB.dbo.Users' as the target is not in the current database.
It seems ,you are using three part naming like below
'IT_ServerDB.dbo.Users
use two part naming
use IT_ServerDB
go
create trigger triggername
on
and rest of syntax

Related

Can create a table but doesn't display in Object explorer, can't select, or delete table?

Using SQL Server 2016. Have a locally hosted database that uses the Windows login for the sa, which is what I am using to login.
Yesterday I ran
CREATE TABLE [Otis].[AnalyzerGroups]
(
[id] [int] IDENTITY,
[Name] [varchar](50) NULL
);
and got command successfully completed. Today I tried selected from this table but got an error:
Msg 208, Level 16, State 1, Line 1
Invalid object name 'Otis.AnalyzerGroups'
So I thought I misremembered and tried running the create statement again but then got the error -
Msg 15530, Level 16, State 1, Line 1
The object with name "AnalyzerGroups" already exists.
The statement has been terminated.
So then I tried DROP TABLE [Otis].[AnalyzerGroups]; and got
Msg 3701, Level 11, State 5, Line 1
Cannot drop the table 'Otis.AnalyzerGroups', because it does not exist or you do not have permission.
I tried making a new test table and the same thing. The first time I run the create statement command successfully completes, but then I can't select / insert / drop from the table, and I cannot see it in the Object explorer either.
I assume this must be some permissions issue but I don't know what property is keeping me from viewing these tables - its not like I'm putting security on these tables, and I can see every other table in our database. Any thoughts?
You put it most likely in the wrong database. Try this.
sp_MSforeachdb 'SELECT "?" AS DB, * FROM [?].sys.tables WHERE name like
''%AnalyzerGroups%'''
There was a trigger in the SQL Server database that fired any time a new table was created under any schema, and would then transfer it to the dbo schema. So my tables did exist, but they were under the schema I was expecting because of this trigger. Why does this trigger exist? Got me. But it's in production and has been for over a decade so there's some reason/it's definitely not changing. Thanks for the help though everyone.

Trigger cannot be created in Microsoft SQL Server Management Studio

I'm trying to run this trigger that is suppose to update a row in my customers table after a row is updated in the same table.
CREATE TRIGGER [project].updateFreeShipping
ON [project].[customers]
AFTER UPDATE
AS
BEGIN
UPDATE [project].[customers]
SET NextShippingIsFree = 1
WHERE Email IN (SELECT TOP 5 Email FROM dbo.Top10byMoney)
END
It throws the following error:
Msg 8197, Level 16, State 4, Procedure updateFreeShipping, Line 2
The object 'project.customers' does not exist or is invalid for this operation
Screenshot:
This is a clear and classical error that is raised when you execute a statement in another database that the one you usually use. Very often, beginners does not verify the contexteual database and try to create objets in master which is the default one in SSMS. Remember that SQL Server is a multi-database multi-schema RDBMS and does not require any DBlink to execute SQL scripts from one DB to another... So make a great attention to which database you contextually use !
To avoid such trouble, begin your script with the USE statement, like :
USE MyDatabase;
GO
CREATE TRIGGER [project].updateFreeShipping
ON [project].[customers]
AFTER UPDATE
AS
...
I agree to all others comments about the logic of your code which has no sense...

removing an object from Publication database sys.sp_droparticle and sp_dropsubscription

SQL Server Admin is not my forte. So please bear with while I explain this
A SQL Server 2012 cluster is involved in a Change data capture ( CDC ) effort using a 3rd party CDC utility. for it to work replication needs to be turned on, without replication CDC will not work. The CDC taps some 2000+ odd tables from SQL Server in a database Db1. Out of these we found out that some 200+ tables undergo truncate and load as against increments. So we removed those from our CDC lists but since replication is turned on at DB Level we also need to remove these from publication database so that truncates happening to this exception list wont need replication switched off DB level ( aka truncates to these tables and replication can co-exist. As its known, for truncates to happen we need to switch off replication. The code is in prod so replacing truncate by delete is not an option now besides the fact that for billion row tables deletes are going to be expensive & time consuming )
The above is the requirement. So based on that if a better solution can be conceived do let me know
What I tried :
EXEC sys.sp_droparticle #publication = 'pub', #article = 'art', #force_invalidate_snapshot = 1
Error I get
Msg 14013, Level 16, State 1, Procedure sp_MSrepl_droparticle, Line 104 [Batch Start Line 2]
This database is not enabled for publication.
Another SP
DECLARE #subscriber AS sysname;
EXEC sp_dropsubscription #publication = 'AR_PUBLICATION_00010', #article = 'BPA_BRGR_RUL_GRP_R' ,#subscriber=#subscriber
Msg 14013, Level 16, State 1, Procedure sp_MSrepl_dropsubscription, Line 55 [Batch Start Line 1]
This database is not enabled for publication.
But using GUI I am able to uncheck the tables I dont want in that publication. ( right click publication --> properties --> articles --> check /uncheck whatever you want excluded ) . I dont have any subscription just there is a publication.
Whatever code I ran through GUI above I can def. run through T-SQL But I dont know what code was it that was run ? How do I get this done using a scripting approach. I have 200+ tables to deal with and unchecking em 1 by 1 ain't helping
Nearly four years late, but in case it helps anyone... I think you want sp_dropmergearticle not sp_droparticle.
EXEC sys.sp_dropmergearticle #publication = 'pub', #article = 'art', #force_invalidate_snapshot = 1
I was getting an identical error message using sp_droparticle, but sp_dropmergearticle removed the table from the publication and allowed me to delete it.
Whatever code I ran through GUI above I can def. run through T-SQL But I dont know what code was it that was run ? How do I get this done using a scripting approach.
SSMS does not have a special API. Everything it does, it does through TSQL. So use SQL Profiler to watch what SSMS does, and capture the script.

SQL Server : Create Procedure in DB without using [GO]

I want to execute this (simplified) query using node-mssql that executes in SQL Server 2017 fine:
USE [Journal]
[GO]
CREATE PROCEDURE [dbo].[EventDelete]
#NotificationID INT
AS
DELETE Notification
WHERE NotificationID = #NotificationID
[GO]
node-mssql declares syntax error using [GO] and requires semicolon, therefore I try this:
USE [Journal];
CREATE PROCEDURE [dbo].[EventDelete]
#NotificationID INT
AS
DELETE Notification
WHERE NotificationID = #NotificationID;
Now we get error:
CREATE/ALTER PROCEDURE' must be the first statement in a query batch.
So let's try this:
CREATE PROCEDURE [Journal].[dbo].[EventDelete]
#NotificationID INT
AS
DELETE Notification
WHERE NotificationID = #NotificationID;
Now we get
RequestError: 'CREATE/ALTER PROCEDURE' does not allow specifying the database name as a prefix to the object name.
Naturally without any DB declaration it attempts to attach to the master error:
CREATE PROCEDURE permission denied in database 'master'.
So writing the question really works at setting one's thoughts straight.
The reason is the stored procedure requires to be created in one batch, which [GO] signifies, with nothing else.
Execute USE [Journal] as one batch using the .batch('USE [Journal]') method and then the SQL to CREATE PROCEDUCE as a new .batch(...) execution, sequentially.
Unless there is another method within node-mssql which allows for multi-batch executions?

MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT on SQL Server 2014 CTP1

I am trying to edit this option with the following script:
ALTER DATABASE RIServer
SET MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT ON;
GO
As far as I can tell, the syntax is correct (per msdn). However, I get the following:
Msg 102, Level 15, State 6, Line 9
Incorrect syntax near 'MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT'.
Am I doing something wrong? Is this setting not available on CTP1? Do I need to install CTP2?
I believe that option was added between CTP1 and CTP2.
YOU SHOULD NO LONGER BE USING CTP1. Yes, you need to install CTP2.

Resources