473,386 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

SQL Server 2005: TRIGGER AFTER INSERT

Hello,
I am learning SQL Server 2005.
I need to create a trigger which increments number of book's
publications:

CREATE TRIGGER InsertPublication
ON Publications
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Num smallint
SET @Num = SELECT NumPublications FROM Books WHERE ISBN IN
(SELECT ISBN FROM inserted);
UPDATE Books
SET NumPublications = @Num + 1
WHERE ISBN IN
(SELECT ISBN FROM inserted);
END

Unfortunately I receive a message:

Incorrect syntax near the keyword 'SELECT'.

Could you explain me please how to correct the code?
I am new to SQL Server.
Thank you very much.
/RAM/
Mar 22 '06 #1
2 124923

R.A.M. wrote:
Hello,
I am learning SQL Server 2005.
I need to create a trigger which increments number of book's
publications:

CREATE TRIGGER InsertPublication
ON Publications
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Num smallint
SET @Num = SELECT NumPublications FROM Books WHERE ISBN IN
(SELECT ISBN FROM inserted);
UPDATE Books
SET NumPublications = @Num + 1
WHERE ISBN IN
(SELECT ISBN FROM inserted);
END

Unfortunately I receive a message:

Incorrect syntax near the keyword 'SELECT'.

Could you explain me please how to correct the code?
I am new to SQL Server.
Thank you very much.
/RAM/

CREATE TRIGGER InsertPublication
ON Publications
AFTER INSERT
AS
BEGIN
UPDATE Books
SET NumPublications = NumPublications + 1
FROM Books, inserted
WHERE Books.ISBN = inserted.ISBN
END

Mar 22 '06 #2
R.A.M. (r_********@poczta.onet.pl) writes:
I am learning SQL Server 2005.
I need to create a trigger which increments number of book's
publications:

CREATE TRIGGER InsertPublication
ON Publications
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Num smallint
SET @Num = SELECT NumPublications FROM Books WHERE ISBN IN
(SELECT ISBN FROM inserted);
UPDATE Books
SET NumPublications = @Num + 1
WHERE ISBN IN
(SELECT ISBN FROM inserted);
END

Unfortunately I receive a message:

Incorrect syntax near the keyword 'SELECT'.

Could you explain me please how to correct the code?


The syntax error is that when you use a SELECT statement to return
a value in a expression it must be in parentheses:

SET @Num = (SELECT NumPublications FROM Books WHERE ISBN IN
(SELECT ISBN FROM inserted));

You can also write:

SELECT @Num = NumPublications FROM Books WHERE ISBN IN
(SELECT ISBN FROM inserted);

This syntax is older and a bit proprietary. It has the advantage that
you can assign severak variables at the time, but behaviour when no
rows match may take you by surprise. (It leaves the variable unchanged.)

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: DTB | last post by:
I am having trouble creating an INSTEAD OF trigger in SQL Server to replicate a BEFORE UPDATE trigger from ORACLE. Here is a sample of the ORACLE BEFORE UPDATE trigger: CREATE TRIGGER myTRIGGER ON...
2
by: Elvira Zeinalova | last post by:
Hei, We have 2 MS SQL SERVER 2000 installed on 2 different servers (2 separated machines). I am triing to connect them så that when one row is added to the table in the database in main server - ...
17
by: Trevor Best | last post by:
I don't know if this has been reported before but it appears to be a bug with Access. If I create two tables both with an identity column then create an insert trigger on table1 that inserts a...
6
by: fumanchu | last post by:
I've got to let end users (really just one person) load billing batch files into a third party app table. They need to specify the billing cycle name, the batch name, and the input file name and...
2
by: dba_222 | last post by:
Dear Experts, I'm an Oracle guy, who is being given more SQL Server assignments lately. I've been looking for things on the web about this, but I can't anything so far. In Oracle, I you...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
1
by: Zeljko Bilandzija | last post by:
Hello! I have a problem, and I looking for help if someone can handle this. I use asp.net 2.0, and I create web site which support users from internet (Web Site Administration Tool) and with...
8
by: Tony Toews [MVP] | last post by:
Thanks to a posting by fellow MVP Steve Foster On a computer that is running Windows Vista, Windows Server 2008, or Windows XP, an incorrect value is returned when an application queries the...
0
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$) { } ...
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.