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
 General SQL Server Forums
 New to SQL Server Programming
 SELECT & SCOPE_IDENTITY() -- quick question

Author  Topic 

walkingInMud
Starting Member

1 Post

Posted - 2008-11-02 : 11:30:34
What do the last two lines do? and/or What are they for?

(NOTE: CustomerID is a generated primary key and Modified is a generated timestamp)

Thanks heaps!



--inserts a Customer row
INSERT INTO Customer
(LastName, FirstName, Address, City, PostCode
VALUES
(@LastName, @FirstName, @Address, @City, @PostCode);


IF @@ROWCOUNT>0 AND @@ERROR=0 --checks if the last statement produces an error

--reselect the two generated values from the current calling of this procedure

SELECT @customerID = customerID, @Modifed = Modified FROM Customer WHERE (CustomerID = SCOPE_IDENTITY());


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-02 : 11:38:36
it checks if there was at least one rows inserted (@@ROWCOUNT gives number of rows affected by previous operation (INSERT) and @@ERROR returns error if any occured (and 0 if no error)). so what IF does is check if at least one row was inserted successfully without any error.
and last select returns the inserted values (customerID,Modified) by checking the ID value equal to last generated id value (SCOPE_IDENTITY() gives last generated id value).
Go to Top of Page
   

- Advertisement -