User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 401,628 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,904 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 1121 | Replies: 2 | Solved
Reply
Join Date: Jan 2008
Posts: 19
Reputation: J'Tok is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
J'Tok J'Tok is offline Offline
Newbie Poster

Help StoredProcedure @variable = table.field not working

  #1  
Jan 25th, 2008
I am using SQL Server 2005 to run a stored procedure to update, delete, or insert depending on some conditions.
The problem is that it gives the following error:
Msg 4104, Level 16, State 1, Line 49
The multi-part identifier "PART.ID" could not be bound.

I have it inserting some useless data right now to test it. The problem lies with determining If @PrtID1 = PART.ID
Here is a copy of the stored procedure:


DECLARE @ProblemID int
DECLARE	@PartDescription1 nvarchar(100)
DECLARE	@PartCost1 money
DECLARE @PartCharge1 money
DECLARE @PartOrderedFrom1 nvarchar(40)
DECLARE @PartOrderNumber1 nvarchar(20)
DECLARE @PartDescription2 nvarchar(100)
DECLARE @PartCost2 money
DECLARE @PartCharge2 money
DECLARE @PartOrderedFrom2 nvarchar(40)
DECLARE @PartOrderNumber2 nvarchar(20)
DECLARE @PrtID1 int
DECLARE @PrtID2 int
DECLARE @PartDescription3 nvarchar(100)
DECLARE @PartCost3 money
DECLARE @PartCharge3 money
DECLARE @PartOrderedFrom3 nvarchar(40)
DECLARE @PartOrderNumber3 nvarchar(30)
DECLARE @PrtID3 int



Select @PartDescription1 = 'PleaseWork'
Select @PartCost1 = 12.00
Select @PartCharge1 = 12.00
Select @PartOrderedFrom1 = 'BooYA!'
Select @PartOrderNumber1 = 'abc123'
Select @PrtID1 = 16

Select @PartDescription2 = NULL
Select @PartCost2 = NULL
Select @PartCharge2 = NULL
Select @PartOrderedFrom2 = NULL
Select @PartOrderNumber2 = NULL
Select @PrtID2 = 19
Select @ProblemID = 11

Select @PartDescription3 = 'Three'
Select @PartCost3 = 1.00
Select @PartCharge3 = 3.00
Select @PartOrderedFrom3 = 'Hyper'
Select @PartOrderNumber3 = 'N/A'
Select @ProblemID = 11



	DECLARE @PartID1 char(10)
	If @PrtID1 IS NOT NULL AND @PrtID1 = PART.ID AND @PartDescription1 IS NOT NULL AND @PartCost1 IS NOT NULL
BEGIN
		Update PART
		Set PartDescription = @PartDescription1, PartCost = @PartCost1, PartCharge = @PartCharge1, OrderedFrom = @PartOrderedFrom1, OrderNumber = @PartOrderNumber1
		Where ID = @PrtID1
		Select @PartID1=@PrtID1
END
	Else If @PrtID1 IS NOT NULL AND @PrtID1 = PART.ID AND @PartDescription1 IS NULL
BEGIN
		Delete From PROBLEM_PART Where PROBLEM_ID = @ProblemID AND PART_ID = @PrtID1
		Delete From PART Where ID = @PrtID1
		Select @PartID1=@PrtID1
END
	Else If @PrtID1 IS NOT NULL AND @PrtID1 != PART.ID AND @PartDescription1 IS NOT NULL AND @PartCost1 IS NOT NULL
BEGIN
		Insert PART (PartDescription,PartCost,PartCharge,OrderedFrom,OrderNumber) Values(@PartDescription1,@PartCost1,@PartCharge1,@PartOrderedFrom1,@PartOrderNumber1)
		Select @PartID1=@@Identity
END



	DECLARE @PartID2 char(10)
	If @PrtID2 IS NOT NULL AND @PrtID2 = PART.ID AND @PartDescription2 IS NOT NULL AND @PartCost2 IS NOT NULL
BEGIN
		Update PART
		Set PartDescription = @PartDescription2, PartCost = @PartCost2, PartCharge = @PartCharge2, OrderedFrom = @PartOrderedFrom2, OrderNumber = @PartOrderNumber2
		Where ID = @PrtID2
		Select @PartID2=@PrtID2
END
	Else If @PrtID2 IS NOT NULL AND @PrtID2 = PART.ID AND @PartDescription2 IS NULL
BEGIN
		Delete From PROBLEM_PART Where PROBLEM_ID = @ProblemID AND PART_ID = @PrtID2
		Delete From PART Where ID = @PrtID2
		Select @PartID2=@PrtID2
END
	Else If @PrtID2 IS NOT NULL AND @PrtID2 != PART.ID AND @PartDescription2 IS NOT NULL AND @PartCost2 IS NOT NULL
BEGIN
		Insert PART (PartDescription,PartCost,PartCharge,OrderedFrom,OrderNumber) Values(@PartDescription2,@PartCost2,@PartCharge2,@PartOrderedFrom2,@PartOrderNumber2)
		Select @PartID2=@@Identity
END


	DECLARE @PartID3 char(10)
	If @PrtID3 IS NOT NULL AND @PrtID3 = PART.ID AND @PartDescription3 IS NOT NULL AND @PartCost3 IS NOT NULL
BEGIN
		Update PART
		Set PartDescription = @PartDescription3, PartCost = @PartCost3, PartCharge = @PartCharge3, OrderedFrom = @PartOrderedFrom3, OrderNumber = @PartOrderNumber3
		Where ID = @PrtID3
		Select @PartID3=@PrtID3
END
	Else If @PrtID3 IS NOT NULL AND @PrtID3 = PART.ID AND @PartDescription3 IS NULL
BEGIN
		Delete From PROBLEM_PART Where PROBLEM_ID = @ProblemID AND PART_ID = @PrtID3
		Delete From PART Where ID = @PrtID3
		Select @PartID3=@PrtID3
END
	Else If @PrtID3 IS NOT NULL AND @PrtID3 != PART.ID AND @PartDescription3 IS NOT NULL AND @PartCost3 IS NOT NULL
BEGIN
		Insert PART (PartDescription,PartCost,PartCharge,OrderedFrom,OrderNumber) Values(@PartDescription3,@PartCost3,@PartCharge3,@PartOrderedFrom3,@PartOrderNumber3)
		Select @PartID3=@@Identity
END




	If @PartID1 != PROBLEM_PART.PART_ID AND @PartDescription1 IS NOT NULL AND @PartCost1 IS NOT NULL
		Insert PROBLEM_PART Values(@ProblemID,@PartID1)
	If @PartID2 != PROBLEM_PART.PART_ID AND @PartDescription2 IS NOT NULL AND @PartCost2 IS NOT NULL
		Insert PROBLEM_PART Values(@ProblemID,@PartID2)
	If @PartID3 != PROBLEM_PART.PART_ID AND @PartDescription3 IS NOT NULL AND @PartCost3 IS NOT NULL
		Insert PROBLEM_PART Values(@ProblemID,@PartID3)

Thanks,
J'Tok
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2008
Posts: 19
Reputation: J'Tok is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
J'Tok J'Tok is offline Offline
Newbie Poster

Re: StoredProcedure @variable = table.field not working

  #2  
Jan 26th, 2008
I think I'm not asking the question correctly, or simply enough.
What I need to do in this stored procedure is see if the part ID variables I'm passing into it already exist in the PART table. Hence:
@PrtID1 = PART.ID

I'm just not sure how to make it do this.

- J'Tok

Edit: Do I actually need to be doing this in the where clause?
Last edited by J'Tok : Jan 26th, 2008 at 11:40 am.
Reply With Quote  
Join Date: Jan 2008
Posts: 19
Reputation: J'Tok is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
J'Tok J'Tok is offline Offline
Newbie Poster

Solution Re: StoredProcedure @variable = table.field not working

  #3  
Jan 26th, 2008
Finally figured it out! My approach was all wrong. Here's what I came up with:


DECLARE @ProblemID int
DECLARE @PartDescription1 nvarchar(100)
DECLARE @PartCost1 money
DECLARE @PartCharge1 money
DECLARE @PartOrderedFrom1 nvarchar(40)
DECLARE @PartOrderNumber1 nvarchar(20)
DECLARE @PartDescription2 nvarchar(100)
DECLARE @PartCost2 money
DECLARE @PartCharge2 money
DECLARE @PartOrderedFrom2 nvarchar(40)
DECLARE @PartOrderNumber2 nvarchar(20)
DECLARE @PrtID1 int
DECLARE @PrtID2 int
DECLARE @PartDescription3 nvarchar(100)
DECLARE @PartCost3 money
DECLARE @PartCharge3 money
DECLARE @PartOrderedFrom3 nvarchar(40)
DECLARE @PartOrderNumber3 nvarchar(30)
DECLARE @PrtID3 int


Select @PartDescription1 = 'PleaseWork'
Select @PartCost1 = 12.00
Select @PartCharge1 = 12.00
Select @PartOrderedFrom1 = 'BooYA!'
Select @PartOrderNumber1 = 'abc123'
Select @PrtID1 = 16

Select @PartDescription2 = NULL
Select @PartCost2 = NULL
Select @PartCharge2 = NULL
Select @PartOrderedFrom2 = NULL
Select @PartOrderNumber2 = NULL
Select @PrtID2 = 19
Select @ProblemID = 11

Select @PartDescription3 = 'Three'
Select @PartCost3 = 1.00
Select @PartCharge3 = 3.00
Select @PartOrderedFrom3 = 'Hyper'
Select @PartOrderNumber3 = 'N/A'


DECLARE @PartID1 char(10)
If @PrtID1 IS NOT NULL AND @PartDescription1 IS NOT NULL AND @PartCost1 IS NOT NULL
BEGIN
Update PART
Set PartDescription = @PartDescription1, PartCost = @PartCost1, PartCharge = @PartCharge1, OrderedFrom = @PartOrderedFrom1, OrderNumber = @PartOrderNumber1
Where ID = @PrtID1
Select @PartID1=@PrtID1
END
Else If @@ROWCOUNT = 0 AND @PrtID1 IS NULL AND @PartDescription1 IS NOT NULL AND @PartCost1 IS NOT NULL
BEGIN
Insert PART (PartDescription,PartCost,PartCharge,OrderedFrom,OrderNumber) Values(@PartDescription1,@PartCost1,@PartCharge1,@PartOrderedFrom1,@PartOrderNumber1)
Select @PartID1=@@Identity
Insert PROBLEM_PART Values(@ProblemID,@PartID1)
END
Else If @@ROWCOUNT = 0 AND @PrtID1 IS NOT NULL AND @PartDescription1 IS NULL
BEGIN
Delete From PROBLEM_PART Where PROBLEM_ID = @ProblemID AND PART_ID = @PrtID1
Delete From PART Where ID = @PrtID1
Select @PartID1=@PrtID1
END


DECLARE @PartID2 char(10)
If @PrtID2 IS NOT NULL AND @PartDescription2 IS NOT NULL AND @PartCost2 IS NOT NULL
BEGIN
Update PART
Set PartDescription = @PartDescription2, PartCost = @PartCost2, PartCharge = @PartCharge2, OrderedFrom = @PartOrderedFrom2, OrderNumber = @PartOrderNumber2
Where ID = @PrtID2
Select @PartID2=@PrtID2
END
Else If @@ROWCOUNT = 0 AND @PrtID2 IS NULL AND @PartDescription2 IS NOT NULL AND @PartCost2 IS NOT NULL
BEGIN
Insert PART (PartDescription,PartCost,PartCharge,OrderedFrom,OrderNumber) Values(@PartDescription2,@PartCost2,@PartCharge2,@PartOrderedFrom2,@PartOrderNumber2)
Select @PartID2=@@Identity
Insert PROBLEM_PART Values(@ProblemID,@PartID2)
END
Else If @@ROWCOUNT = 0 AND @PrtID2 IS NOT NULL AND @PartDescription2 IS NULL
BEGIN
Delete From PROBLEM_PART Where PROBLEM_ID = @ProblemID AND PART_ID = @PrtID2
Delete From PART Where ID = @PrtID2
Select @PartID2=@PrtID2
END



DECLARE @PartID3 char(10)
If @PrtID3 IS NOT NULL AND @PartDescription3 IS NOT NULL AND @PartCost3 IS NOT NULL
BEGIN
Update PART
Set PartDescription = @PartDescription3, PartCost = @PartCost3, PartCharge = @PartCharge3, OrderedFrom = @PartOrderedFrom3, OrderNumber = @PartOrderNumber3
Where ID = @PrtID3
Select @PartID3=@PrtID3
END
Else If @@ROWCOUNT = 0 AND @PrtID3 IS NULL AND @PartDescription3 IS NOT NULL AND @PartCost3 IS NOT NULL
BEGIN
Insert PART (PartDescription,PartCost,PartCharge,OrderedFrom,OrderNumber) Values(@PartDescription3,@PartCost3,@PartCharge3,@PartOrderedFrom3,@PartOrderNumber3)
Select @PartID3=@@Identity
Insert PROBLEM_PART Values(@ProblemID,@PartID3)
END
Else If @@ROWCOUNT = 0 AND @PrtID3 IS NOT NULL AND @PartDescription3 IS NULL
BEGIN
Delete From PROBLEM_PART Where PROBLEM_ID = @ProblemID AND PART_ID = @PrtID3
Delete From PART Where ID = @PrtID3
Select @PartID3=@PrtID3
END



- J'Tok
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb MS SQL Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 5:56 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC