- 浏览: 303987 次
- 性别:
- 来自: 河南开封
文章分类
最新评论
-
keven_niu:
好不错哦。。
jquery dialog对话框插件实例弹层效果 -
我是云:
zjuttsw 写道zjuttsw 写道楼主最后两个例子很好。 ...
Java switch-case语句用法 -
ytt06460105:
问题已解决。
使用JDBC访问DB2的问题:no db2jdbc in java.library.path -
ytt06460105:
楼主:按照你的方法做了,又出现了新的错误:[com.aliba ...
使用JDBC访问DB2的问题:no db2jdbc in java.library.path -
zjuttsw:
zjuttsw 写道楼主最后两个例子很好。学习了 不过第三条好 ...
Java switch-case语句用法
引用 session和cookie机制 sql server 2005触发器例子2
sql server2005 触发器例子
数据库 2009-10-26 15:36:00 阅读650 评论0 字号:大中小 订阅
===============================
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER TRIGGER [TR_Lz_Week_BaseOil_Insert] ON [dbo].[Lz_Week_BaseOil]
FOR INSERT,UPDATE
AS
SET NOCOUNT ON
if update(yy) or Update(wk)
Begin
update [lzlt].[dbo].[Lz_Week_BaseOil] set
index_id=cast(I.yy as varchar) + right(cast(power(10,2) as varchar)+I.wk,2) + right(cast(power(10,9) as varchar)+I.id,9)
from [lzlt].[dbo].[Lz_Week_BaseOil] P
inner join Inserted I On P.Id = I.ID
Update [lzlt].[dbo].[Lz_Week_BaseOil]
Set change_rate = P.price -
(select top 1 price
from [lzlt].[dbo].[Lz_Week_BaseOil] a
where a.index_id<=P.index_id and a.lz_BaseOil_product_id=I.lz_BaseOil_product_id
And a.ID<> I.ID
Order by a.index_id Desc
)
from [lzlt].[dbo].[Lz_Week_BaseOil] P
INNER JOIN Inserted AS I ON P.id=I.ID
--剔除错误数据
Update [lzlt].[dbo].[Lz_Week_BaseOil]
Set Change_rate=NULL
from [lzlt].[dbo].[Lz_Week_BaseOil] P
INNER JOIN Inserted AS I ON P.id=I.ID
Where (P.price + P.change_rate < 1) AND (P.price + P.change_rate > -1)
End
SET NOCOUNT OFF
/*
Update [lzlt].[dbo].[Lz_Week_BaseOil]
Set Change_rate=NULL
Where (price + change_rate < 1) AND (price + change_rate > -1)
*/
**************************************
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER TRIGGER [TR_djjz_Update] ON [dbo].[djjz]
FOR Update
AS
SET NOCOUNT ON
Begin
If Update (wp_ggxh)
Begin
Update [lzlt].[dbo].[djjzList]
Set
wp_ggxh = I.wp_ggxh
from [lzlt].[dbo].[djjzList] P
inner join Inserted I on I.[sid] = P.[sid]
End
End
SET NOCOUNT OFF
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER TRIGGER [TR_djjz_deleted] ON [dbo].[djjz]
FOR delete
AS
SET NOCOUNT ON
Begin
delete from djjzList
Where sid in (select sid from deleted)
Update [lzlt].[dbo].[lz_Domestic_exfactory_product]
Set
isdjjz = 0
from [lzlt].[dbo].[lz_Domestic_exfactory_product] P
inner join deleted I on I.[product_id] = P.[product_id]
And P.[product_Model_Id]=I.[model_id]
/*
Update [lzlt].[dbo].[lz_DomesticMarketProduct]
Set
isdjjz = 0
from [lzlt].[dbo].[lz_DomesticMarketProduct] P
inner join deleted I on I.[product_id] = P.[product_id]
And P.[product_Model_Id]=I.[model_id]
*/
Update [lzlt].[dbo].[lz_International_market_product]
Set
isdjjz = 0
from [lzlt].[dbo].[lz_International_market_product] P
inner join deleted I on I.[product_id] = P.[product_id]
-- And P.[product_Model_Id]=I.[model_id]
/*
Update [lzlt].[dbo].[lz_DomesticMarketOilProduct]
Set
isdjjz = 0
from [lzlt].[dbo].[lz_DomesticMarketOilProduct] P
inner join deleted I on I.[product_id] = P.[product_id]
And P.[ProductModelID]=I.[model_id]
*/
End
SET NOCOUNT OFF
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER TRIGGER [TR_djjzList_Insert] ON [dbo].[djjzList]
FOR INSERT,UPDATE
AS
SET NOCOUNT ON
if update(pricedate)
Begin
update [lzlt].[dbo].[djjzList] set
index_id=CONVERT(varchar(8) ,I.pricedate, 112) + Replace(CONVERT(varchar(20) ,getdate(), 108) ,':' ,'') + right(cast(power(10,9) as varchar)+I.id,9)
from [lzlt].[dbo].[djjzList] P
inner join Inserted I On P.Id = I.ID
End
SET NOCOUNT OFF
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER TRIGGER [qq_delete_bopPrice]
ON [dbo].[lz_BaseOil_Price]
FOR DELETE
AS
BEGIN
DECLARE @product_ID INT
DECLARE @price_date datetime
DECLARE @p_price_date datetime
DECLARE @p_price float
DECLARE @p_Change_Rate float
SET NOCOUNT ON;
/*
SET @price_date = (SELECT price_date FROM Deleted)
SET @product_ID = (SELECT lz_BaseOil_product_id FROM Deleted)
DECLARE @last_date datetime
set @last_date = ( select last_date from lz_BaseOil_product where lz_BaseOil_product_id=@product_ID)
If DateDiff(day,@last_date,@price_date)=0
Begin
--查找相关数据进入游标
DECLARE TR_DELETE_lz_Domestic_exfactory_price_cursor CURSOR FOR
SELECT TOP 1 price_date,price,Change_Rate FROM lz_BaseOil_Price
WHERE lz_BaseOil_product_id =@product_id
order by price_date desc
OPEN TR_DELETE_lz_Domestic_exfactory_price_cursor
FETCH NEXT FROM TR_DELETE_lz_Domestic_exfactory_price_cursor
INTO @p_price_date,@p_price,@p_Change_Rate
CLOSE TR_DELETE_lz_Domestic_exfactory_price_cursor
DEALLOCATE TR_DELETE_lz_Domestic_exfactory_price_cursor
--结束游标
update lz_BaseOil_product set last_date=@p_price_date,last_price=@p_price, Last_change_Rate=@p_Change_Rate where lz_BaseOil_product_id=@product_id
End
--print @next_id
*/
Update lz_BaseOil_product set
last_date=(select top 1 price_date from lz_BaseOil_Price WHERE lz_BaseOil_product_id =I.lz_BaseOil_product_id and lz_BaseOil_Price_Id not in (select lz_BaseOil_Price_Id from Deleted ) order by price_date desc ),
last_price=(select top 1 price from lz_BaseOil_Price WHERE lz_BaseOil_product_id =I.lz_BaseOil_product_id and lz_BaseOil_Price_Id not in(select lz_BaseOil_Price_Id from deleted) order by price_date desc ),
Last_change_Rate=(select top 1 Change_Rate from lz_BaseOil_Price WHERE lz_BaseOil_product_id =I.lz_BaseOil_product_id and lz_BaseOil_Price_Id not in(select lz_BaseOil_Price_Id from deleted) order by price_date desc )
from lz_BaseOil_product P inner join Deleted I
ON P.lz_BaseOil_product_id = I.lz_BaseOil_product_id
SET NOCOUNT OFF;
END
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER TRIGGER [qq_insert_update_bop]
ON [dbo].[lz_BaseOil_Price]
FOR INSERT,UPDATE
AS
DECLARE @current_date datetime --当前价格日期
DECLARE @product_id int --当前产品id
DECLARE @id int
DECLARE @next_id int
DECLARE @current_price decimal(18, 2) --当前价格
DECLARE @Previous_PRICE decimal(18, 2) --以前一个价格
DECLARE @memo nvarchar(500)
SET NOCOUNT ON
if update(price_date) or update(price)
Begin
SET @id = (SELECT lz_BaseOil_Price_Id FROM Inserted)
SET @current_date = (SELECT price_date FROM Inserted)
SET @product_id = (SELECT lz_BaseOil_product_id FROM Inserted)
SET @current_price = (SELECT price FROM Inserted)
SET @memo = (SELECT memo FROM Inserted)
SET @Previous_PRICE = ( SELECT TOP 1 price FROM lz_baseOil_Price WHERE lz_BaseOil_product_id=@product_id and DATEDIFF(day,price_date,@current_date)>0 order by price_date desc )
SET @Previous_PRICE = isnull(@Previous_PRICE,@current_price)
update lz_baseOil_Price set
index_id=CONVERT(varchar(8) ,price_date, 112) + Replace(CONVERT(varchar(20) ,getdate(), 108) ,':' ,'') + right(cast(power(10,9) as varchar)+lz_BaseOil_Price_Id,9)
,change_Rate=@current_price-@Previous_PRICE where lz_BaseOil_Price_Id=@id
set @next_id = ( SELECT TOP 1 lz_BaseOil_Price_Id FROM lz_baseOil_Price WHERE lz_BaseOil_product_id =@product_id and DATEDIFF(day,price_date,@current_date)<0 order by price_date asc )
if @next_id is not null
Begin
update lz_baseOil_Price set yesterday_price=@current_price,change_Rate=price-@current_price where lz_BaseOil_Price_Id=@next_id
end
DECLARE @last_date datetime
set @last_date = ( select last_date from lz_BaseOil_product where lz_BaseOil_product_id=@product_id)
If DateDiff(day,@last_date,@current_date)>=0
Begin
update lz_BaseOil_product set memo=@memo,last_date=@current_date,last_price=@current_price, Last_change_Rate=@current_price-@Previous_PRICE,modify_date=getdate() where lz_BaseOil_product_id=@product_id
End
Else
Begin
update lz_BaseOil_product set modify_date=getdate() where lz_BaseOil_product_id=@product_id
End
--print @next_id
End
SET NOCOUNT OFF
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER TRIGGER [TR_DELETE_LZ_City] ON [dbo].[Lz_City]
FOR DELETE
AS
BEGIN
DECLARE @CityID INT
DECLARE @ParentID int
SET NOCOUNT ON;
SET @CityID = (SELECT CityID FROM Deleted)
SET @ParentID = (SELECT ParentID FROM Deleted)
If @ParentID > 0
Begin
update [dbo].[LZ_City] set Child = Child - 1 Where CityID in (select CityID from [dbo].[GetLz_City_Parent](@ParentID))
End
SET NOCOUNT OFF;
END
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER TRIGGER [TR_UPDATE_LZ_City] ON [dbo].[Lz_City]
FOR UPDATE
AS
BEGIN
DECLARE @CityID int
DECLARE @ParentID int
DECLARE @f_CityID INT
DECLARE @f_ParentID int
DECLARE @f_ParentStr nvarchar(250)
DECLARE @f_Depth int
DECLARE @f_RootID int
DECLARE @f_Child int
DECLARE @f_orders int
DECLARE @f_ClassStr nvarchar(250)
SET NOCOUNT ON;
If Update(ParentID)
Begin
DECLARE @ParentStr nvarchar(250)
DECLARE @ClassStr nvarchar(250)
DECLARE @OldParentStr nvarchar(250)
DECLARE @OldParentID int
DECLARE @OldDepth int
DECLARE @Depth int
DECLARE @Child int
DECLARE @orders int
DECLARE @RootID int
SET @CityID = (SELECT CityID FROM Inserted)
SET @ParentID = (SELECT ParentID FROM Inserted)
SET @OldParentID = (SELECT OldParentID FROM Inserted)
SET @OldParentStr = (SELECT ParentStr FROM Inserted)
SET @OldDepth = (SELECT Depth FROM Inserted)
SET @Child = (SELECT Child FROM Inserted)
If @OldParentID<>@ParentID --如果改变得了父ID
Begin
If @ParentID = 0
Begin
SET @RootID = @CityID
SET @Depth = 0
--set @ParentStr = ','+ CONVERT(varchar(10) ,@CityID) +','
set @ParentStr = ''
SET @orders = (SELECT IsNULL(max(orders),0) FROM [dbo].[LZ_City] where ParentID=@ParentID )
Update [dbo].[LZ_City] SET OldParentID=@ParentID,ParentStr=',0,',Depth=0,RootID=@CityID,orders=@orders+1,classstr=','+CONVERT(varchar(10) ,@CityID)+','
WHERE CityID=@CityID
End
Else
Begin
--@CityID,@ParentID,@ParentStr,@Depth,@RootID,@Child,@orders,@ClassStr
--查找相关数据进入游标
DECLARE TR_INSERT_LZ_City_cursor CURSOR FOR
SELECT Top 1 CityID,ParentID,ParentStr,Depth,RootID,Child,orders,ClassStr FROM [dbo].[LZ_City]
WHERE CityID = @ParentID
OPEN TR_INSERT_LZ_City_cursor
FETCH NEXT FROM TR_INSERT_LZ_City_cursor
INTO @f_CityID,@f_ParentID,@f_ParentStr,@f_Depth,@f_RootID,@f_Child,@f_orders,@f_ClassStr
CLOSE TR_INSERT_LZ_City_cursor
DEALLOCATE TR_INSERT_LZ_City_cursor
--结束游标
If @f_ParentStr=',0,'
Begin
set @ParentStr = ','+ CONVERT(varchar(10) ,@f_CityID) +','
End
Else
Begin
set @ParentStr = @f_ParentStr + CONVERT(varchar(10) ,@f_CityID) + ','
End
SET @RootID = @f_RootID
SET @Depth = @f_Depth+1
SET @orders = (SELECT IsNULL(max(orders),0) FROM [dbo].[LZ_City] where ParentID=@ParentID )
Update [dbo].[LZ_City] SET
OldParentID=@ParentID,ParentStr=@ParentStr,Depth=@f_Depth+1,RootID=@f_RootID,orders=@orders+1,classstr=@f_ClassStr+CONVERT(varchar(10) ,@CityID)+','
WHERE CityID=@CityID
-- update [dbo].[LZ_City] set Child = Child + 1 Where CityID in (select CityID from [dbo].[GetLz_City_Parent](@ParentID))
End
If @Child > 0 --如果有子节点
Begin
If @OldParentID = 0
Begin
Update [dbo].[LZ_City] set
Depth=Depth + @Depth-@oldDepth,RootID=@RootID
,ParentStr=SUBSTRING(@ParentStr,1,len(@ParentStr)-1)+ParentStr
,classstr=SUBSTRING(@ParentStr,1,len(@ParentStr)-1)+classstr
Where CityID in (select CityID from [dbo].[GetLz_City_GetChildren](@CityID))
End
Else
Begin
If @ParentID = 0
Begin
Update [dbo].[LZ_City] set
Depth=Depth + @Depth-@oldDepth,RootID=@RootID
,ParentStr=','+replace(ParentStr,@oldParentStr,@ParentStr)
,classstr=','+replace(classstr,@oldParentStr,@ParentStr)
Where CityID in (select CityID from [dbo].[GetLz_City_GetChildren](@CityID))
End
else
Begin
Update [dbo].[LZ_City] set
Depth=Depth + @Depth-@oldDepth,RootID=@RootID
,ParentStr=replace(ParentStr,@oldParentStr,@ParentStr)
,classstr=replace(classstr,@oldParentStr,@ParentStr)
Where CityID in (select CityID from [dbo].[GetLz_City_GetChildren](@CityID))
end
End
update [dbo].[LZ_City] set Child = Child + @Child + 1 Where CityID in (select CityID from [dbo].[GetLz_City_Parent](@ParentID))
End
Else
Begin
update [dbo].[LZ_City] set Child = Child + 1 Where CityID in (select CityID from [dbo].[GetLz_City_Parent](@ParentID))
end
If @OldParentID > 0
Begin
If @Child > 0 --如果有子节点
Begin
update [dbo].[LZ_City] set Child = Child - @Child -1 Where CityID in (select CityID from [dbo].[GetLz_City_Parent](@oldParentID))
End
Else
Begin
update [dbo].[LZ_City] set Child = Child - 1 Where CityID in (select CityID from [dbo].[GetLz_City_Parent](@oldParentID))
End
End
end
End
If Update(AreaID)
Begin
--区域更新
update Lz_City set ProvId = NULL,CapiID = NULL,TownID = NULL ,AreaName=D.CityName,ProvName = NULL,CapiName = NULL,TownName = NULL
FROM Lz_City
INNER JOIN Inserted AS I ON Lz_City.CityId = I.CityId And Lz_City.Depth = 0
INNER JOIN Lz_City AS D on I.AreaID = D.CityID
-- 省份更新
update Lz_City set ProvId = I.CityId,CapiID = NULL,TownID = NULL ,AreaName=D.CityName,ProvName = I.CityName,CapiName = NULL,TownName = NULL
FROM Lz_City
INNER JOIN Inserted AS I ON Lz_City.CityId = I.CityId And Lz_City.Depth = 1
INNER JOIN Lz_City AS D on I.AreaID = D.CityID
-- 市级更新
update Lz_City set ProvId = I.ParentID,CapiID = I.CityID,TownID = NULL,AreaName=D.CityName,ProvName = B.CityName,CapiName = I.CityName,TownName = NULL
From Lz_City
INNER JOIN Inserted AS I ON Lz_City.CityId = I.CityId And Lz_City.Depth = 2
INNER JOIN Lz_City AS B on Lz_City.ParentId = B.CityID
INNER JOIN Lz_City AS D on I.AreaID = D.CityID
-- 县级更新
update Lz_City set ProvId = B.ParentId,CapiID = I.ParentID,TownID = I.CityID,AreaName=D.CityName,ProvName = C.CityName,CapiName = B.CityName,TownName = I.CityName
From Lz_City
INNER JOIN Inserted AS I ON Lz_City.CityId = I.CityId And Lz_City.Depth = 3
INNER JOIN Lz_City AS B on Lz_City.ParentId = B.CityID
INNER JOIN Lz_City AS C on B.ParentId = C.CityID
INNER JOIN Lz_City AS D on I.AreaID = D.CityID
End
SET NOCOUNT OFF
END
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER TRIGGER [TR_INSERT_LZ_City] ON [dbo].[Lz_City]
FOR INSERT
AS
DECLARE @CityID INT
DECLARE @ParentID int
DECLARE @ParentStr nvarchar(250)
DECLARE @orders int
DECLARE @f_CityID INT
DECLARE @f_ParentID int
DECLARE @f_ParentStr nvarchar(250)
DECLARE @f_Depth int
DECLARE @f_RootID int
DECLARE @f_AreaID int
DECLARE @f_Child int
DECLARE @f_orders int
DECLARE @f_ClassStr nvarchar(250)
DECLARE @Depth int
SET NOCOUNT ON
SET @CityID = (SELECT CityID FROM Inserted)
SET @ParentID = (SELECT ParentID FROM Inserted)
If @ParentID = 0
Begin
SET @orders = (SELECT IsNULL(max(orders),0) FROM [dbo].[LZ_City] where ParentID=@ParentID )
Update [dbo].[LZ_City] SET OldParentID=@ParentID,ParentStr=',0,',Depth=0,RootID=@CityID,AreaID=@CityID,orders=@orders+1,classstr=','+CONVERT(varchar(10) ,@CityID)+','
WHERE CityID=@CityID
Set @Depth = 0
End
Else
Begin
--@CityID,@ParentID,@ParentStr,@Depth,@RootID,@Child,@orders,@ClassStr
--查找相关数据进入游标
DECLARE TR_INSERT_LZ_City_cursor CURSOR FOR
SELECT Top 1 CityID,ParentID,ParentStr,Depth,RootID,Child,orders,ClassStr,AreaID FROM [dbo].[LZ_City]
WHERE CityID = @ParentID
OPEN TR_INSERT_LZ_City_cursor
FETCH NEXT FROM TR_INSERT_LZ_City_cursor
INTO @f_CityID,@f_ParentID,@f_ParentStr,@f_Depth,@f_RootID,@f_Child,@f_orders,@f_ClassStr,@f_AreaID
CLOSE TR_INSERT_LZ_City_cursor
DEALLOCATE TR_INSERT_LZ_City_cursor
--结束游标
If @f_ParentStr=',0,'
Begin
set @ParentStr = ','+ CONVERT(varchar(10) ,@f_CityID) +','
End
Else
Begin
set @ParentStr = @f_ParentStr + CONVERT(varchar(10) ,@f_CityID) + ','
End
SET @orders = (SELECT IsNULL(max(orders),0) FROM [dbo].[LZ_City] where ParentID=@ParentID )
Update [dbo].[LZ_City] SET
OldParentID=@ParentID,ParentStr=@ParentStr,Depth=@f_Depth+1,RootID=@f_RootID,AreaID=@f_AreaID,orders=@orders+1,classstr=@f_ClassStr+CONVERT(varchar(10) ,@CityID)+','
WHERE CityID=@CityID
set @Depth = @f_Depth+1
update [dbo].[LZ_City] set Child = Child + 1 Where CityID in (select CityID from [dbo].[GetLz_City_Parent](@ParentID))
End
If @Depth=0
Begin
--区域更新
update Lz_City set ProvId = NULL,CapiID = NULL,TownID = NULL ,AreaName=D.CityName,ProvName = NULL,CapiName = NULL,TownName = NULL
FROM Lz_City
INNER JOIN Inserted AS I ON Lz_City.CityId = I.CityId
INNER JOIN Lz_City AS D on Lz_City.AreaID = D.CityID
End
If @Depth=1
Begin
-- 省份更新
update Lz_City set ProvId = I.CityId,CapiID = NULL,TownID = NULL ,AreaName=D.CityName,ProvName = I.CityName,CapiName = NULL,TownName = NULL
FROM Lz_City
INNER JOIN Inserted AS I ON Lz_City.CityId = I.CityId
INNER JOIN Lz_City AS D on Lz_City.AreaID = D.CityID
End
If @Depth=2
Begin
-- 市级更新
update Lz_City set ProvId = I.ParentID,CapiID = I.CityID,TownID = NULL,AreaName=D.CityName,ProvName = B.CityName,CapiName = I.CityName,TownName = NULL
From Lz_City
INNER JOIN Inserted AS I ON Lz_City.CityId = I.CityId
INNER JOIN Lz_City AS B on Lz_City.ParentId = B.CityID
INNER JOIN Lz_City AS D on Lz_City.AreaID = D.CityID
End
If @Depth=3
Begin
-- 县级更新
update Lz_City set ProvId = B.ParentId,CapiID = I.ParentID,TownID = I.CityID,AreaName=D.CityName,ProvName = C.CityName,CapiName = B.CityName,TownName = I.CityName
From Lz_City
INNER JOIN Inserted AS I ON Lz_City.CityId = I.CityId
INNER JOIN Lz_City AS B on Lz_City.ParentId = B.CityID
INNER JOIN Lz_City AS C on B.ParentId = C.CityID
INNER JOIN Lz_City AS D on Lz_City.AreaID = D.CityID
End
SET NOCOUNT OFF
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER TRIGGER [TR_DELETE_LZ_Columns] ON [dbo].[LZ_Columns]
FOR DELETE
AS
BEGIN
DECLARE @ColumnID INT
DECLARE @ParentID int
SET NOCOUNT ON;
SET @ColumnID = (SELECT ColumnID FROM Deleted)
SET @ParentID = (SELECT ParentID FROM Deleted)
If @ParentID > 0
Begin
update [dbo].[LZ_Columns] set Child = Child - 1 Where ColumnID in (select ColumnID from [dbo].[GetLz_Columns_Parent](@ParentID))
End
SET NOCOUNT OFF;
END
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER TRIGGER [TR_lz_Domestic_exfactory_price] ON [dbo].[lz_Domestic_exfactory_price]
FOR INSERT,UPDATE
AS
DECLARE @current_date datetime --当前价格日期
DECLARE @product_id int --当前产品id
DECLARE @id int
DECLARE @next_id int
DECLARE @current_price decimal(18, 2) --当前价格
DECLARE @Previous_PRICE decimal(18, 2) --以前一个价格
DECLARE @memo nvarchar(500)
SET NOCOUNT ON
if update(price_date) or update(exfactory_price)
Begin
SET @id = (SELECT lz_Domestic_exfactory_price_id FROM Inserted)
SET @current_date = (SELECT price_date FROM Inserted)
SET @product_id = (SELECT lz_Domestic_exfactory_product_id FROM Inserted)
SET @current_price = (SELECT exfactory_price FROM Inserted)
SET @memo = (SELECT memo FROM Inserted)
SET @Previous_PRICE = ( SELECT TOP 1 exfactory_price FROM lz_Domestic_exfactory_price WHERE lz_Domestic_exfactory_product_id =@product_id and DATEDIFF(day,price_date,@current_date)>0 order by price_date desc )
SET @Previous_PRICE = isnull(@Previous_PRICE,@current_price)
update lz_Domestic_exfactory_price set
index_id=CONVERT(varchar(8) ,price_date, 112) + Replace(CONVERT(varchar(20) ,getdate(), 108) ,':' ,'') + right(cast(power(10,9) as varchar)+lz_Domestic_exfactory_price_id,9)
,change_Rate=@current_price-@Previous_PRICE
where lz_Domestic_exfactory_price_id=@id
set @next_id = ( SELECT TOP 1 lz_Domestic_exfactory_price_id FROM lz_Domestic_exfactory_price WHERE lz_Domestic_exfactory_product_id =@product_id and DATEDIFF(day,price_date,@current_date)<0 order by price_date asc )
if @next_id is not null
Begin
update lz_Domestic_exfactory_price set change_Rate=exfactory_price-@current_price where lz_Domestic_exfactory_price_id=@next_id
end
DECLARE @last_date datetime
set @last_date = ( select last_date from lz_Domestic_exfactory_product where lz_Domestic_exfactory_product_id=@product_id)
If DateDiff(day,@last_date,@current_date)>=0
Begin
update lz_Domestic_exfactory_product set memo=@memo,last_date=@current_date,last_exfactory_price=@current_price, Last_change_Rate=@current_price-@Previous_PRICE,modify_date=getdate() where lz_Domestic_exfactory_product_id=@product_id
End
Else
Begin
update lz_Domestic_exfactory_product set modify_date=getdate() where lz_Domestic_exfactory_product_id=@product_id
End
--print @next_id
End
SET NOCOUNT OFF
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER TRIGGER [TR_DELETE_lz_Domestic_exfactory_price] ON [dbo].[lz_Domestic_exfactory_price]
FOR DELETE
AS
BEGIN
DECLARE @product_ID INT
DECLARE @price_date datetime
DECLARE @p_price_date datetime
DECLARE @p_exfactory_price float
DECLARE @p_Change_Rate int
SET NOCOUNT ON;
/*
SET @price_date = (SELECT price_date FROM Deleted)
SET @product_ID = (SELECT lz_Domestic_exfactory_product_id FROM Deleted)
DECLARE @last_date datetime
set @last_date = ( select last_date from lz_Domestic_exfactory_product where lz_Domestic_exfactory_product_id=@product_ID)
If DateDiff(day,@last_date,@price_date)=0
Begin
--查找相关数据进入游标
DECLARE TR_DELETE_lz_Domestic_exfactory_price_cursor CURSOR FOR
SELECT TOP 1 price_date,exfactory_price,Change_Rate FROM lz_Domestic_exfactory_price
WHERE lz_Domestic_exfactory_product_id =@product_id
order by price_date desc
OPEN TR_DELETE_lz_Domestic_exfactory_price_cursor
FETCH NEXT FROM TR_DELETE_lz_Domestic_exfactory_price_cursor
INTO @p_price_date,@p_exfactory_price,@p_Change_Rate
CLOSE TR_DELETE_lz_Domestic_exfactory_price_cursor
DEALLOCATE TR_DELETE_lz_Domestic_exfactory_price_cursor
--结束游标
update lz_Domestic_exfactory_product set last_date=@p_price_date,last_exfactory_price=@p_exfactory_price, Last_change_Rate=@p_Change_Rate where lz_Domestic_exfactory_product_id=@product_id
End
--print @next_id
*/
Update lz_Domestic_exfactory_product set
last_date=(select top 1 price_date from lz_Domestic_exfactory_price WHERE lz_Domestic_exfactory_product_id =I.lz_Domestic_exfactory_product_id and lz_Domestic_exfactory_price_id not in (select lz_Domestic_exfactory_price_id from Deleted ) order by price_date desc ),
last_exfactory_price=(select top 1 exfactory_price from lz_Domestic_exfactory_price WHERE lz_Domestic_exfactory_product_id =I.lz_Domestic_exfactory_product_id and lz_Domestic_exfactory_price_id not in(select lz_Domestic_exfactory_price_id from deleted) order by price_date desc ),
Last_change_Rate=(select top 1 Change_Rate from lz_Domestic_exfactory_price WHERE lz_Domestic_exfactory_product_id =I.lz_Domestic_exfactory_product_id and lz_Domestic_exfactory_price_id not in(select lz_Domestic_exfactory_price_id from deleted) order by price_date desc )
from lz_Domestic_exfactory_product P inner join Deleted I
ON P.lz_Domestic_exfactory_product_id = I.lz_Domestic_exfactory_product_id
SET NOCOUNT OFF;
END
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER TRIGGER [TR_lz_DataSupportMemo_Insert] ON [dbo].[lz_DataSupportMemo]
FOR INSERT,UPDATE
AS
SET NOCOUNT ON
if update(PostDate) Or Update (yy) or Update(MM)
Begin
update [lzlt].[dbo].[lz_DataSupportMemo] set
index_id=CONVERT(varchar(8) ,I.PostDate, 112) + cast(I.yy as varchar) + right(cast(power(10,2) as varchar)+I.mm,2) + right(cast(power(10,9) as varchar)+I.id,9)
from [lzlt].[dbo].[lz_DataSupportMemo] P
inner join Inserted I On P.Id = I.ID
End
SET NOCOUNT OFF
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER TRIGGER [TR_lz_DataSupport_deleted] ON [dbo].[lz_DataSupport]
FOR delete
AS
SET NOCOUNT ON
Begin
delete from lz_DataSupportMemo
Where sid in (select sid from deleted)
End
SET NOCOUNT OFF
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER TRIGGER [TR_UPDATE_LZ_Columns] ON [dbo].[LZ_Columns]
FOR UPDATE
AS
BEGIN
DECLARE @ColumnID int
DECLARE @ParentID int
DECLARE @f_ColumnID INT
DECLARE @f_ParentID int
DECLARE @f_ParentStr nvarchar(250)
DECLARE @f_Depth int
DECLARE @f_RootID int
DECLARE @f_Child int
DECLARE @f_orders int
DECLARE @f_ClassStr nvarchar(250)
SET NOCOUNT ON;
If Update(ParentID)
Begin
DECLARE @ParentStr nvarchar(250)
DECLARE @ClassStr nvarchar(250)
DECLARE @OldParentStr nvarchar(250)
DECLARE @OldParentID int
DECLARE @OldDepth int
DECLARE @Depth int
DECLARE @Child int
DECLARE @orders int
DECLARE @RootID int
SET @ColumnID = (SELECT ColumnID FROM Inserted)
SET @ParentID = (SELECT ParentID FROM Inserted)
SET @OldParentID = (SELECT OldParentID FROM Inserted)
SET @OldParentStr = (SELECT ParentStr FROM Inserted)
SET @OldDepth = (SELECT Depth FROM Inserted)
SET @Child = (SELECT Child FROM Inserted)
If @OldParentID<>@ParentID --如果改变得了父ID
Begin
If @ParentID = 0
Begin
SET @RootID = @ColumnID
SET @Depth = 0
--set @ParentStr = ','+ CONVERT(varchar(10) ,@ColumnID) +','
set @ParentStr = ''
SET @orders = (SELECT IsNULL(max(orders),0) FROM [dbo].[LZ_Columns] where ParentID=@ParentID )
Update [dbo].[LZ_Columns] SET OldParentID=@ParentID,ParentStr=',0,',Depth=0,RootID=@ColumnID,orders=@orders+1,classstr=','+CONVERT(varchar(10) ,@ColumnID)+','
WHERE ColumnID=@ColumnID
End
Else
Begin
--@ColumnID,@ParentID,@ParentStr,@Depth,@RootID,@Child,@orders,@ClassStr
--查找相关数据进入游标
DECLARE TR_INSERT_LZ_Columns_cursor CURSOR FOR
SELECT Top 1 ColumnID,ParentID,ParentStr,Depth,RootID,Child,orders,ClassStr FROM [dbo].[LZ_Columns]
WHERE ColumnID = @ParentID
OPEN TR_INSERT_LZ_Columns_cursor
FETCH NEXT FROM TR_INSERT_LZ_Columns_cursor
INTO @f_ColumnID,@f_ParentID,@f_ParentStr,@f_Depth,@f_RootID,@f_Child,@f_orders,@f_ClassStr
CLOSE TR_INSERT_LZ_Columns_cursor
DEALLOCATE TR_INSERT_LZ_Columns_cursor
--结束游标
If @f_ParentStr=',0,'
Begin
set @ParentStr = ','+ CONVERT(varchar(10) ,@f_ColumnID) +','
End
Else
Begin
set @ParentStr = @f_ParentStr + CONVERT(varchar(10) ,@f_ColumnID) + ','
End
SET @RootID = @f_RootID
SET @Depth = @f_Depth+1
SET @orders = (SELECT IsNULL(max(orders),0) FROM [dbo].[LZ_Columns] where ParentID=@ParentID )
Update [dbo].[LZ_Columns] SET
OldParentID=@ParentID,ParentStr=@ParentStr,Depth=@f_Depth+1,RootID=@f_RootID,orders=@orders+1,classstr=@f_ClassStr+CONVERT(varchar(10) ,@ColumnID)+','
WHERE ColumnID=@ColumnID
-- update [dbo].[LZ_Columns] set Child = Child + 1 Where ColumnID in (select ColumnID from [dbo].[GetLz_Columns_Parent](@ParentID))
End
If @Child > 0 --如果有子节点
Begin
If @OldParentID = 0
Begin
Update [dbo].[LZ_Columns] set
Depth=Depth + @Depth-@oldDepth,RootID=@RootID
,ParentStr=SUBSTRING(@ParentStr,1,len(@ParentStr)-1)+ParentStr
,classstr=SUBSTRING(@ParentStr,1,len(@ParentStr)-1)+classstr
Where ColumnID in (select ColumnID from [dbo].[GetLz_Columns_GetChildren](@ColumnID))
End
Else
Begin
If @ParentID = 0
Begin
Update [dbo].[LZ_Columns] set
Depth=Depth + @Depth-@oldDepth,RootID=@RootID
,ParentStr=','+replace(ParentStr,@oldParentStr,@ParentStr)
,classstr=','+replace(classstr,@oldParentStr,@ParentStr)
Where ColumnID in (select ColumnID from [dbo].[GetLz_Columns_GetChildren](@ColumnID))
End
else
Begin
Update [dbo].[LZ_Columns] set
Depth=Depth + @Depth-@oldDepth,RootID=@RootID
,ParentStr=replace(ParentStr,@oldParentStr,@ParentStr)
,classstr=replace(classstr,@oldParentStr,@ParentStr)
Where ColumnID in (select ColumnID from [dbo].[GetLz_Columns_GetChildren](@ColumnID))
end
End
update [dbo].[LZ_Columns] set Child = Child + @Child + 1 Where ColumnID in (select ColumnID from [dbo].[GetLz_Columns_Parent](@ParentID))
End
Else
Begin
update [dbo].[LZ_Columns] set Child = Child + 1 Where ColumnID in (select ColumnID from [dbo].[GetLz_Columns_Parent](@ParentID))
end
If @OldParentID > 0
Begin
If @Child > 0 --如果有子节点
Begin
update [dbo].[LZ_Columns] set Child = Child - @Child -1 Where ColumnID in (select ColumnID from [dbo].[GetLz_Columns_Parent](@oldParentID))
End
Else
Begin
update [dbo].[LZ_Columns] set Child = Child - 1 Where ColumnID in (select ColumnID from [dbo].[GetLz_Columns_Parent](@oldParentID))
End
End
end
End
SET NOCOUNT OFF
END
发表评论
-
INSERT ALL和INSERT FIRST语法
2012-04-10 16:54 13在数据仓库中的转换和装载过程中,可能会使用INSERT ALL ... -
oracle字符集及编码知识
2011-07-19 17:58 3234摘要: 1,双字节字符 ... -
sqlldr导入Sequence等类型数据
2011-05-22 14:43 3121sqlldr导入Sequence等类型数据 目标表SQL: ... -
JAVA查询Oracle数据库集群连接字符串及其JDBC jar包选择
2011-02-22 17:11 2512事件: 报表接口数据库突然无法连接 ,导致无法正常取数操作. ... -
DB2基本概念 —— 实例、数据库、表空间、容器
2010-12-24 10:06 5332DB2支持以下两种类型的表空间: 1、 系统管理存储 ... -
PreparedStatement的用法
2010-12-10 17:15 1044jdbc(java database connectivity ... -
DB2中有关日期和时间的函数,及应用
2010-11-01 09:34 1603DAYNAME 返回一个大小写混合的字符串,对 ... -
SQL中UNION和UNION ALL区别
2010-06-10 11:38 2523在数据库中,UNION和UNION ALL关键字都是将两个结果 ... -
sql 特殊字符处理
2010-05-14 10:05 2488用户输入如果没有任何限制的话,则必须对特殊字符进行变换。 如果 ... -
数据库索引的作用
2010-05-05 17:06 7034索引 可以利用索引快 ... -
db2中的连接查询,内连接、外连接、交叉连接
2010-04-19 11:03 3493DB2 连接查询的学习: [size=large] 首先 ...
相关推荐
Sqlserver触发器例子,Sqlserver触发器例子,Sqlserver触发器例子,Sqlserver触发器例子
sqlserver触发器例子 一﹕ 触发器是一种特殊的存储过程﹐它不能被显式地调用﹐而是在往表中插入记录﹑更新记录或者删除记录时被自动地激活。所以触发器可以用来实现对表实施复杂的完整性约`束。 二﹕ SQL ...
SqlServer触发器例子.pdf
sqlserver 触发器 insert阿 一个关于触发器的小例子
本书重点阐述了SQL Server 2005的基础知识,前半部分以建立一个金融数据库系统为主线,从最基础的收集信息入手,一步步地引导读者学会如何保证数据库的安全,创建表、主键、索引等项目,在表之间建立恰当的关系,并...
SQL Server 2000概述、SQL Server 2000安装和配置、SQL Server 2000工具、数据库系统基础、SQL Server 2000数据类型...触发器、存储过程、SQL Server 2000数据库的安全性管理、数据库的备份和恢复、基于Web的数据库应用...
最基础的SQLServer语句总结,包括触发器、索引、监听等。
误区 #4: DDL触发器(SQL Server 2005之后被引入)就是INSTEAD OF触发器 这是错误的 DDL触发器的实现原理其实就是一个AFTER触发器。这个意思是先发生DDL操作,然后触发器再捕捉操作(当然如果你在触发器内写了...
介绍就不多说了,下边是部分目录,觉得有用的话就顶一个 C:. │ sqlserver2000.txt │ ├─第01章 │ 1.9.1 设置内存选项.sql │ 1.9.2(2) 使用文件及文件组.sql │ 1.9.2(3) 调整...
《Microsoft SQL Server 2008技术内幕:T-SQL查询》全面深入地介绍了Microsoft SQL Server 2008中高级T-SQL查询、性能优化等方面的内容,以及SQL Server 2008新增加的一些特性。主要内容包括SQL的基础理论、查询优化...
《Microsoft SQL Server 2008技术内幕:T-SQL查询》全面深入地介绍了Microsoft SQL Server 2008中高级T-SQL查询、性能优化等方面的内容,以及SQL Server 2008新增加的一些特性。主要内容包括SQL的基础理论、查询优化...
重点阐述了SQL Server 2008的基础知识,前半部分以建立一个金融数据库系统为主线,从最基础的收集信息入手,一步步地引导读者学会如何保证数据库的安全,创建表、主键、索引等项目,在表之间建立恰当的关系,并掌握...
本书深入浅出地介绍了目前世界上最受欢迎的数据库管理系统之一——SQL Server。全书共分三个部分:第一部分阐释了数据库的基本概念,讲解了数据库建模语言;第二部分展示了从概念建模到在SQL Server 2008上真正实现...
SQL Server 2005/2008中可以使用EVENTDATA函数来获取DDL触发器的上下文,从而在ROLLBACK之前截获DDL信息。EVENTDATA返回XML字段,下面的例子显示如何截获Drop Table的DDL信息。
《Microsoft SQL Server 2008技术内幕:T-SQL查询》全面深入地介绍了Microsoft SQL Server 2008中高级T-SQL查询、性能优化等方面的内容,以及SQL Server 2008新增加的一些特性。主要内容包括SQL的基础理论、查询优化...
关于T-SQL编程方面的一些技巧和例子,内容主要有触发器、日志、T-SQL、全文检索、视图、游标、存储过程、事务、索引等。
│ sqlserver2000.txt │ ├─第01章 │ 1.9.1 设置内存选项.sql │ 1.9.2(2) 使用文件及文件组.sql │ 1.9.2(3) 调整tempdb数据库的文件属性.sql │ ├─第02章 │ │ 2.1 日期概念理解中的一些测试.sql │ │ ...
对于重复行删除的问题,网上很难找到合适的答案,...如果超过几十万行的话用SQL SERVER企业管理器的设计界面修改通常会超时。 2、然后就是关键了。给个例子就是我手上正要处理的IP地址所在地清单。近40万条数据,有SIP
整理了下面试时候可能会碰到的一些sql概念,主要针对sql server,包括指针,触发器,存储过程等内容,概念和例子结合,面试开发人员的朋友可以看看
支持Oracle,Sybase ASE, IBM,DB2/UDB, MicrosoftSQL Server,MySQL 和ODBC数据源。 可以看到库中有多少表、多少存储过程、多少触发器、多少视图、多少函数,可以直接看到每个表的数据行数,可以导出查询结果的insert...