sqlhelp Posted May 18, 2017 Report Share Posted May 18, 2017 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 Quote Link to comment Share on other sites More sharing options...
chicchara Posted May 18, 2017 Report Share Posted May 18, 2017 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 Quote Link to comment Share on other sites More sharing options...
sqlhelp Posted May 18, 2017 Author Report Share Posted May 18, 2017 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 Quote Link to comment Share on other sites More sharing options...
chicchara Posted May 18, 2017 Report Share Posted May 18, 2017 13 minutes ago, sqlhelp said: End tarvtha AS is not valid kada ? SQL lo begin and end are like block did u try the suggested change? Quote Link to comment Share on other sites More sharing options...
sqlhelp Posted May 18, 2017 Author Report Share Posted May 18, 2017 4 minutes ago, chicchara said: did u try the suggested change? Yes incorrect syntax near As ani error avthundi Quote Link to comment Share on other sites More sharing options...
chicchara Posted May 18, 2017 Report Share Posted May 18, 2017 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 Quote Link to comment Share on other sites More sharing options...
sqlhelp Posted May 18, 2017 Author Report Share Posted May 18, 2017 ah values ni ela check cheyali? print use cheyocha? Quote Link to comment Share on other sites More sharing options...
BostonBullodu Posted May 18, 2017 Report Share Posted May 18, 2017 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 Quote Link to comment Share on other sites More sharing options...
chicchara Posted May 18, 2017 Report Share Posted May 18, 2017 9 minutes ago, sqlhelp said: ah values ni ela check cheyali? print use cheyocha? select 'field name' from table Quote Link to comment Share on other sites More sharing options...
BostonBullodu Posted May 18, 2017 Report Share Posted May 18, 2017 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 Quote Link to comment Share on other sites More sharing options...
sqlhelp Posted May 18, 2017 Author Report Share Posted May 18, 2017 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 Quote Link to comment Share on other sites More sharing options...
BostonBullodu Posted May 18, 2017 Report Share Posted May 18, 2017 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 Quote Link to comment Share on other sites More sharing options...
sqlhelp Posted May 18, 2017 Author Report Share Posted May 18, 2017 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 Quote Link to comment Share on other sites More sharing options...
sqlhelp Posted May 18, 2017 Author Report Share Posted May 18, 2017 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 Quote Link to comment Share on other sites More sharing options...
SwamyRaRa Posted May 18, 2017 Report Share Posted May 18, 2017 Calling @sqladdict Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.