Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Trigger on Insert for a unique row Expand / Collapse
Author
Message
Posted Friday, November 09, 2012 2:11 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, November 13, 2012 7:49 AM
Points: 7, Visits: 23
Hi All,

i'm looking to try to create a trigger that will just create a txt file in a folder when a new row is inserted in the table, however i would only like this to trigger if the row is a new value.

e.g.

say there are two columns ID, Name.

Name is not unique, so if i already have a row with "smith" in the name column, and a new row gets entered with "smith" in the name column, then it wont trigger (or at least won't create the txt file), however if the new insert is the first occurrence of "smith", then i would like it to trigger and create the txt file.

hopefully i have explained this ok, is anyone able to point me in the right direction. - i'm not sure about the uniqueness element.

Many thanks in advance

Jason
Post #1382886
Posted Friday, November 09, 2012 2:23 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, May 14, 2013 4:11 AM
Points: 1,042, Visits: 1,438
You just want to create a text file??....What do you want to be written into it??....

Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1382893
Posted Friday, November 09, 2012 2:36 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, November 13, 2012 7:49 AM
Points: 7, Visits: 23
Hi Vinu,

i am not really interested in any content to be honest, the txt file will be used as a trigger for another bit of software - the only thing needed is that the txt file needs to be called the name in the column e.g. smith.txt

Many Thanks

Jason
Post #1382899
Posted Friday, November 09, 2012 5:15 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, May 14, 2013 4:11 AM
Points: 1,042, Visits: 1,438
Sorry for the late reply Jason.

If I have understood your requirement correctly then the following sample script would do what you want.........test the script with the test cases provided and further according to your requirement you can edit the script....

--Creating Table

Create Table Ex
(
Id int Identity(1,1),
Name nVarchar(30)
)


--Createing Trigger

Create Trigger Test_Trig
On Ex
After INSERT
As
Begin
Declare @val NVarchar(30), @val1 int, @sql NVarchar(400)
Select @val = Name From Inserted
Select @val1 = Count(Name) From Ex Where Name = (Select name From Inserted)
Set @sql = 'echo hello > D:\' + @val + '.txt'---------------specify path of text file according to your requirement
If (@val1 <= 1)
Begin
Execute xp_cmdshell @sql
End
End


--Testing Trigger

Insert Into Ex--------------------First Insert
Select 'Vinu'

Insert Into Ex--------------------Second Insert
Select 'Vinu'

Insert Into Ex--------------------Third Insert
Select 'Vinu'

Insert Into Ex--------------------Fourth Insert
Select 'Jason'


--Checking values in table

Select * From Ex
--Truncate Table Ex


--Check text files created at above mentioned path




NOTE : This Trigger only works for single inserts not batch inserts. Let me see if I can get something to work for Batch inserts. I'll get back to you on this.


Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1382972
Posted Friday, November 09, 2012 8:48 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 10:07 AM
Points: 935, Visits: 1,709
do you only need one text file per insert? ie. If you insert bob, jim, and sally in one batch and bob and sally are unique (new) do you need one text file or 2?

Also there may be a better way to do this. What are you trying to accomplish with the text file triggering another piece of software? could you use something like (but not necessarily) xp_cmdshell in the trigger to call the program directly?

also what happens when there are millions of records in the table that have to be churned through the trigger on every insert? the trigger is probably not the best idea from a design stand point.



For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden for the best way to ask your question.

For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw

Need to Split some strings? Jeff Moden's DelimitedSplit8K
Jeff Moden's Cross tab and Pivots Part 1
Jeff Moden's Cross tab and Pivots Part 2

Jeremy Oursler
Post #1383068
Posted Sunday, November 11, 2012 9:38 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, May 14, 2013 4:11 AM
Points: 1,042, Visits: 1,438
Yes Capn....it definitely is not a good idea to use a trigger for doing this. But, the requirement was very interesting and I just wanted to see if I could trigger something like this....since i haven't done it before.
I just wanted to show the OP that it can b done and was going to tell him that its not a good idea to do so.

Jason, I did get it to work for batch inserts......you can use a temp table to store the inserted values and then use a cursor to select each value and check with the old table if its unique or a duplicate and then create the text file.

But, this would be a very time consuming routine and would kill your performance completely. So, the bottom line is that it is a very bad idea to do what you are doing using a Trigger.


Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1383494
Posted Monday, November 12, 2012 12:38 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, March 26, 2013 8:41 AM
Points: 2,562, Visits: 3,451
vinu512 (11/11/2012)
I did get it to work for batch inserts......you can use a temp table to store the inserted values and then use a cursor to select each value and check with the old table if its unique or a duplicate and then create the text file. But, this would be a very time consuming routine and would kill your performance completely. So, the bottom line is that it is a very bad idea to do what you are doing using a Trigger.

IF the software is using the newly created txt files then we can go with the asynchronous call where service broker OR job with above quoted approach can acheive this task and this can avoid the resource contentions


-------Bhuvnesh----------
While 1 = 1 (Learning SQL....)
Click to get fast response of your post
Post #1383522
Posted Tuesday, November 13, 2012 7:50 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, November 13, 2012 7:49 AM
Points: 7, Visits: 23
Hi All,

thanks for all the feedback, i think i am quickly learning that this may not be the best way to approach the issue i have.

i will put my thinking cap back on.

Many Thanks

Jason
Post #1384108
Posted Wednesday, November 14, 2012 9:22 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, May 14, 2013 4:11 AM
Points: 1,042, Visits: 1,438
Bhuvnesh (11/12/2012)
vinu512 (11/11/2012)
I did get it to work for batch inserts......you can use a temp table to store the inserted values and then use a cursor to select each value and check with the old table if its unique or a duplicate and then create the text file. But, this would be a very time consuming routine and would kill your performance completely. So, the bottom line is that it is a very bad idea to do what you are doing using a Trigger.

IF the software is using the newly created txt files then we can go with the asynchronous call where service broker OR job with above quoted approach can acheive this task and this can avoid the resource contentions


Still....the requirement seems a little over the top. I would like to know what the actual requirement is.....if its simply creating text files for every inserted row then we store the newly added names in the db itself.......it depends on what the requirement is and i would advise this should be done if and only if there is no other way to accomplish this other than using triggers to do it.


Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1384971
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse