1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

anyone decent with ms-sql?

Discussion in 'BBS Hangout' started by Nikos, Oct 14, 2004.

Tags:
  1. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    So should I first execute the trigger (with all three components) and then write code that will insert, edit, delete rows in the members table?

    Do I need to keep re-running my triggers after each?
     
  2. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    What tool are you using sql query analyzer?
     
  3. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    Yes.

    For some reason, when I run the trigger code it states

    Server: Msg 207, Level 16, State 1, Procedure UpdateMemberCount, Line 7
    Invalid column name 'count'.
    Server: Msg 207, Level 16, State 1, Procedure UpdateMemberCount, Line 15
    Invalid column name 'count'.
     
  4. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    oops, not good writing free hand like this. Anyhooo..


    CREATE TRIGGER UpdateMemberCount ON [dbo].[members]
    FOR Insert, Delete, Update
    AS

    if update(groupid)
    Begin
    Update groups
    set NumMems = (Select count(*)
    from members
    where groupid= Inserted.groupid)
    from Inserted
    where groups.gid= Inserted.groupid
    End

    Update groups
    set NumMems= (Select count(*)
    from members
    where groupid= Deleted.groupid)
    from Deleted
    where groups.gid= Deleted.groupid

    ==============
    As for the newmem table:


    In query analyzer, right click the members table > script object to new windows as > create

    Then change the auto generated script from create members to create newmen. Presto, instant create table script.
     
  5. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    The problem is it has in that code,

    CREATE TABLE [NewMem] (
    [SSN] [char] (11) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
    [lname] [varchar] (40) COLLATE .................................................................................................SQL_Latin1_General_CP1_CI_AS NULL ,
    CONSTRAINT [PK_members] PRIMARY KEY CLUSTERED
    ([SSN]) ON [PRIMARY] ) ON [PRIMARY]

    I am suppose to create it from scratch, but can't seem to find the right code to do so (example).

    Should I edit this and remove the COLLATE portions?


    The first part of the code worked (trigger), I just need to test it.

    By testing, do I need to run the tirgger 3 times after I do an update, then delete, then insert? Or is it all set once I execute it once?
     
  6. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    I ran it as it was.....

    it said succesful, but i cant seem to find the table lol?

    Is it under a diff folder name?:D
     
  7. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    The trigger, you just create it once, and it will automatically run everytime the members gets updated/inserted/deleted. Just set it and forget it =)

    Yeah, just edit that script and remove the stuff you don't want.

    CREATE TABLE [NewMem] (
    [SSN] [char] (11) COLLATE NOT NULL ,
    [lname] [varchar] (40) AS Null,
    ...
    )

    Is this your homework assignment? =)
     
  8. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    right click the database and click refresh.
     
  9. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    Yes it is an assignment,that is basically impossible for me, and I have been trying at it for hours, looking at class notes/the book, and none of it is really helping much, :(

    For the create table, if I delete that stuff, won't it affect the actual creating of the fields and cause errors? I created, and refreshed, but it still didnt show up?:confused:
     
  10. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    Just be sure to create all the fields you need, set the right datatype and declare it as null or not null, same as the original table.

    Are you sure you ran the script in the right database?
     
  11. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    The table is there, just not viewable from the folder (but when I do code it can display when I ask it to).

    What I don't understand how to do is to write code that can UPDATE or DELETE rows from the members table. Any ideas?
     
  12. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    You guys are learning about triggers but have gotten into updating or deleting records from a table? :confused:

    Try

    Delete from
    where [condition]

    and

    Insert into
    (field1, field2)
    values(value1,value2)
     
  13. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    Lets say I inserted

    INSERT into NewMem
    Values ('123-46-7844', 'Duncan', 'Tim', '345 445-5645', '32 Sun Dr.', 'Phoenix', 'AZ', '38575', '00100')

    How would I remove that

    Delete from [NewMem]
    where [SSN='123-46-7844'] ??

    How do I remove the entire row?
     
  14. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    Delete from NewMem
    where SSN='123-46-7844'

    seemed to work
     
  15. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    For the update how would I change just one column cell

    Update from NewMem
    where SSN='123-46-7844'
    ................................?

    (and then change the groupid from say 5 to 6?)
     
  16. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    Update members
    set groupid = 34
    where SSN='123-46-7844'


    You only need ' around varchar, char, datetime etc.. if it's a int, tinyint etc... you won't need it.
     

Share This Page

  • About ClutchFans

    Since 1996, ClutchFans has been loud and proud covering the Houston Rockets, helping set an industry standard for team fan sites. The forums have been a home for Houston sports fans as well as basketball fanatics around the globe.

  • Support ClutchFans!

    If you find that ClutchFans is a valuable resource for you, please consider becoming a Supporting Member. Supporting Members can upload photos and attachments directly to their posts, customize their user title and more. Gold Supporters see zero ads!


    Upgrade Now