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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Stored Procedure Insert/Update Issue

Author  Topic 

rankone
Starting Member

24 Posts

Posted - 2012-04-16 : 22:20:04
Hi,

I have two tables that are of the same structure, the only difference is one is the main table and the other one stores the input from the application. I want to use the main table as a reference in my query in order to help my application.

Table 1 (Main Table)(dbo.MainMaster):
User ID Price
2 5401 62.00
2 6921 61.00
2 2215 49.00
3 3210 89.00
3 2425 99.00

Table 2 (Used By Application)(dbo.PriceTest):
User ID Price
2 5401 60.00
2 6921 61.55
2 2215 49.00
3 3210 91.00
3 2425 99.00

What I am trying to achieve is IF a record exists in Table 2 for the specific user on the same ID with the same price then to update the Price in column in Table 2 with the price for that ID and User in Table 1. I am not sure if I go the Update route or the select into route. But so Table 2 is getting populated through the following stored procedure that I want to modify in order to reference Table 1.

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[AppInsert]
@User int
,@ID int
,@price money

AS
DELETE FROM dbo.PriceTest
WHERE User = @User
AND ID = @ID

INSERT INTO dbo.PriceTest
VALUES(@User,@ID,@price)


Any help will be appreciated
Thanks

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-04-17 : 02:30:25
You are not clear: IF a record exists in Table 2 for the specific user on the same ID with the same price

Do you mean with a different price?

Do you want a stored procedure to update all (different price) records or only for given parameters to the stored procedure?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

rankone
Starting Member

24 Posts

Posted - 2012-04-17 : 09:45:56
Yea if the user is trying to insert into Table 2 a record that already exists then to reference and insert the price from Table 1 for that specific user and id.
Go to Top of Page
   

- Advertisement -