Jump to content

Sql trigger


sqlhelp

Recommended Posts

Hellooo 

Oka trigger create chesa for column 'created' in T-sql, whenever there is a row insert I want to insert create dat for updated row  in  'table' 

kinda query null chesesthundi instead of giving me a date ? or date ichi when we do next step null chesthundi ... okasari create dat insert avthe inka update avkudadhu.. 

 

em wrong undi query lo ? if else laga rayala? 

column default value get date set cheydanki kuda chusanu trigger badulu but did not work as permisns levu sariga

 

ALTER TRIGGER [dbo].[Tri_Created]

ON [dbo].[Table]

After Update

as

begin

UPDATE [dbo].[table]

    SET [created] = 

Case 

When t.created is not null then d.created

When t.created is null then getdate()

END

From Deleted d

Join table t on t.ID = d.ID 

Where t.ID = d.ID 

--and t.created <> d.created

END

 

GO

Link to comment
Share on other sites

Case 

When t.created is not null then d.created

When t.created is null then getdate()

END as CREATED 

ani ichi chudu... case tharvatha you are not telling what the column shd end as

Link to comment
Share on other sites

56 minutes ago, chicchara said:

Case 

When t.created is not null then d.created

When t.created is null then getdate()

END as CREATED 

ani ichi chudu... case tharvatha you are not telling what the column shd end as

End tarvtha AS is not valid kada ? SQL lo begin and end are like block 

Link to comment
Share on other sites

t.created and  d.created lo emaina null values unaay emo check chey.. 

if not... lets wait for experts to answer.

I think solution is simple tweak in code

Link to comment
Share on other sites

2 hours ago, sqlhelp said:

Hellooo 

Oka trigger create chesa for column 'created' in T-sql, whenever there is a row insert I want to insert create dat for updated row  in  'table' 

kinda query null chesesthundi instead of giving me a date ? or date ichi when we do next step null chesthundi ... okasari create dat insert avthe inka update avkudadhu.. 

 

em wrong undi query lo ? if else laga rayala? 

column default value get date set cheydanki kuda chusanu trigger badulu but did not work as permisns levu sariga

 

ALTER TRIGGER [dbo].[Tri_Created]

ON [dbo].[Table]

After Update

as

begin

UPDATE [dbo].[table]

    SET [created] = 

Case 

When t.created is not null then d.created

When t.created is null then getdate()

END

From Deleted d

Join table t on t.ID = d.ID 

Where t.ID = d.ID 

--and t.created <> d.created

END

 

GO

eh query parse chesi choodu okasari 

case when x then y when y then z else missing emo choodu

Link to comment
Share on other sites

try this

 

ALTER TRIGGER [dbo].[Tri_Created]

ON [dbo].[Table]

After Update

as

begin

UPDATE [dbo].[table]

    SET [created] = 

Case 


When t.created is null then getdate() ELSE d.Created

END 

From table t

Join Deleted d on t.ID = d.ID 

Where t.ID = d.ID 

Link to comment
Share on other sites

Inserted has new date value and deleted has 1st created date value. 

I made some changes to the query instead of case

    DECLARE @ID INT
    SELECT @ID = ID FROM Inserted 
    
    If (Select Created from Deleted) is not null
    Update Table
    Set Created = (Select Created from Deleted)
    Where ID in (Select top 1 ID from Deleted)

    Else
    Update Table 
    Set Created = Getdate()
    Where ID = @ID
 

ipudu null avatledu value but the 'Created' column is still updating

naku 1st time row create aina time eh kavali and it should not update if there is a value in the row

Link to comment
Share on other sites

3 minutes ago, sqlhelp said:

Inserted has new date value and deleted has 1st created date value. 

I made some changes to the query instead of case

    DECLARE @ID INT
    SELECT @ID = ID FROM Inserted 
    
    If (Select Created from Deleted) is not null
    Update Table
    Set Created = (Select Created from Deleted)
    Where ID in (Select top 1 ID from Deleted)

    Else
    Update Table 
    Set Created = Getdate()
    Where ID = @ID
 

ipudu null avatledu value but the 'Created' column is still updating

naku 1st time row create aina time eh kavali and it should not update if there is a value in the row

new record insert ayina prathi sari created update ayithadi 

Lets say oka record insereted at 10: 55 AM

inko record inserted at 12:35PM

nee created dates will be 10:55 and 12:35 right 

and deleted will contain 10:55 for inserted record of 12:35

Link to comment
Share on other sites

53 minutes ago, BostonBullodu said:

try this

 

ALTER TRIGGER [dbo].[Tri_Created]

ON [dbo].[Table]

After Update

as

begin

UPDATE [dbo].[table]

    SET [created] = 

Case 


When t.created is null then getdate() ELSE d.Created

END 

From table t

Join Deleted d on t.ID = d.ID 

Where t.ID = d.ID 

null ki update avthundi ee query 

Link to comment
Share on other sites

5 minutes ago, BostonBullodu said:

new record insert ayina prathi sari created update ayithadi 

Lets say oka record insereted at 10: 55 AM

inko record inserted at 12:35PM

nee created dates will be 10:55 and 12:35 right 

and deleted will contain 10:55 for inserted record of 12:35

You are right but ikada same row just status update avthundi so for example 

First row insert aina apudu values ila unte 

name     notes                      created                                 status

India       testnotes              05-17-2017 9:00:00 AM    Open

 

After the trigger  ila update avthunayi value either with null or current date time

name   notes               created  status

India    testnotes         NULL    Close

 

But what I want is

name   notes               created                                    status

India    testnotes         05-17-2017 9:00:00 AM          Close

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...