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: -
USE [RiskAnalysis]
-
GO
-
SET ANSI_NULLS ON
-
GO
-
SET QUOTED_IDENTIFIER ON
-
GO
-
-
alter PROCEDURE [dbo].[usp_RiskArchive]
-
-
AS
-
-
SET NOCOUNT ON
-
-
IF NOT EXISTS
-
(SELECT RiskArchive.RiskID, RiskArchive.Criticality
-
FROM RiskArchive, Risk
-
WHERE RiskArchive.RiskID = Risk.RiskID AND RiskArchive.Criticality = Risk.Criticality)
-
-
if Risk.MitigationPlan = 'See action'
-
INSERT INTO RiskArchive (RiskID, Criticality, MitigationActionID)
-
SELECT Risk.RiskID, case when Risk.Criticality <> '' then Risk.Criticality else '' end, Mitigation.MitigationActionID
-
FROM Risk, Mitigation
-
WHERE Mitigation.RiskID = Risk.RiskID
-
else
-
INSERT INTO RiskArchive (RiskID, Criticality)
-
SELECT Risk.RiskID, case when Risk.Criticality <> '' then Risk.Criticality else '' end
-
FROM Risk, Mitigation
-
-
set nocount off
-
Any help on this will be much appreciated,
Andrew
3 5987
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
ck9663 2,878
Recognized Expert Specialist
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: -
USE [RiskAnalysis]
-
GO
-
SET ANSI_NULLS ON
-
GO
-
SET QUOTED_IDENTIFIER ON
-
GO
-
-
alter PROCEDURE [dbo].[usp_RiskArchive]
-
-
AS
-
-
SET NOCOUNT ON
-
-
IF NOT EXISTS
-
(SELECT RiskArchive.RiskID, RiskArchive.Criticality
-
FROM RiskArchive, Risk
-
WHERE RiskArchive.RiskID = Risk.RiskID AND RiskArchive.Criticality = Risk.Criticality)
-
-
if Risk.MitigationPlan = 'See action'
-
INSERT INTO RiskArchive (RiskID, Criticality, MitigationActionID)
-
SELECT Risk.RiskID, case when Risk.Criticality <> '' then Risk.Criticality else '' end, Mitigation.MitigationActionID
-
FROM Risk, Mitigation
-
WHERE Mitigation.RiskID = Risk.RiskID
-
else
-
INSERT INTO RiskArchive (RiskID, Criticality)
-
SELECT Risk.RiskID, case when Risk.Criticality <> '' then Risk.Criticality else '' end
-
FROM Risk, Mitigation
-
-
set nocount off
-
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: ajikoe |
last post by:
Hello,
Is anyone has experiance in running python code to run multi thread
parallel in multi processor. Is it possible ?
Can python manage which cpu shoud do every thread?
Sincerely Yours,...
|
by: Frank Jona |
last post by:
Intellisense with C# and a multi-file assembly is not
working. With VB.NET it is working.
Is there a fix availible?
We're using VisualStudio 2003
Regards
Frank
|
by: * ProteanThread * |
last post by:
but depends upon the clique:
...
|
by: frankenberry |
last post by:
I have multi-page tiff files. I need to extract individual frames from the
multi-page tiffs and save them as single-page tiffs. 95% of the time I
receive multi-page tiffs containing 1 or more black...
|
by: cody |
last post by:
What are multi file assemblies good for?
What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a
single multi file assembly (A.DLL+A.NETMODULE)?
| |
by: Joe |
last post by:
I have 2 multi-list boxes, 1 displays course categories based on a
table called CATEGORIES. This table has 2 fields CATEGORY_ID,
CATEGORY_NAME
The other multi-list box displays courses based on...
|
by: mimmo |
last post by:
Hi! I should convert the accented letters of a string in the correspondent
letters not accented. But when I compile with -Wall it give me:
warning: multi-character character constant
Do the...
|
by: Shane Story |
last post by:
I can seem to get the dimensions of a frame in a multiframe tiff.
After selecting activeframe, the Width/Height is still really much larger
than the page's actual dimensions.
When I split a...
|
by: bobwansink |
last post by:
Hi, I'm relatively new to programming and I would like to create a C++
multi user program. It's for a project for school. This means I will
have to write a paper about the theory too. Does anyone...
|
by: Sabri.Pllana |
last post by:
We apologize if you receive multiple copies of this call for papers.
***********************************************************************
2008 International Workshop on Multi-Core Computing...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |