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 |
|
dbserver2000
Starting Member
47 Posts |
Posted - 2009-05-18 : 01:55:43
|
| Here is the StatementCREATE TRIGGER trgZipStateErrorON ZipstateINSTEAD OF INSERTASDECLARE @State char (2)DECLARE @Count intSET @State =(SELECT State FROM inserted)SET @Count =(SELECT Count (StateID)FROM State WHERE STATEID = @State)IF @Count = 0--Insert the data into the error tableBEGININSERT INTO ZipStateErrorSELECT ZipCode, State FROM insertedRAISERROR ('ZipstateError entry added' , 16, 10)WITH LOGENDELSE--insert the date into the regular tableINSERT INTO ZipState SELECT ZipCode, State, GetDate ( ) FROM insertedI get this error msg:Msg 8101, Level 16, State 1, Procedure trgZipStateError, Line 18An explicit value for the identity column in table 'ZipState' can only be specified when a column list is used and IDENTITY_INSERT is ON.Can not figure out what is wrong |
|
|
timmy
Master Smack Fu Yak Hacker
1242 Posts |
Posted - 2009-05-18 : 02:27:02
|
| It's telling you that you should be providing a list of columns in your insert statement:e.g. INSERT INTO ZipState (ZipCode, State) SELECT ......This is good practice anyway. You also need to turn IDENTITY_INSERT ON so you can insert a value into the identity column (although I don't know why a zip code would be an identity column unless you're running the database that allocates them....)HTH,Tim |
 |
|
|
dbserver2000
Starting Member
47 Posts |
Posted - 2009-05-18 : 16:42:35
|
Tim,Thank you, but I am lost.Have no idea how to do what you are asking me to.quote: Originally posted by timmy It's telling you that you should be providing a list of columns in your insert statement:e.g. INSERT INTO ZipState (ZipCode, State) SELECT ......This is good practice anyway. You also need to turn IDENTITY_INSERT ON so you can insert a value into the identity column (although I don't know why a zip code would be an identity column unless you're running the database that allocates them....)HTH,Tim
|
 |
|
|
AjarnMark
SQL Slashing Gunting Master
3246 Posts |
Posted - 2009-05-18 : 17:17:56
|
| Focus on the first part of Tim's response...What is the definition of the ZipState table? What field has the Identity property turned on?--------------------------------------------Brand yourself at EmeraldCityDomains.com |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-05-18 : 17:23:45
|
| >>SET @State =(SELECT State FROM inserted)In triggers you should never assume that only one row will be Inserted, Updated, or Deleted. If three different states is in inserted your @state variable will only hold one of them.Why are you using a trigger anyway? This should be done with a Foreign Key constraint.Be One with the OptimizerTG |
 |
|
|
|
|
|
|
|