Hi,
I'm having a problem with the following trigger:
CREATE TRIGGER tr_FamID
ON dbo.myFirstTbl
FOR INSERT
AS
DECLARE @address2 VARCHAR (50)
DECLARE @compareFamID INT
SELECT @address2 = Address FROM inserted
SELECT @compareFamID = FamID FROM inserted
IF @address2 = ' '
BEGIN
DROP TRIGGER tr_famID
END
IF @address2 != ' '
BEGIN
DECLARE @newRec int
DECLARE @newFam int
DECLARE @newLast VARCHAR(50)
DECLARE @newFirst VARCHAR(50)
DECLARE @newAddress VARCHAR(50)
SET @newRec = (SELECT RecID FROM myFirstTbl)
SET @newFam = (SELECT FamID FROM myFirstTbl)
SET @newLast = (SELECT LastName FROM myFirstTbl)
SET @newFirst = (SELECT FirstName FROM myFirstTbl)
SET @newAddress = (SELECT Address FROM myFirstTbl)
INSERT INTO dbName.dbo.mySecondTbl (FamID, LastName,
FirstName, Address) SELECT @newFam, @newLast, @newFirst,
@newAddress FROM myFirstTbl WHERE @newFam = @compareFamID
DELETE FROM myFirstTbl WHERE FamID = @compareFamID
END
The error that's giving me is that the subquery is returning more than one value...and I guess sql doesn't like that. What I want is to INSERT all of the records on the myFirstTbl table that have the same FamID (for example...WHERE FamID = '2') into the mySecondTbl table which is located in another database separate from the myFirstTbl table. After that's done, I would like to delete the records from the myFirstTbl table. So, because of this, I think it makes sense for the subquery to return more than 1 value, but it's erroring out. The information will be entered into the database using a Visual Basic.Net application (Visual Studio 2005). I would also like to add that i'm new to triggers so if i'm doing something that I should't be doing or that could hurt performance, please tell me.
I would really appreciate your help.
3 1230
Hi,
...
INSERT INTO dbName.dbo.mySecondTbl (FamID, LastName,
FirstName, Address) SELECT @newFam, @newLast, @newFirst,
@newAddress FROM myFirstTbl WHERE @newFam = @compareFamID
...
The error that's giving me is that the subquery is returning more than one value...and I guess sql doesn't like that.
...
I think the problem is that the inner SELECT is a bit malformed. I believe you meant to select columns from myFirstTable in which case you column names of myFirstTable in the select list not local variables (i.e. surely no @'s in it). It also applies to the left part of the WHERE clause, like this: -
NSERT INTO dbName.dbo.mySecondTbl (FamID, LastName,
-
FirstName, Address) SELECT myFirstTbl.FamID, myFirstTbl.LastName,
-
myFirstTbl.FirstName, myFirstTbl.Address FROM myFirstTbl WHERE myFirstTbl.FamID = @compareFamID
-
It is also an interesting action to drop the trigger from within the trigger. While to my recon it is OK for the SQL Server (syntax OK), I wouldn't use it myself. (The trigger may mysteriuosly disappear with an insert... If you do want to get rid of it, do it explicitly from the application code.)
thanks for your help man, it was very helpful. You were right, I didn't needed the variables. It worked by replacing the variables with the actual column names from the table.
As for DROPING the trigger, you were right again...it was a bad idea to include it.
thanks for your help man, it was very helpful. You were right, I didn't needed the variables. It worked by replacing the variables with the actual column names from the table.
As for DROPING the trigger, you were right again...it was a bad idea to include it.
or access the inserted table instead...trigger runs right after the insert, but it will not immediately commit the transactions...it will insert a similar record first on the inserted table, before it actually insert it to your myFirstTable...that's the best way to access the record that you are trying to insert..
Sign in to post your reply or Sign up for a free account.
Similar topics
by: mjm |
last post by:
Folks,
Stroustrup indicates that returning by value can be faster than
returning by reference but gives no details as to the size of the
returned object up to which this holds.
My question is...
|
by: Peter Frost |
last post by:
Please help
I don't know if this is possible but what I would really like to do is
to use On Error Goto to capture the code that is being executed when
an error occurs.
Any help would be much...
|
by: TD |
last post by:
This is the code under a command button -
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.BackColor <> RGB(255, 255, 255) Then
ctl.BackColor = RGB(255, 255, 255)
End If
Next ctl
|
by: Alberto Giménez |
last post by:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi there! I'm doing some homework (almost finished), but now i'm
reconsidering a design decission I made at the beginning. This is about
error...
|
by: jt |
last post by:
I'm getting a compiler error: warning C4172: returning address of local
variable or temporary
Here is the function that I have giving this error: I'm returning a
temporary char string and its...
|
by: suzy |
last post by:
i am trying to write aspx login system and the login process requires a
validity check of username and password.
i have been told that raising exception is costly, but i want a custom error...
|
by: p |
last post by:
WE had a Crystal 8 WebApp using vs 2002 which we upgraded to VS2003. I also
have Crystal 9 pro on my development machine. The web app runs fine on my
dev machine but am having problems deploying....
|
by: Ratfish |
last post by:
I'm getting a "2014:: Commands out of sync; you can't run this command
now" error on a php page when I try to call a second stored procedure
against a MySQL db. Does anyone know why I might be...
|
by: Conrad Lender |
last post by:
I'd like to hear your opinions about the appropriate way to deal with
non-critical errors that can occur in user-defined functions. For
example, an application chooses to extend String.prototype...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |