sp_settriggerorder [ @triggername = ] '[ triggerschema. ] triggername'
, [ @order = ] 'value(First or Last)'
, [ @stmttype = ] 'statement_type ( Insert or update or delete)'
[ , [ @namespace = ] { 'DATABASE' | 'SERVER' | NULL } ]
Note that after executing the sp_settriggerorder if you edit your trigger you will notice the call to the sp_settriggerorder
store procedure at the bottom of the trigger. This is because the trigger execution sequence can change any time a trigger is Alter
For http://msdn2.microsoft.com/en-us/library/ms186762.aspx
Tuesday, October 23, 2007
define trigger execution order
Monday, October 22, 2007
Update from
You can use the Update from statement to update tables of one table from another
for example you have a table Employee_old_table (employee_id,employee_name,employee_lname)
and e table employee_new_table (employee_id,employee_full_name,employee_first_name,employee_last_name)
to set the employee order.employee_full_name = employee.employee_name + ' ' + employee_lname
you can do this
update employee_full_name
set
employee_full_name = e.employee_name + ' ' + e.empoyee_lname,
employee_first_name = e.employee_name,
employee_last_name = e.employee_lname
from employee e
where e.employee_id = employee_id
instead of
update employee_full_name
set
employee_full_name = (select e.employee_name + ' ' + e.empoyee_lname from employee e where e.employee_id = employee_id),
employee_first_name = (select e.employee_name from employee e where e.employee_id = employee_id),
employee_last_name = (select e.employee_lname from employee e where e.employee_id = employee_id)
for example you have a table Employee_old_table (employee_id,employee_name,employee_lname)
and e table employee_new_table (employee_id,employee_full_name,employee_first_name,employee_last_name)
to set the employee order.employee_full_name = employee.employee_name + ' ' + employee_lname
you can do this
update employee_full_name
set
employee_full_name = e.employee_name + ' ' + e.empoyee_lname,
employee_first_name = e.employee_name,
employee_last_name = e.employee_lname
from employee e
where e.employee_id = employee_id
instead of
update employee_full_name
set
employee_full_name = (select e.employee_name + ' ' + e.empoyee_lname from employee e where e.employee_id = employee_id),
employee_first_name = (select e.employee_name from employee e where e.employee_id = employee_id),
employee_last_name = (select e.employee_lname from employee e where e.employee_id = employee_id)
Tuesday, October 16, 2007
Return a value from a strore procedure value
Store procedures can return value using a return statement in the store procedure
you can do something like this
create PROCEDURE dbo.test1
(
@iID int
)
AS
BEGIN
SET NOCOUNT ON;
Declare @iNewId int
set @iNewId = @iID + 1
return @iNewId
END
declare @iT int
exec @iT = dbo.test1 6
print @iT
you can do something like this
create PROCEDURE dbo.test1
(
@iID int
)
AS
BEGIN
SET NOCOUNT ON;
Declare @iNewId int
set @iNewId = @iID + 1
return @iNewId
END
declare @iT int
exec @iT = dbo.test1 6
print @iT
Adding elements to a structure
While I was playing with a request structure in coldfusion I come across a strange scenario with adding elements in the structure
There are 3 ways to add an element in the structure
<cfset request.sMyStructre[elementid] = value />
<cfset structInsert(request.sMyStructre,elementid,value) />
<cfset request.sMyStructre.elementid = value />
Even though the result of those 3 commant 99% of the times is the same I found a case where they produce a different output
I create an object test.cfc
<cfcomponent output="true">
<cfset request.sReqObjects1 = structNew() />
</cfcomponent>
and a test cfm page
<cfset request.sReqObjects1 = structNew() /> <Cfset request.sReqObjects1['t2'] = createObject("component","Test") /> using the request.sReqObjects1['t2']<br />
<cfdump var="#request.sReqObjects1#"> <cfset structInsert(request.sReqObjects1,'t3',createObject("component","Test")) /> using the structInsert(request.sReqObjects1,'t3',createObject("component","Test")) <br /> <cfdump var="#request.sReqObjects1#"> <Cfset request.sReqObjects1.t1 = createObject("component","Test") />
using the request.sReqObjects1.t1 <cfdump var="#request.sReqObjects1#">
And the output was
using the request.sReqObjects1['t2']
using the structInsert(request.sReqObjects1,'t3',createObject("component","Test"))
using the request.sReqObjects1.t1
The only reason I can thing for this is that
using [request.sReqObjects1['t2'] =] or [structInsert ] creates the element on a different memory location and just adds a reference to that location in the structure, which then is cleared by the <cfset request.sReqObjects1 = structNew() /> in the cfc constructor. But when using the <Cfset request.sReqObjects1.t1 = the new element is actually created in the memory space of the structure.
There are 3 ways to add an element in the structure
<cfset request.sMyStructre[elementid] = value />
<cfset structInsert(request.sMyStructre,elementid,value) />
<cfset request.sMyStructre.elementid = value />
Even though the result of those 3 commant 99% of the times is the same I found a case where they produce a different output
I create an object test.cfc
<cfcomponent output="true">
<cfset request.sReqObjects1 = structNew() />
</cfcomponent>
and a test cfm page
<cfset request.sReqObjects1 = structNew() /> <Cfset request.sReqObjects1['t2'] = createObject("component","Test") /> using the request.sReqObjects1['t2']<br />
<cfdump var="#request.sReqObjects1#"> <cfset structInsert(request.sReqObjects1,'t3',createObject("component","Test")) /> using the structInsert(request.sReqObjects1,'t3',createObject("component","Test")) <br /> <cfdump var="#request.sReqObjects1#"> <Cfset request.sReqObjects1.t1 = createObject("component","Test") />
using the request.sReqObjects1.t1 <cfdump var="#request.sReqObjects1#">
And the output was
using the request.sReqObjects1['t2']
struct [empty] |
---|
struct [empty] |
---|
struct | |||
---|---|---|---|
T1 |
|
The only reason I can thing for this is that
using [request.sReqObjects1['t2'] =] or [structInsert ] creates the element on a different memory location and just adds a reference to that location in the structure, which then is cleared by the <cfset request.sReqObjects1 = structNew() /> in the cfc constructor. But when using the <Cfset request.sReqObjects1.t1 = the new element is actually created in the memory space of the structure.
Wednesday, October 10, 2007
ColdFusion 8 and MSSQL triggers
Coldfusion 8 handles MSSQL triggers different that older versions of Coldfusion.
IN older CFMX
We had some For insert triggers on tables for updating related tables etc.. The last trigger used to return the new identity using select SCOPE_IDENTITY() as @iNewRec
the cfquery was
<cfquery name="qInsert" datasource="#myDSN#">
insert into tbl1 (name,fname)
values
('test1','test2')
</cfquery>
Then you could do the
<cfset variables.iNewId = qInsert.iNewRec />
In CFML 8 this will not work. You will get the error that qInsert is Undefined.
The solution is
a) make sure that the select SCOPE_IDENTITY() as @iNewRec is executed before any update/insert delete statement in any trigger in the sequence...
or
b) In the cfquery tag after the insert add a dummy select
IN older CFMX
We had some For insert triggers on tables for updating related tables etc.. The last trigger used to return the new identity using select SCOPE_IDENTITY() as @iNewRec
the cfquery was
<cfquery name="qInsert" datasource="#myDSN#">
insert into tbl1 (name,fname)
values
('test1','test2')
</cfquery>
Then you could do the
<cfset variables.iNewId = qInsert.iNewRec />
In CFML 8 this will not work. You will get the error that qInsert is Undefined.
The solution is
a) make sure that the select SCOPE_IDENTITY() as @iNewRec is executed before any update/insert delete statement in any trigger in the sequence...
or
b) In the cfquery tag after the insert add a dummy select
<cfquery name="qInsert" datasource="#myDSN#"> insert into tbl1 (name,fname) values ('test1','test2') select 'ttt' as t </cfquery>
Monday, October 8, 2007
DateDiff in coldfusion does not work the same way as dateDiff in sql
DateDiff in coldfusion does not work the same way as dateDiff in sql
I believe Coldfusion gets the different between date1 and date2 in ms and then converts this value to the date part that you specify on the dateDiff function. Sql subtracts the datepart value of date 1 from the date part value of date2.
What does that mean
DateDiff('d','2007/10/02','2007/12/01') in coldfusion is : 1 dateDiff(month,'2007/10/02','2007/12/01') in SQL is : 2 DateDiff('d','2007/10/02','2007/12/04') in coldfusion is : 2 dateDiff(month,'2007/10/02','2007/12/04') in SQL is : 2
To make coldfusion return the same value as SQL use this code
datePart('m',createDate(2007,12,1)) - datePart('m',createDate(2007,10,1))
I believe Coldfusion gets the different between date1 and date2 in ms and then converts this value to the date part that you specify on the dateDiff function. Sql subtracts the datepart value of date 1 from the date part value of date2.
What does that mean
DateDiff('d','2007/10/02','2007/12/01') in coldfusion is : 1 dateDiff(month,'2007/10/02','2007/12/01') in SQL is : 2 DateDiff('d','2007/10/02','2007/12/04') in coldfusion is : 2 dateDiff(month,'2007/10/02','2007/12/04') in SQL is : 2
To make coldfusion return the same value as SQL use this code
datePart('m',createDate(2007,12,1)) - datePart('m',createDate(2007,10,1))
Wednesday, October 3, 2007
Calling public methods with in the component without 'this' is faster
I did some testing whether i should use this to call public methods with in the component and it seams that calling a public method with in the component is faster if you do not use the 'this' .
I created 2 simple cfc that were calling internally a public function the one with this.function name and the other one the function name directly not using the this was faster for 15-20 %
I created 2 simple cfc that were calling internally a public function the one with this.function name and the other one the function name directly not using the this was faster for 15-20 %
Subscribe to:
Posts (Atom)