• 0

[SQL] Insert error


Question

I'm having an interesting situation with a SQL Server proc. The issue is that the new data is getting into the "Restaurants" table, but only some data is making it into the "Partner_Has_Vendors" table. Specifcally there are 125 records in the initial test and only 24 ended up in the second table.

Any thoughts?

Here is some of the proc code:

BEGIN

SELECT @iCOUNT = COUNT(RST_UID)

FROM Restaurants.dbo.RESTAURANTS

WHERE RST_LONGITUDE = @fRestaurantLongitude

AND RST_LATITUDE = @fRestaurantLatitude

END

BEGIN

IF @iCount = 0

BEGIN

INSERT INTO Restaurants.dbo.RESTAURANTS (

RST_NAME,

RST_STREET,

RST_CITY,

RST_STT_UID,

RST_ZIP,

RST_ZIP4,

RST_PHONE,

RST_DELIVERY,

RST_TAKEOUT,

RST_LONGITUDE,

RST_LATITUDE,

RST_PARTNERS_UID,

RST_ON_WEB,

RST_TRACKING,

RST_TRACKING_FAX)

VALUES (

@sRestaurantName,

@sRestaurantAddress,

@sRestaurantCity,

@iStateID,

@sRestaurantZIPCode,

@sRestaurantZIPCodePlusFour,

@sRestaurantPhone,

@bDelivery,

@bTakeOut,

@fRestaurantLongitude,

@fRestaurantLatitude,

@iRestaurantID,

@iRestaurantOnline,

1,

0)

SELECT @iRestaurantID = @@IDENTITY

INSERT INTO Customers.dbo.PARTNER_HAS_VENDORS (

PHV_PTR_UID,

PHV_RST_UID,

PHV_LEVEL1_SALES,

PHV_LEVEL1_COMMISSION)

VALUES (

@iPartnerID,

@iRestaurantID,

0,

0)

-- IMPORTANT: Do we have have values for these two fields???

END

ELSE

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

are the ones being inserted different in some way?

all the same restaurant id?

where does partnerid come from?

it quite hard to sort this out without the whole sproc, data types used, and also some sample data. if you prepare a zip of all of them i take a look.

Link to comment
Share on other sites

  • 0

If you have triggers inserting data then your iRestaurantID won't be correct.

SELECT @@IDENTITY

It returns the last IDENTITY value produced on a connection, regardless of the table that produced the value, and regardless of the scope of the statement that produced the value.

@@IDENTITY will return the last identity value entered into a table in your current session. While @@IDENTITY is limited to the current session, it is not limited to the current scope. If you have a trigger on a table that causes an identity to be created in another table, you will get the identity that was created last, even if it was the trigger that created it.

SELECT SCOPE_IDENTITY()

It returns the last IDENTITY value produced on a connection and by a statement in the same scope, regardless of the table that produced the value.

SCOPE_IDENTITY(), like @@IDENTITY, will return the last identity value created in the current session, but it will also limit it to your current scope as well. In other words, it will return the last identity value that you explicitly created, rather than any identity that was created by a trigger or a user defined function.

Link to comment
Share on other sites

  • 0

oh, don't use triggers they are awful to maintain :)

unless you are:

a) modifying an existing 3rd party application and you want to support future upgrades

b) update a change date / changed by field

c) have absolutely no other choice

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.