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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 variable problem

Author  Topic 

TAS
Yak Posting Veteran

65 Posts

Posted - 2005-04-15 : 10:19:37
I have a INSERT, UPDATE, DELETE trigger as follows



CREATE TRIGGER [trigInvUpdate] ON [dbo].[tblInv]
FOR INSERT, UPDATE, DELETE
AS
Declare @InsCode varchar(5), @InsCodeOld varchar(5)
Declare @InsCodeInclude varchar(5), @InsCodeIncludeOld varchar(5)
Declare @InsCodeExclude varchar(5), @InsCodeExcludeOld varchar(5)
Declare @InvCal int, @InvCalOld int
Declare @RateVar decimal, @RateVarOld decimal
Declare @RateFixed decimal, @RateFixedOld decimal
Declare @LimitLow decimal, @LimitLowOld decimal
Declare @LimitHigh decimal, @LimitHighOld decimal
Declare @FirstPay smalldatetime, @FirstPayOld smalldatetime
Declare @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 bit
Declare @InsertCount int, @DeleteCount int
Declare @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)
End


If ISNULL(@Street,'') <>ISNULL(@StreetOld,'')
Begin
Insert into tblChngAgtProv values('P', @InsCode, 'Street', @StreetOld, @Street, Getdate(),Substring(System_user,7,30), @Note)

End




When 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 TRIGGER

Personally 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_TYPE

And move the entire row

And lose the FRO INSERT piece...the new row will be stored in your current table...

MOO


Brett

8-)
Go to Top of Page

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 = value3

Not 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 correctly
Begin
INSERT INTO tblChngAgtProv VALUES('P', @InsCode, 'Provider Code', @InsCodeOld, @InsCode, Getdate(),Substring(System_user,7,30), @Note)
End

You 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"
Go to Top of Page

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2005-04-18 : 00:55:52
To answer your question regarding error, see text in red

quote:
Originally posted by TAS

I have a INSERT, UPDATE, DELETE trigger as follows



CREATE TRIGGER [trigInvUpdate] ON [dbo].[tblInv]
FOR INSERT, UPDATE, DELETE
AS
Declare @InsCode varchar(5), @InsCodeOld varchar(5)
Declare @InsCodeInclude varchar(5), @InsCodeIncludeOld varchar(5)
Declare @InsCodeExclude varchar(5), @InsCodeExcludeOld varchar(5)
Declare @InvCal int, @InvCalOld int
Declare @RateVar decimal, @RateVarOld decimal
Declare @RateFixed decimal, @RateFixedOld decimal
Declare @LimitLow decimal, @LimitLowOld decimal
Declare @LimitHigh decimal, @LimitHighOld decimal
Declare @FirstPay smalldatetime, @FirstPayOld smalldatetime
Declare @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 bit
Declare @InsertCount int, @DeleteCount int
Declare @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)
End


If ISNULL(@Street,'') <>ISNULL(@StreetOld,'')
Begin
Insert into tblChngAgtProv values('P', @InsCode, 'Street', @StreetOld, @Street, Getdate(),Substring(System_user,7,30), @Note)

End




When 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...
Go to Top of Page
   

- Advertisement -