Re: How do I create a trigger in a stored procedure?



Mark B (none123@xxxxxxxx) writes:
Thanks very much.

DISABLE TRIGGER LanguageValuesTrigger ON DATABASE

UPDATE tblLanguageValues
SET LanguageText = REPLACE(LanguageText,'"', '"')
WHERE (LanguageCode <> 'EN-US')

UPDATE tblLanguageValues
SET LanguageText = REPLACE(LanguageText,'&#39;', char(39))
WHERE (LanguageCode <> 'EN-US');

ENABLE TRIGGER LanguageValuesTrigger ON DATABASE

And this works? Since you say ON DATABASE, that should refer to a
DDL trigger on database level, not a trigger on a table. But maybe
SQL Server let's go away with it.

In any case, you should absolutely have BEGIN/COMMIT TRANSACTION
around this, so that you don't leave the trigger disabled in case of
an accident. That also prevents other users from accessing the table
while the transaction is running. (Or at least I hope it does; I did
not test.)

By the way, do I need to use the word GO after the DISABLE and ENABLE
commands? Seems to compile OK without it. What is the purpose of GO
anyway? And I needed to use the ; before ENABLE to compile. What's the
difference between that and GO and when do I need to use it?

GO and ; are two very different things. In fact, when it comes to SQL
Server alone, "GO" is just another identifier like Foo, Bar or
LanguageText. It's not a command. "GO" is something that is intercepted
by many query tools. When you press F5, SSMS takes the selected area
(or the entire window) and sends it to SQL Server, but not in one piece.
For each GO it finds on a line of its own, it stops and sends what it
has so far to SQL Server. Thus, GO is a batch separator.

This means that if you put a "GO" in the middle of your stored procedure
code, this is the same effect as if you delete all code below this
point, at least with regards to the procedure definition. (The rest of
the code will be executed directly.)

; on the other hand is a statement terminator, just like in many other
languages. The only difference is that in Transact-SQL, the semicolon
is optional in most cases. This is because originally T-SQL did have
semicolons at all. There are a few situations when you need it, and
that is when a statement begins with a keyword that is not "statement
leader".

Microsoft's recommendation is that you should always terminate your
statements with semicolons, and they say that one day, they will be
mandatory in SQL Server. Personally, I can't see how they will ever
be able to this without causing misery and mayhem all over the planet,
and I will have to confess that I don't put in more semicolons in my
SQL code that I actually need to.




--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

.



Relevant Pages

  • Re: How to limited number of rows in a table?
    ... If this is a log table, database blocking should ... Pro SQL Server 2000 Database Design ... > I'll bend on the trigger, but I think a nightly job may be too infrequent ...
    (microsoft.public.sqlserver.programming)
  • Re: Trigger: To fill another Database with using Stored Procedures of the other Database
    ... I fill from Database A with triggers Database B, ... add additional information accordingly, this Stored Procedures is ... trigger does not work anymore, even if I do a try catch over the whole ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ...
    (comp.databases.ms-sqlserver)
  • Re: How do I create a trigger in a stored procedure?
    ... You were right it shouldn't have been ON DATABASE: ... DISABLE TRIGGER LanguageValuesTrigger ON DATABASE ... SQL Server let's go away with it. ...
    (microsoft.public.sqlserver.programming)
  • call to xp_cmdshell from trigger problem
    ... To any SQL Server MVP: ... database that is used to alert and calculate required amounts of assistance ... At first it failed because the trigger called the EXE but the tables were ...
    (microsoft.public.sqlserver.server)
  • Re: how does trigger works?
    ... Improve your validation process and use a trigger. ... Pro SQL Server 2000 Database Design ...
    (microsoft.public.sqlserver.programming)

Loading