Unable to find where triggers are stored in sql server 2008 - sql-server

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.

Related

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.

Where does a Server trigger save in 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

Moving all non-clustered indexes to another filegroup in SQL Server

In SQL Server 2008, I want to move ALL non-clustered indexes in a DB to a secondary filegroup. What's the easiest way to do this?
Run this updated script to create a stored procedure called MoveIndexToFileGroup. This procedure moves all the non-clustered indexes on a table to a specified file group. It even supports the INCLUDE columns that some other scripts do not. In addition, it will not rebuild or move an index that is already on the desired file group. Once you've created the procedure, call it like this:
EXEC MoveIndexToFileGroup #DBName = '<your database name>',
#SchemaName = '<schema name that defaults to dbo>',
#ObjectNameList = '<a table or list of tables>',
#IndexName = '<an index or NULL for all of them>',
#FileGroupName = '<the target file group>';
To create a script that will run this for each table in your database, switch your query output to text, and run this:
SELECT 'EXEC MoveIndexToFileGroup '''
+TABLE_CATALOG+''','''
+TABLE_SCHEMA+''','''
+TABLE_NAME+''',NULL,''the target file group'';'
+char(13)+char(10)
+'GO'+char(13)+char(10)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
ORDER BY TABLE_SCHEMA, TABLE_NAME;
Please refer to the original blog for more details. I did not write this procedure, but updated it according to the blog's responses and confirmed it works on both SQL Server 2005 and 2008.
Updates
#psteffek modified the script to work on SQL Server 2012. I merged his changes.
The procedure fails when your table has the IGNORE_DUP_KEY option on. No fix for this yet.
#srutzky pointed out the procedure does not guarantee to preserve the order of an index and made suggestions on how to fix it. I updated the procedure accordingly.
ojiNY noted the procedure left out index filters (for compatibility with SQL 2005). Per his suggestion, I added them back in.
Script them, change the ON clause, drop them, re-run the new script. There is no alternative really.
Luckily, there are scripts on the Interwebs such as this one that will deal with scripting for you.
Update: This thing will take long time to do step 2 manually if you are using MS SQL Server manager 2008R2 or earlier. I used sql server manager 2014, so it works well (because the way it export the drop and create index is easy to modify)
I tried to run script in SQL server 2014 and got some issue, I'm too lazy to detect the problems, SO I come up with another solution that not depend on the version of SQL server you are running.
Export your index (with drop and create)
2.Update your script, remove all things related to drop create tables, keep the thing belong to indexs. and Replace your original index with the new index (in my case, I replace ON [PRIMARY] by ON [SECONDARY]
[]5
Run script! And wait until it done.
(You may want to save the script to run in some others environment).

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

How to disable CDC for a table manually?

I dropped a table before disabling CDC for that. Now when I recreated the table and tried enabling CDC it says that capture instance already exists. I can use a different Capture Instance name but need to know if there is anyway to drop the associated capture instance manually.
When I delete a table through SSMS GUI it drops CDC tables too. But this time I dropped the table using code and it didn't disable or remove CDC. Hence the trouble. Ms documentation talks about a hot fix if Change Table are removed by mistake. But I have removed the base table. Any clues on how to remove this capture instance for the dropped table?
Here are the steps I took to remove an orphaned capture instance in CDC:
DROP FUNCTION [cdc].[fn_cdc_get_net_changes_dbo_(tablename)]
DROP FUNCTION [cdc].[fn_cdc_get_all_changes_dbo_(tablename)]
Then run the following:
declare #objid int
set #objid = (select object_id from cdc.change_tables where capture_instance = 'your orphaned capture instance')
delete from cdc.index_columns where object_id = #objid
delete from cdc.captured_columns where object_id = #objid
delete from cdc.change_tables where object_id = #objid
At that point you should be able to re-create your capture instance via sp_cdc_enable_table as normal.
Well I figured out a way. I removed all the records related to that table from all CDC system tables and tried recreating the capture instance with same name. It worked!
I had to execute one more step in addition to the REPLY by pdanke:
DROP TABLE cdc.<capture_insance>_CT
My cdc orphan may have come about when I restored a database where change data capture had been enabled. In my case,
EXECUTE sys.sp_cdc_help_change_data_capture
resulted in one entry where source_schema and source_table were both NULL.
It's Simple
Just use the following Script
EXEC sys.sp_cdc_disable_table 'schema_name','Source_Table_Name','CDC_Table_Name'

Resources