Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

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/

Report this thread to moderator Post Follow-up to this message
Old Post
R.A.M.
03-22-06 06:30 PM


Re: SQL Server 2005: TRIGGER AFTER INSERT
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


Report this thread to moderator Post Follow-up to this message
Old Post
ZeldorBlat
03-23-06 01:33 AM


Re: SQL Server 2005: TRIGGER AFTER INSERT
R.A.M. (r_ahimsa_m@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, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

Report this thread to moderator Post Follow-up to this message
Old Post
Erland Sommarskog
03-23-06 01:33 AM


Sponsored Links





Last Thread Next Thread
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 09:48 AM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006