| Author |
Topic |
|
TAS
Yak Posting Veteran
65 Posts |
Posted - 2005-04-15 : 10:19:37
|
| I have a INSERT, UPDATE, DELETE trigger as followsCREATE TRIGGER [trigInvUpdate] ON [dbo].[tblInv] FOR INSERT, UPDATE, DELETE ASDeclare @InsCode varchar(5), @InsCodeOld varchar(5)Declare @InsCodeInclude varchar(5), @InsCodeIncludeOld varchar(5)Declare @InsCodeExclude varchar(5), @InsCodeExcludeOld varchar(5)Declare @InvCal int, @InvCalOld intDeclare @RateVar decimal, @RateVarOld decimalDeclare @RateFixed decimal, @RateFixedOld decimalDeclare @LimitLow decimal, @LimitLowOld decimalDeclare @LimitHigh decimal, @LimitHighOld decimalDeclare @FirstPay smalldatetime, @FirstPayOld smalldatetimeDeclare @Name varchar(40), @NameOld varchar(40)Declare @Street varchar(40), @StreetOld varchar(40)Declare @Street2 varchar(40), @Street2Old varchar(40)Declare @City varchar(28), @CityOld varchar(28)Declare @State char(2), @StateOld char(2)Declare @Zip varchar(10), @ZipOld varchar(10)Declare @LikeCode bit, @LikeCodeOld bitDeclare @InsertCount int, @DeleteCount intDeclare @Note varchar(100)Select @InsertCount = (Select COUNT(*) from INSERTED)If @InsertCount >0 Begin Select @InsCode=vchrInsCode,@InsCodeInclude=vchrInsCodeInclude, @InsCodeExclude =vchrInsCodeExclude,@InvCal=intInvCalc, @RateVar =decRateVar, @RateFixed=decRateFixed, @LimitLow =decLimitLow, @LimitHigh =decLimitHigh , @FirstPay =sdtmFirstDay , @Name =vchrName, @Street=vchrStreet, @Street2 = vchrStreet2 , @City =vchrCity , @State =chrState , @Zip =vchrZip, bitLikeCode = @LikeCode from INSERTED Select @InsCodeOld = vchrInsCode, @InsCodeIncludeOld=vchrInsCodeInclude, @InsCodeExcludeOld =vchrInsCodeExclude, @InvCalOld = intInvCal, @RateVarOld =decRateVar,@RateFixedOld =decRateFixed, @LimitLowOld =decLimitLow, @LimitHighOld =decLimitHigh, @FirstPayOld =sdtmFirstPay, @NameOld =vchrName, @StreetOld =vchrStreet,@Street2Old = vchrStreet2, @CityOld =vchrCity, @StateOld =chrState, @ZipOld =vchrZip, @LikeCodeOld =bitLikeCode from DELETED End If ISNULL(@InsCode,'') <>ISNULL(@InsCodeOld,'')Select @Note='Change Provider Invoice Info'Begin INSERT INTO tblChngAgtProv VALUES('P', @InsCode, 'Provider Code', @InsCodeOld, @InsCode, Getdate(),Substring(System_user,7,30), @Note)EndIf ISNULL(@Street,'') <>ISNULL(@StreetOld,'')Begin Insert into tblChngAgtProv values('P', @InsCode, 'Street', @StreetOld, @Street, Getdate(),Substring(System_user,7,30), @Note)EndWhen it runs, I got an error:A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.Anyone would help? |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-04-15 : 10:44:26
|
| First problem you have is that the inserted and deleted tables can have 1 to many rows. So your logic as it stands will not handle the many right now.Also, you might want to look at IF UPDATE in Books online under CREATE TRIGGERPersonally I prefer to keep this as simple as possible.Create an almost identical table, add a couple of columns for HIST_AD_TS, HIST_AD _BY, HIST_ADD_TYPEAnd move the entire rowAnd lose the FRO INSERT piece...the new row will be stored in your current table...MOOBrett8-) |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2005-04-16 : 06:01:06
|
| This error will typically be given if you do something like this:SELECT @Value1 = value1, value2, @Value3 = value3Not the same you have done here but the following does not make sense:If ISNULL(@InsCode,'') <>ISNULL(@InsCodeOld,'')Select @Note='Change Provider Invoice Info' <--- this select is not placed correctlyBeginINSERT INTO tblChngAgtProv VALUES('P', @InsCode, 'Provider Code', @InsCodeOld, @InsCode, Getdate(),Substring(System_user,7,30), @Note)EndYou should probably put this inside the begin-end statements...--Lumbago"Real programmers don't document, if it was hard to write it should be hard to understand" |
 |
|
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2005-04-18 : 00:55:52
|
To answer your question regarding error, see text in redquote: Originally posted by TAS I have a INSERT, UPDATE, DELETE trigger as followsCREATE TRIGGER [trigInvUpdate] ON [dbo].[tblInv] FOR INSERT, UPDATE, DELETE ASDeclare @InsCode varchar(5), @InsCodeOld varchar(5)Declare @InsCodeInclude varchar(5), @InsCodeIncludeOld varchar(5)Declare @InsCodeExclude varchar(5), @InsCodeExcludeOld varchar(5)Declare @InvCal int, @InvCalOld intDeclare @RateVar decimal, @RateVarOld decimalDeclare @RateFixed decimal, @RateFixedOld decimalDeclare @LimitLow decimal, @LimitLowOld decimalDeclare @LimitHigh decimal, @LimitHighOld decimalDeclare @FirstPay smalldatetime, @FirstPayOld smalldatetimeDeclare @Name varchar(40), @NameOld varchar(40)Declare @Street varchar(40), @StreetOld varchar(40)Declare @Street2 varchar(40), @Street2Old varchar(40)Declare @City varchar(28), @CityOld varchar(28)Declare @State char(2), @StateOld char(2)Declare @Zip varchar(10), @ZipOld varchar(10)Declare @LikeCode bit, @LikeCodeOld bitDeclare @InsertCount int, @DeleteCount intDeclare @Note varchar(100)Select @InsertCount = (Select COUNT(*) from INSERTED)If @InsertCount >0 Begin Select @InsCode=vchrInsCode,@InsCodeInclude=vchrInsCodeInclude, @InsCodeExclude =vchrInsCodeExclude,@InvCal=intInvCalc, @RateVar =decRateVar, @RateFixed=decRateFixed, @LimitLow =decLimitLow, @LimitHigh =decLimitHigh , @FirstPay =sdtmFirstDay , @Name =vchrName, @Street=vchrStreet, @Street2 = vchrStreet2 , @City =vchrCity , @State =chrState , @Zip =vchrZip, bitLikeCode = @LikeCode from INSERTED Select @InsCodeOld = vchrInsCode, @InsCodeIncludeOld=vchrInsCodeInclude, @InsCodeExcludeOld =vchrInsCodeExclude, @InvCalOld = intInvCal, @RateVarOld =decRateVar,@RateFixedOld =decRateFixed, @LimitLowOld =decLimitLow, @LimitHighOld =decLimitHigh, @FirstPayOld =sdtmFirstPay, @NameOld =vchrName, @StreetOld =vchrStreet,@Street2Old = vchrStreet2, @CityOld =vchrCity, @StateOld =chrState, @ZipOld =vchrZip, @LikeCodeOld =bitLikeCode from DELETED End If ISNULL(@InsCode,'') <>ISNULL(@InsCodeOld,'')Select @Note='Change Provider Invoice Info'Begin INSERT INTO tblChngAgtProv VALUES('P', @InsCode, 'Provider Code', @InsCodeOld, @InsCode, Getdate(),Substring(System_user,7,30), @Note)EndIf ISNULL(@Street,'') <>ISNULL(@StreetOld,'')Begin Insert into tblChngAgtProv values('P', @InsCode, 'Street', @StreetOld, @Street, Getdate(),Substring(System_user,7,30), @Note)EndWhen it runs, I got an error:A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.Anyone would help?
--------------------keeping it simple... |
 |
|
|
|
|
|