Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to create a SQL2K trigger that updates other fields?
Message
General information
Forum:
Visual FoxPro
Category:
Client/server
Miscellaneous
Thread ID:
00713079
Message ID:
00713813
Views:
23
>Sergey must be sleeping, ;) Well, my answer is yes I would like you to send me the code to do that when you can. It also would help me on learning using SQL-DMO.

Here's the code
* Program: GenInsTrigger
* Author: George Tasker
* Date: July 29, 2002 - 3:54 PM
* Purpose: Generates a trigger that
* will insert the current datetime

LPARAMETERS tcServer, tcDatabase, tcTable

LOCAL lcuser, lcpassword,;
  oSQLSvr AS "SQLDMO.SQLServer",;
  lccrlf, lcheader, oDMO AS "SQLDMO.Database",;
  oTable AS "SQLDMO.Table",;
  lnlast, lni, a_fields, lcdeclare,;
  lnoffset, lcdeclare, lcfetch, lcloop,;
  lccursor, lctrigger,;
  oTrigger AS "SQLDMO.Trigger"
lcuser = < username >
lcpassword = < password >
lccrlf = CHR(13) + CHR(10)
* Create the header
lcheader = "CREATE TRIGGER " + "tu_" + tctable + " ON [" + tctable + "]" + lccrlf
lcheader = lcheader + "FOR UPDATE" + lccrlf + "AS" + lccrlf 
lctrigger = lcheader + 'IF NOT update(modifiedDate)' + lccrlf
lctrigger = lctrigger + 'BEGIN' + lccrlf
lctrigger = lctrigger + '  UPDATE ' + tcTable + lccrlf
lctrigger = lctrigger + '  SET modifiedDate = getdate()' + lccrlf
lctrigger = lctrigger + '  FROM ' + tcTable + ', inserted' + lccrlf
lctrigger = lctrigger + '  WHERE ' + tctable + '.idColumn = inserted.idColumn' + lccrlf
lctrigger = lctrigger + 'END'
* Create the SQL Server reference
oSQLSvr = CREATEOBJECT("SQLDMO.SQLServer")
* Connect to the database
oSQLSvr.Connect(tcServer, lcuser, lcpassword)
* Get a reference to the database
oDMO = oSQLSvr.Databases(tcDatabase)
* Get a reference to the table
oTable = oDMO.Tables(tcTable)
* Get a reference to a trigger
oTrigger = CREATEOBJECT('SQLDMO.Trigger')
oTrigger.Name = 'tu_' + tcTable
oTrigger.Text = lctrigger
oTable.Triggers.Add(oTrigger)
* All done, close up
oSQLSvr.Close 
STORE NULL TO oSQLSvr, oTable, oDMO, oTrigger
>I also have another question: I created my database at hand, I mean manually in the enterprise manager as I can't afford a Database design software. But I took the time to comment every table and every field. Is there any way to get that information and have a document of everuthing?
>
Without looking at the docs, I can't say for sure, but I would image that you can access the information via DMO. As I said earlier, just about anything you can do with the Enterprise Manager, you can do with SQL-DMO.
George

Ubi caritas et amor, deus ibi est
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform