By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,191 Members | 1,411 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,191 IT Pros & Developers. It's quick & easy.

The multi-part identifier "..." could not be bound

P: 20
Hi,

I am using SQL Server 2005. I have this stored procedure below but it doesn't work. It is for an archiving process I am currently implementing. The RiskArchive table has fields ArchiveID (primary key), RiskID, Criticality, MitigationActionID and ArchiveMonth (datetime data type). What I am trying to do is take the RiskID from the Risk table, the Criticality (a number showing how severe the risk is) for that risk and also any mitigations (MitigationActionID from the table Mitigation) for that risk. There can be many mitigations for each risk. In the Mitigation table, there is a RiskID field so that you can see which risk it belongs to.

First of all, I had this working at one stage. It was working before I put in the lines "if Risk.MitigationPlan = 'See action'", the "else" and the 3 lines of code after the else. But what I realised is that some risks might not have a mitigation when the stored procedure is run, so I implemented the if..else block that you see. The error that I get is:

Msg 4104, Level 16, State 1, Procedure usp_RiskArchive, Line 13
The multi-part identifier "Risk.MitigationPlan" could not be bound.

This error is on the if line, just before the insert into. Basically, when a mitigation has been entered for a risk, the MitigationPlan field for that risk is changed from None to See detail so that the user can see there are mitigations on that risk. So what I tried to do is when the MitigationPlan field for a risk is equal to See action, then insert the RiskID, Criticality and the MitigationActionID for that risk into the table RiskArchive, otherwise only insert the RiskID and Criticality (with the MitigationActionID field being left blank (null)). Note that this is what I want as I want a record of if there wasn't mitigations before, but are now, I can see when there wasn't any (by ArchiveMonth). The ArchiveMonth has a default value of getdate(). Here is the stored procedure:

Expand|Select|Wrap|Line Numbers
  1. USE [RiskAnalysis]
  2. GO
  3. SET ANSI_NULLS ON
  4. GO
  5. SET QUOTED_IDENTIFIER ON
  6. GO
  7.  
  8. alter PROCEDURE [dbo].[usp_RiskArchive] 
  9.  
  10. AS
  11.  
  12. SET NOCOUNT ON
  13.  
  14. IF NOT EXISTS
  15.     (SELECT RiskArchive.RiskID, RiskArchive.Criticality
  16.      FROM RiskArchive, Risk
  17.      WHERE RiskArchive.RiskID = Risk.RiskID AND RiskArchive.Criticality = Risk.Criticality)
  18.  
  19.     if Risk.MitigationPlan = 'See action'
  20.         INSERT INTO RiskArchive (RiskID, Criticality, MitigationActionID)
  21.         SELECT     Risk.RiskID, case when Risk.Criticality <> '' then Risk.Criticality else '' end, Mitigation.MitigationActionID
  22.         FROM Risk, Mitigation
  23.         WHERE Mitigation.RiskID = Risk.RiskID
  24.     else
  25.         INSERT INTO RiskArchive (RiskID, Criticality)
  26.         SELECT     Risk.RiskID, case when Risk.Criticality <> '' then Risk.Criticality else '' end
  27.         FROM Risk, Mitigation
  28.  
  29. set nocount off
  30.  
Any help on this will be much appreciated,

Andrew
Apr 11 '08 #1
Share this Question
Share on Google+
3 Replies


100+
P: 293
Hi,

It won't be possible in any case to check the value in the table just by writing
TableName.ColumnName at least in the scenario you have written since the compiler will look for the alias named "Risk".
The Useful solution over the scenario is to use CTE (Common table expression ) for the If..Then..else ..Part...You wrote about

Regards,
Mukta
Apr 11 '08 #2

ck9663
Expert 2.5K+
P: 2,878
Hi,

I am using SQL Server 2005. I have this stored procedure below but it doesn't work. It is for an archiving process I am currently implementing. The RiskArchive table has fields ArchiveID (primary key), RiskID, Criticality, MitigationActionID and ArchiveMonth (datetime data type). What I am trying to do is take the RiskID from the Risk table, the Criticality (a number showing how severe the risk is) for that risk and also any mitigations (MitigationActionID from the table Mitigation) for that risk. There can be many mitigations for each risk. In the Mitigation table, there is a RiskID field so that you can see which risk it belongs to.

First of all, I had this working at one stage. It was working before I put in the lines "if Risk.MitigationPlan = 'See action'", the "else" and the 3 lines of code after the else. But what I realised is that some risks might not have a mitigation when the stored procedure is run, so I implemented the if..else block that you see. The error that I get is:

Msg 4104, Level 16, State 1, Procedure usp_RiskArchive, Line 13
The multi-part identifier "Risk.MitigationPlan" could not be bound.

This error is on the if line, just before the insert into. Basically, when a mitigation has been entered for a risk, the MitigationPlan field for that risk is changed from None to See detail so that the user can see there are mitigations on that risk. So what I tried to do is when the MitigationPlan field for a risk is equal to See action, then insert the RiskID, Criticality and the MitigationActionID for that risk into the table RiskArchive, otherwise only insert the RiskID and Criticality (with the MitigationActionID field being left blank (null)). Note that this is what I want as I want a record of if there wasn't mitigations before, but are now, I can see when there wasn't any (by ArchiveMonth). The ArchiveMonth has a default value of getdate(). Here is the stored procedure:

Expand|Select|Wrap|Line Numbers
  1. USE [RiskAnalysis]
  2. GO
  3. SET ANSI_NULLS ON
  4. GO
  5. SET QUOTED_IDENTIFIER ON
  6. GO
  7.  
  8. alter PROCEDURE [dbo].[usp_RiskArchive] 
  9.  
  10. AS
  11.  
  12. SET NOCOUNT ON
  13.  
  14. IF NOT EXISTS
  15.     (SELECT RiskArchive.RiskID, RiskArchive.Criticality
  16.      FROM RiskArchive, Risk
  17.      WHERE RiskArchive.RiskID = Risk.RiskID AND RiskArchive.Criticality = Risk.Criticality)
  18.  
  19.     if Risk.MitigationPlan = 'See action'
  20.         INSERT INTO RiskArchive (RiskID, Criticality, MitigationActionID)
  21.         SELECT     Risk.RiskID, case when Risk.Criticality <> '' then Risk.Criticality else '' end, Mitigation.MitigationActionID
  22.         FROM Risk, Mitigation
  23.         WHERE Mitigation.RiskID = Risk.RiskID
  24.     else
  25.         INSERT INTO RiskArchive (RiskID, Criticality)
  26.         SELECT     Risk.RiskID, case when Risk.Criticality <> '' then Risk.Criticality else '' end
  27.         FROM Risk, Mitigation
  28.  
  29. set nocount off
  30.  
Any help on this will be much appreciated,

Andrew

Two options:

1. Store Risk.MitigationPlan into a variable.

2. Create a dynamic query.


You're having problem because there are no tables open. The code that you wrote is fine in some database (foxpro, dbase, etc). Some of these, will keep the table open and the record pointer pointed to a record and will stay there until you move it or close the table. This is not the case in sql-server. SELECT, generally speaking, is the only way to read the value on the table.

-- CK
Apr 11 '08 #3

P: 20
Im having a different problem with this stored procedure, but as its to do with a different error, I will make a new thread for it.
Apr 15 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.