Where does a Server trigger save in SQL Server? - sql-server

A couple of days ago , I was practicing and I wrote some triggers like these :
create trigger trg_preventionDrop
on all server
for drop_database
as
print 'Can not Drop Database'
rollback tran
go
create trigger trg_preventDeleteTable
on database
for drop_table
as
print 'you can not delete any table'
rollback tran
But the problem is I don't know where it has saved and How can I delete or edit those.
Thank you

Server Trigger
You can see them here
select * from sys.server_triggers
To drop use this syntax
drop trigger trg_preventionDrop on all server
In Management Studio they are under the "Server Objects" -> "Triggers" node
Database Trigger
You can see them here
select * from yourdb.sys.triggers
To drop use this syntax
drop trigger trg_preventDeleteTable on database
In Management Studio they are under the "Databases" -> "yourdb" -> "Programmability" -> "Database Triggers" node

trigger on a particular table resides in "DataBase" -> "YourDb" -> "YourTable" -> "Trigger" folder in Management Studio
also one can find the trigger on a particular table by execution the following sql:
EXEC sp_helptrigger yourtablename

Related

SQL Server 2008 R2: 'CREATE/ALTER VIEW' does not allow specifying the database name as a prefix to the object name

I have to 2 databases DB1 and DB2.
I have a view called View1 which is stored in the database DB2. Now I want to ALTER VIEW from DB1 database.
My attempt:
ALTER VIEW DB2..View1
AS
SELECT * FROM DB2..Test;
But I'm getting an error:
'CREATE/ALTER VIEW' does not allow specifying the database name as a prefix to the object name
It is really simple: you need to change the database
USE DB2
GO
ALTER VIEW View1
...
In my case I'm trying to run a script to create/alter a view in different database, so I'm using EXEC (...) to create my views.. But I hit a bit of a paradox:
EXEC ('CREATE VIEW...') will not let you specify the database. You have to switch to that database to create the view.
But you can't do EXEC ('USE [db]; CREATE VIEW...') as CREATE VIEW will demand it be the first command.
I got around this problem feeling like I went Inception:
        EXEC('USE [db]; EXEC('CREATE VIEW'))
Comments are self explanatory. You need to be working in the DataBase your view is for/from. Switch your connection to DB2 and you should be able to CREATE and ALTER a/your view. From MSDN
I was connected to the same database server:DB1 in Azure Data Studio where I wanted the view, and getting the error from this:
CREATE OR ALTER VIEW DB1.dbo.my_View AS
SELECT ...
I removed the prefix and this worked:
CREATE OR ALTER VIEW my_View AS
SELECT ...

SQL Server Trigger - Need to Alter

I need to alter a trigger in SQL Server. After I am doing, do I just execute the trigger similar to how I would do for a Stored Procedure?
ALTER TRIGGER
Yes, that is right, just use ALTER. If you right-click on your trigger in Object Explorer in SSMS and select Script Trigger as/ALTER To, you will see the ALTER statement created for your trigger.
ALTER TRIGGER triggerName
ON tableName
FOR INSERT -- or update & delete
AS
-- sql here
http://msdn.microsoft.com/en-us/library/ms176072.aspx
You don't "execute" a trigger. Triggers are "triggered" at certain points depending upon your definition of them.
For example an AFTER UPDATE trigger would run for all rows updated after you send an UPDATE command to the table on which the trigger is created.

How to add a new schema to sql server 2008?

How do you add a new schema to a database? I am creating a new table and would like to select my own schema from the properties list, but I don't know how to create it. I am using SQL Server Management 2008.
Use the CREATE SCHEMA syntax or, in SSMS, drill down through Databases -> YourDatabaseName -> Security -> Schemas. Right-click on the Schemas folder and select "New Schema..."
Here's a trick to easily check if the schema already exists, and then create it, in it's own batch, to avoid the error message of trying to create a schema when it's not the only command in a batch.
IF NOT EXISTS (SELECT schema_name
FROM information_schema.schemata
WHERE schema_name = 'newSchemaName' )
BEGIN
EXEC sp_executesql N'CREATE SCHEMA NewSchemaName;';
END
I use something like this:
if schema_id('newSchema') is null
exec('create schema newSchema');
The advantage is if you have this code in a long sql-script you can always execute it with the other code, and its short.
Best way to add schema to your existing table: Right click on the specific table-->Design -->
Under the management studio Right sight see the Properties window and select the schema and click it, see the drop down list and select your schema. After the change the schema save it. Then will see it will chage your schema.
You can try this:
use database
go
declare #temp as int
select #temp = count(1) from sys.schemas where name = 'newSchema'
if #temp = 0
begin
exec ('create SCHEMA temporal')
print 'The schema newSchema was created in database'
end
else
print 'The schema newSchema already exists in database'
go
In SQL Server 2016 SSMS expand 'DATABASNAME' > expand 'SECURITY' > expand 'SCHEMA' ; right click 'SCHEMAS' from the popup left click 'NEW SCHEMAS...' add the name on the window that opens and add an owner i.e dbo click 'OK' button

Unable to find where triggers are stored in sql server 2008

I want to delete and modify previously created triggers but i cant find them anywhere in database. Where they exist and how to edit or delele them
You can find Triggers under Table node:
Under the Tables node in SSMS (SQL Server Management Studio), for each table there is a Triggers node.
You can manage your triggers from there.
Here is a better way:
select a.[name] as trgname, b.[name] as [tbname]
from sys.triggers a join sys.tables b on a.parent_id = b.object_id
Just be sure to run it against the database where you think the trigger is located.
You can also find the triggers by querying the management views in SQL Server Management Studio:
SELECT
OBJECT_NAME(object_id) 'Table name', *
FROM
sys.triggers
That gives you a list of all triggers and what table they're defined on for your current database. You can then go on to either disable or drop them.
To expand a little on the previous answers, in all the recent versions of SQL Server you can right click on a trigger and choose: Script Trigger as… ALTER To… "New Query Editor Window"
This will open an SQL script with the details of the trigger, if you read the code you will notice that it includes the ALTER syntax: ALTER TRIGGER [dbo].triggername ...
This means you can edit the SQL and press Execute to alter the trigger - this will overwrite the previous definition.
If the triggers have been built using automated tools, you may find duplicate code in the trigger definition which you will want to remove.
It is worth trying to Execute the script first before trying to edit anything, that will tell you if the trigger definition is valid. If a table or column has been renamed, things can get out of sync.
Similarly to Delete/Drop a trigger completely select: Script Trigger as… DROP To… "New Query Editor Window" and then execute it.

How do i recreate a trigger in SQL Server?

i use the statement drop trigger if exist TRIGGER in sqlite but sql server doesnt like the if statement. (i guess exist is the offending word). I do this right next to my create trigger statement because i want to drop older triggers with the same name so i can replace it with this new one.
How do i do this in SQL server?
in SQL Server Management Studio (and, I think in Query Analyzer) right-click the trigger in the explorer, and choose the Script-as option, choose 'Drop Trigger' in clipboard, and SSMS will create the T-SQL syntax for you to drop that trigger.
Sorry I haven't given you T-SQL you can copy and paste, but this way you'll know how to do it for next time.
You can check for the existence of a specific Trigger like so.
IF EXISTS
(
select name
from sys.objects
where type='TR' and name ='Trigger Name'
)
BEGIN
--Add your Trigger create code here
END
I find this to be a more compact SQL Server equivalent to MySQL's DROP TRIGGER IF EXISTS syntax:
IF OBJECT_ID('XXXX', 'TR') IS NOT NULL
DROP TRIGGER XXXX
I'd use something like:
IF objectproperty(object_id('dbo.xxx'), 'isTrigger') = 1
DROP PROCEDURE dbo.xxx
GO
CREATE TRIGGER dbo.xxx [etc]
replacing xxx with your trigger name (and dbo with the relevant schema, if necessary).
Alternatively, you could just use
ALTER TRIGGER dbo.xxx [etc]
Since version 2016 this syntax is also supported by Microsoft SQL Server:
DROP TRIGGER IF EXISTS trigger_name

Resources