I pass 4 Variables to the Procedure and the following SHOULD happen.If @MsgGUID does not exist in the MESSAGES Table then the it should be INSERTed, if it does exist then it should be updated.@Tokens is a delimited String which is parsed to the Function and each element of the array is checked for existence in the TOKENS Table. If the Token does exist then id @IsSpam is TRUE then the TOKENS.Spams Column is incremented by ONE else the TOKENS.Hams Column is incremented by ONE. If the Token does NOT exist then it is added to the TOKENS Table and the appropriate Column is set to ONE or ZERO (Spams / Hams)Finally the Score is calculated and written to the TOKENS.Score Column.I know this is a big ask but I really need this functionality and I am a NOVICE at SQL.The 2 Tables have the following ColumnsMessages------------MsgGUID nVarChar(32) - I allocate this GUID Manually and is the KeySpam bit(1) True if Spam or False if not SpamTokens---------Token nVarChar(50) - Key ColumnSpams Int(4)Hams Int(4)Score decimal(5)-- ---------------------------------------------------- CREATE PROCEDURE UPDATE TOKENS-- --------------------------------------------------CREATE PROCEDURE spUpdateTokens( @MsgGUID nVarChar(32), @Tokens nVarChar(4000), @Delimiter Char(1), @IsSpam Bit)ASSET NoCount ONSET Xact_Abort ONDECLARE @GoodMessages int, @BadMessages intBEGIN TRANSACTION If @IsSpam = 1 BEGIN UPDATE Messages SET Spam = 1 WHERE MsgGUID = @MsgGUID UPDATE T SET T.Spams = T.Spams + 1 FROM Tokens T INNER JOIN fnSplitString(@Tokens, @Delimiter) F ON T.Token = F.Token INSERT Tokens (Token, Spams) SELECT F.Token, 1 FROM fnSplitString(@Tokens, @Delimiter) F LEFT OUTER JOIN Tokens T ON F.Token = T.Token WHERE T.Token is null END If @IsSpam = 0 BEGIN UPDATE Messages SET Spam = 0 WHERE MsgGUID = @MsgGUID UPDATE T SET T.Hams = T.Hams + 1 FROM Tokens T INNER JOIN fnSplitString(@Tokens, @Delimiter) F ON T.Token = F.Token INSERT Tokens (Token, Hams) SELECT F.Token, 1 FROM fnSplitString(@Tokens, @Delimiter) F LEFT OUTER JOIN Tokens T ON F.Token = T.Token WHERE T.Token is null END SELECT @GoodMessages = COUNT(*) FROM Messages WHERE Spam = 0 SELECT @BadMessages = COUNT(*) FROM Messages WHERE Spam = 1 If (@GoodMessages > 0 and @Badmessages > 0) BEGIN UPDATE Tokens SET Score = Convert(decimal(6,3), Spams) / Convert(decimal(6,3), @BadMessages) / ( Convert(decimal(6,3), Hams) / Convert(decimal(6,3), @GoodMessages) + Convert(decimal(6,3), Spams) / Convert(decimal(6,3), @BadMessages) ) ENDCOMMITGOThis function appears to work perfectly-- ----------------------------------------------------- SPLIT FUNCTION - Splits a String using a Delimiter-- ---------------------------------------------------CREATE FUNCTION fnSplitString( @InString nvarchar(4000), @Delimiter char(1))RETURNS @Tokens table ( Token nVarChar(50) )ASBEGIN DECLARE @Position int WHILE Len(@InString) > 0 BEGIN SET @Position = CharIndex(@Delimiter, @InString) If @Position > 0 BEGIN INSERT @Tokens SELECT LTrim(RTrim(Left(@InString, @Position - 1))) SET @InString = Right(@InString, Len(@InString) - @Position) END ELSE BEGIN INSERT @Tokens SELECT LTrim(RTrim(@InString)) SET @InString = '' END ENDRETURNEND
Brian GillhamFailSafe Systems