PDA

View Full Version : حرفه ای: سوال در مورد trigger



ali15462002
پنج شنبه 23 مهر 1388, 08:23 صبح
سلام من یک trigger نوشتم که هروقت یک رکوردی توی یک جدول update شد اطلاعات کل آن رکورد را در یک جدول دیگر وارد کند ولی زمانیکه فیلدی که کلید خارجی است در جدول تغییر میکند مقدار null در جدول دوم وارد میشود
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @Sec_Code smallint ,
@User_Code int,
@User_Employee_Code int,
@User_LoginName nvarchar(50) ,
@User_Password nvarchar(256),
@User_Role_Code smallint ,
@User_Start_Date char(10),
@User_End_Date char(10) ,
@IsActive bit ,
@IsBlocked bit
select @Sec_Code=upd.Sec_Code,@User_Code=upd.User_Code ,
@User_LoginName=upd.User_LoginName,@User_Password= upd.User_Password,
@User_Role_Code=upd.User_Role_Code,@User_Employee_ Code=upd.User_Employee_Code,
@User_Start_Date=upd.User_Start_Date,@User_End_Dat e=upd.User_End_Date,
@IsActive=upd.IsActive,@IsBlocked=upd.IsBlocked
FROM inserted upd


exec SP_User_temp 1,@User_Role_Code,@Sec_Code,@IsActive,@User_End_Da te,@User_Password,
@User_Code,@User_Start_Date,@User_LoginName,@User_ Employee_Code,@IsBlocked,null
-- Insert statements for trigger here
END

ali15462002
شنبه 25 مهر 1388, 07:54 صبح
کسی نمیتونه به من کمک کنه؟

ali15462002
یک شنبه 26 مهر 1388, 13:05 عصر
:اشتباه:بابا یکی کمک کنه

محمد سلیم آبادی
یک شنبه 26 مهر 1388, 13:51 عصر
حد اقل نوع Trigger را ذکر می کردین.
الان Instead Of یا After؟

خطوط مربوط به تعریف Trigger را هم ارسال کنین.

ali15462002
یک شنبه 26 مهر 1388, 14:20 عصر
ALTER TRIGGER [trigger_Update_User_Temp]
ON [dbo].[tbl_User]
after UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @Sec_Code smallint ,
@User_Code int,
@User_Employee_Code int,
@User_LoginName nvarchar(50) ,
@User_Password nvarchar(256),
@User_Role_Code smallint ,
@User_Start_Date char(10),
@User_End_Date char(10) ,
@IsActive bit ,
@IsBlocked bit
select @Sec_Code=upd.Sec_Code,@User_Code=upd.User_Code ,
@User_LoginName=upd.User_LoginName,@User_Password= upd.User_Password,
@User_Role_Code=upd.User_Role_Code,@User_Employee_ Code=upd.User_Employee_Code,
@User_Start_Date=upd.User_Start_Date,@User_End_Dat e=upd.User_End_Date,
@IsActive=upd.IsActive,@IsBlocked=upd.IsBlocked
FROM inserted upd


exec SP_User_temp 1,@User_Role_Code,@Sec_Code,@IsActive,@User_End_Da te,@User_Password,
@User_Code,@User_Start_Date,@User_LoginName,@User_ Employee_Code,@IsBlocked,null
-- Insert statements for trigger here
END

اینم کل کد من

محمد سلیم آبادی
یک شنبه 26 مهر 1388, 14:30 عصر
امکان دارد محتوای Stored Procedure --> SP_User_Temp هم ارسال کنید؟
اگر مقدار ستون خارجی تغییر نکند Null درج نخواهد شد؟ یا اینکه در هر دو صورت Null ذخیره می شود؟

ali15462002
یک شنبه 26 مهر 1388, 14:33 عصر
ALTER PROCEDURE [dbo].[SP_User_temp]
@OperationType int, /* 1-->insert_Update , 2-->Delete , 3-->SelectOne , 4-->Select All , 5--> SelectByCondition */
@User_Role_Code smallint = null,
@Sec_Code smallint = null,
@IsActive bit = null,
@User_End_Date char(10) = null,
@User_Password nvarchar(512) = null,
@User_Code int = null output,
@User_Start_Date char(10) = null,
@User_LoginName nvarchar(100) = null,
@User_Employee_Code int = null,
@IsBlocked bit = null,
@condition nvarchar(200)= null

AS
if @OperationType = 1
begin

if @User_Code in ( Select [User_Code] from [dbo].[tbl_User_temp] where [Sec_Code]=@Sec_Code )
begin
Update [dbo].[tbl_User_temp]
set
[User_Role_Code]=@User_Role_Code,
/*[Sec_Code]=@Sec_Code,*/
[IsActive]=@IsActive,
[User_End_Date]=@User_End_Date,
[User_Password]=@User_Password,
[User_Start_Date]=@User_Start_Date,
[User_LoginName]=@User_LoginName,
[User_Employee_Code]=@User_Employee_Code,
[IsBlocked]=@IsBlocked
where [User_Code] = @User_Code and [Sec_Code]=@Sec_Code
end
else
begin

INSERT INTO [dbo].[tbl_User_temp]
(
[User_Role_Code],
[Sec_Code],
[IsActive],
[User_End_Date],
[User_Password],
User_Code,
[User_Start_Date],
[User_LoginName],
[User_Employee_Code],
[IsBlocked]
)
VALUES
(
@User_Role_Code,
@Sec_Code,
@IsActive,
@User_End_Date,
@User_Password,
@User_Code,
@User_Start_Date,
@User_LoginName,
@User_Employee_Code,
@IsBlocked
)

end
end
if @OperationType = 2
begin
Delete From [dbo].[tbl_User_temp] where [User_Code] = @User_Code
end
if @OperationType = 3
begin
Select * From [dbo].[tbl_User_temp] where [User_Code] = @User_Code
end
if @OperationType = 4
begin
Select * From [dbo].[tbl_User_temp]
end
if @OperationType = 5
begin
declare @con nvarchar(250)
set @con = 'select * from [dbo].[tbl_User_temp] where ' + @condition
exec( @con )
end

محمد سلیم آبادی
یک شنبه 26 مهر 1388, 14:40 عصر
برای خوانایی بیشتر امکان دارد کد هایتان را مخصوصا کد قبلی را داخل تگ کد قرار دهین.

برای این منظور از آیکنی شبیه به # استفاده کنین

ali15462002
یک شنبه 26 مهر 1388, 14:44 عصر
وقتی فیلدی دیگری تغییر میکند null برنمی گرداند

ali15462002
یک شنبه 26 مهر 1388, 14:47 عصر
کدام قسمت ر ا داخل تگ کد قرار بدم؟

محمد سلیم آبادی
یک شنبه 26 مهر 1388, 14:54 عصر
کدام قسمت ر ا داخل تگ کد قرار بدم؟

شما ابتدا کد هایی را که می خواهین ارسال کنین را در یک Document کپی پیست کرده (رنگ آن هم مشکلی کنید بهتر است) سپس دوباره آنها را کپی کرده بین دو تگ Code.

ali15462002
یک شنبه 26 مهر 1388, 15:00 عصر
ALTER PROCEDURE [dbo].[SP_User_temp]
@OperationType int, /* 1-->insert_Update , 2-->Delete , 3-->SelectOne , 4-->Select All , 5--> SelectByCondition */
@User_Role_Code smallint = null,
@Sec_Code smallint = null,
@IsActive bit = null,
@User_End_Date char(10) = null,
@User_Password nvarchar(512) = null,
@User_Code int = null output,
@User_Start_Date char(10) = null,
@User_LoginName nvarchar(100) = null,
@User_Employee_Code int = null,
@IsBlocked bit = null,
@condition nvarchar(200)= null

AS

if @OperationType = 1
begin

if @User_Code in ( Select [User_Code] from [dbo].[tbl_User_temp] where [Sec_Code]=@Sec_Code )
begin
Update [dbo].[tbl_User_temp]
set
[User_Role_Code]=@User_Role_Code,
/*[Sec_Code]=@Sec_Code,*/
[IsActive]=@IsActive,
[User_End_Date]=@User_End_Date,
[User_Password]=@User_Password,
[User_Start_Date]=@User_Start_Date,
[User_LoginName]=@User_LoginName,
[User_Employee_Code]=@User_Employee_Code,
[IsBlocked]=@IsBlocked
where [User_Code] = @User_Code and [Sec_Code]=@Sec_Code
end

else
begin

INSERT INTO [dbo].[tbl_User_temp]
(
[User_Role_Code],
[Sec_Code],
[IsActive],
[User_End_Date],
[User_Password],
User_Code,
[User_Start_Date],
[User_LoginName],
[User_Employee_Code],
[IsBlocked]
)
VALUES
(
@User_Role_Code,
@Sec_Code,
@IsActive,
@User_End_Date,
@User_Password,
@User_Code,
@User_Start_Date,
@User_LoginName,
@User_Employee_Code,
@IsBlocked
)

end
end

if @OperationType = 2
begin
Delete From [dbo].[tbl_User_temp] where [User_Code] = @User_Code
end

if @OperationType = 3
begin
Select * From [dbo].[tbl_User_temp] where [User_Code] = @User_Code
end

if @OperationType = 4
begin
Select * From [dbo].[tbl_User_temp]
end

if @OperationType = 5
begin
declare @con nvarchar(250)
set @con = 'select * from [dbo].[tbl_User_temp] where ' + @condition
exec( @con )
end

محمد سلیم آبادی
یک شنبه 26 مهر 1388, 15:14 عصر
وقتی فیلدی دیگری تغییر میکند null برنمی گرداند

اگر ستون کلید خارجی مقدارش تغییر کند تمام مقادیر ستون ها (یعنی کل سطر) مقدارشان Null شده و در جدول مورد نظر Null ذخیره می شود؟

یا اینکه تنها ستون مربوط به FK مقدار Null در خود ذخیره می کند؟

ali15462002
یک شنبه 26 مهر 1388, 15:17 عصر
تمام مقادیر null میشوند