Hello all, I have a legacy database on MSSQL 2005 Express where I need to do a complicated delete. I need to know if the following is possible and if it is what is wrong with my code.
Concept of Schema.
Database holds information on members of staff based at many care homes within an organisation. I need to delete any reference to staff members based at home number 7.
Staff members do not have a single column unique identifier, they are identified by a composite key of HID (Home unique identifier) and StaffID.
To complicate this situation when the database was coded there are 2 different ways HID columns are named so HID columns can either be called HID or HID_ID
Progress.
The first step I have taken is to find all tables with the StaffID in it and to find out how the HID column is named using the following function:-
The above provides me with a table with 2 cols first table name and secondly HIDname.
I intend to use this function as part of a stored procedure that will then loop through this table and delete from each table rows with a HID or HID_ID equal to 7.
The following is the procedure I have created to do this.
The problem is that when trying to run this create statement I get the following errors:-
[red]
Msg 1087, Level 15, State 2, Procedure remStaff, Line 17
Must declare the table variable "@TblName".
Msg 102, Level 15, State 1, Procedure remStaff, Line 23
Incorrect syntax near 't_cursor'.
[/red]
I'm not that experienced with TSQL and this seems the most obvious way of getting the desired result.
Is there some special Character to enclose my variable in or is there some other completely different way of achieving the desired result.
Thanks in advance for any help that anyone can give me with this problem.
Normm (lee)
P.S. the database was originally converted from a Clarion based TPS system (Not by me) so it has absolutely no referencial integrity constraints and only some tables have defined primary keys that are only on a single RecordId column.
Concept of Schema.
Database holds information on members of staff based at many care homes within an organisation. I need to delete any reference to staff members based at home number 7.
Staff members do not have a single column unique identifier, they are identified by a composite key of HID (Home unique identifier) and StaffID.
To complicate this situation when the database was coded there are 2 different ways HID columns are named so HID columns can either be called HID or HID_ID
Progress.
The first step I have taken is to find all tables with the StaffID in it and to find out how the HID column is named using the following function:-
Code:
create FUNCTION HID_OR_HID_ID()
returns @REZ TABLE(TblName varchar(50), HIDName varchar(150))
AS
BEGIN
DECLARE @TblName varchar(50)
DECLARE @TMP TABLE(
TblName varchar(50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
HIDName varchar(10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
)
DECLARE t_cursor CURSOR
FOR
select TABLE_NAME from INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME = 'StaffID'
OPEN t_cursor
FETCH NEXT FROM t_cursor INTO @TblName
WHILE (@@FETCH_STATUS <> -1)
BEGIN
IF (@@FETCH_STATUS <> -2)
BEGIN
INSERT INTO @TMP
select @TblName, isc.COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS isc where isc.TABLE_NAME = @TblName and (isc.COLUMN_NAME = 'HID' OR isc.COLUMN_NAME = 'HID_ID')
END
FETCH NEXT FROM t_cursor INTO @TblName
END
CLOSE t_cursor
DEALLOCATE t_cursor
INSERT @REZ
select TblName, HIDName from @TMP
RETURN
END
The above provides me with a table with 2 cols first table name and secondly HIDname.
I intend to use this function as part of a stored procedure that will then loop through this table and delete from each table rows with a HID or HID_ID equal to 7.
The following is the procedure I have created to do this.
Code:
create procedure remStaff(@HID_ID_IN int)
as
begin
DECLARE @TblName varchar(50)
DECLARE @HIDName varchar(10)
DECLARE t_cursor CURSOR
FOR
select TblName, HIDName from HID_OR_HID_ID()
OPEN t_cursor
FETCH NEXT FROM t_cursor INTO @TblName, @HIDName
WHILE (@@FETCH_STATUS <> -1)
BEGIN
IF (@@FETCH_STATUS <> -2)
BEGIN
DELETE FROM @TblName WHERE @HIDName = @HID_ID_IN
END
FETCH NEXT FROM t_cursor INTO @TblName, @HIDName
END
CLOSE t_cursor
DEALLOCATE t_cursor
The problem is that when trying to run this create statement I get the following errors:-
[red]
Msg 1087, Level 15, State 2, Procedure remStaff, Line 17
Must declare the table variable "@TblName".
Msg 102, Level 15, State 1, Procedure remStaff, Line 23
Incorrect syntax near 't_cursor'.
[/red]
I'm not that experienced with TSQL and this seems the most obvious way of getting the desired result.
Is there some special Character to enclose my variable in or is there some other completely different way of achieving the desired result.
Thanks in advance for any help that anyone can give me with this problem.
Normm (lee)
P.S. the database was originally converted from a Clarion based TPS system (Not by me) so it has absolutely no referencial integrity constraints and only some tables have defined primary keys that are only on a single RecordId column.