Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Palermo
Starting Member
25 Posts |
Posted - 2013-11-16 : 13:29:00
|
I have created a function that returns the high score from a field in a table :CREATE FUNCTION AssessSchema.fn_calcHigh(@p bigint)Returns int WITH EXECUTE AS CALLER AS BEGIN DECLARE @high intSELECT @high = (SELECT MAX(score)FROM AssessSchema.scoresWHERE score = @p)RETURN @highEND GOPlayer i.e. the players name is a field in the same table so how do I include it in this function? |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2013-11-16 : 23:07:48
|
You can't. A scalar function can only return a single value. You'll need to do it another way. a table valued function, or stored procedure, or select statement, or view, or anything other than a scalar function.Be One with the OptimizerTG |
|
|
Palermo
Starting Member
25 Posts |
Posted - 2013-11-17 : 07:53:58
|
quote: Originally posted by TG You can't. A scalar function can only return a single value. You'll need to do it another way. a table valued function, or stored procedure, or select statement, or view, or anything other than a scalar function.Be One with the OptimizerTG
Ok thank you for the reply. I will approach this another way. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-17 : 11:41:21
|
you should ideally make it a procedure which will return highest score and also player who got it.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Palermo
Starting Member
25 Posts |
Posted - 2013-11-23 : 08:19:56
|
This is how I did it :CREATE TRIGGER highscoreON AssessSchema.scoresAFTER INSERT, UPDATEAS DECLARE @score int SET @score = (SELECT score FROM INSERTED) IF @score > 100 OR @score <0BEGIN DELETE FROM Result WHERE score = @score PRINT 'insert error score < 0 or > 100' END ELSE BEGIN PRINT 'insert success' END GO |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-23 : 09:34:59
|
quote: Originally posted by Palermo This is how I did it :CREATE TRIGGER highscoreON AssessSchema.scoresAFTER INSERT, UPDATEAS DECLARE @score int SET @score = (SELECT score FROM INSERTED) IF @score > 100 OR @score <0BEGIN DELETE FROM Result WHERE score = @score PRINT 'insert error score < 0 or > 100' END ELSE BEGIN PRINT 'insert success' END GO
you're assuming only single record will be inserted/updated in one time which may not always be true. So INSERTED table can have multiple recordsSO trigger should be like belowCREATE TRIGGER highscoreON AssessSchema.scoresAFTER INSERT, UPDATEAS IF EXISTS(SELECT score FROM INSERTED WHERE score > 100 OR score <0)BEGIN DELETE FROM Result rINNER JOIN INSERTED iON i.PK = r.PKWHERE i.score > 100 OR i.score < 0PRINT 'insert error score < 0 or > 100' END ELSE BEGIN PRINT 'insert success' END GO Where PK is the primary key of the tableOne thing to note here is that even if you update a score value to be < 0 or > 100 the way its written trigger will cause record to be deleted. is that what you really want to happen?or you could even make it INSTEAD OF trigger so that INSERT doesnt happen at all------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|