Before delete item from table check weather that item referring some table's value, if present then get table name:
ALTER PROCEDURE [dbo].[sp_CheckReferences]
(
@tableName varchar(50) ,
@considerPrimaryKey bit,
@consideringColumnName varchar(50),
@value varchar(20)
)
AS
BEGIN
declare @TableNames varchar(max)='';
--declare @ColumnNames varchar(max)='';
declare @tblTmp Table
(
rowID int identity(1,1),
foreign_key_name varchar(100),
foreign_table varchar(100),
foreign_column varchar(100),
parent_table varchar(100),
parent_column varchar(100)
);
--declare @errorintables TABLE
--(
-- foreign_table varchar(50),
-- foreign_column varchar(50)
--);
INSERT INTO @tblTmp
select * from
(
select cast(f.name as varchar(255)) as foreign_key_name
, cast(c.name as varchar(255)) as foreign_table
, cast(fc.name as varchar(255)) as foreign_column
, cast(p.name as varchar(255)) as parent_table
, cast(rc.name as varchar(255)) as parent_column
from sysobjects f
inner join sysobjects c on f.parent_obj = c.id
inner join sysreferences r on f.id = r.constid
inner join sysobjects p on r.rkeyid = p.id
inner join syscolumns rc on r.rkeyid = rc.id and r.rkey1 = rc.colid
inner join syscolumns fc on r.fkeyid = fc.id and r.fkey1 = fc.colid
where f.type = 'F'
AND cast(p.name as varchar(255))=@tableName
) ss
declare @cnt int;
declare @loopcnt int=1;
declare @resultCount int;
declare @finalTableCount int = 0;
select @cnt = COUNT(*) from @tblTmp
WHILE(@loopcnt<=@cnt)
BEGIN
declare @sqltext nvarchar(max);
declare @ftable varchar(100);
declare @fcolumn varchar(100);
declare @pcolumn varchar(100);
select @ftable=foreign_table, @fcolumn=foreign_column,@pcolumn=parent_column from @tblTmp where rowID=@loopcnt;
set @sqltext = 'SELECT @resultCount=COUNT(*) FROM '+@tableName+' INNER JOIN '+@ftable+' ON '+@pcolumn+'='+@fcolumn;
if(@considerPrimaryKey = 1)
begin
SET @sqltext = @sqltext +' WHERE '+@tableName+'.'+@pcolumn+'='+@value;
end
else
begin
SET @sqltext = @sqltext +' WHERE '+@tableName+'.'+@consideringColumnName+'='+@value;
end
exec sp_executesql @sqltext,N'@resultCount int out',@resultCount out
if(@resultCount>0)
begin
SET @finalTableCount=@finalTableCount+1;
--insert into @errorintables values(@ftable,@fcolumn);
if(@finalTableCount>1)
begin
SET @TableNames = @TableNames + ',';
end
SET @TableNames = @TableNames + dbo.fn_SplitCamelCaseWord(@ftable);
--SET @ColumnNames = @ColumnNames + @fcolumn;
end
SET @loopcnt=@loopcnt+1;
end
--select * from @errorintables
select @TableNames
END
ALTER PROCEDURE [dbo].[sp_CheckReferences]
(
@tableName varchar(50) ,
@considerPrimaryKey bit,
@consideringColumnName varchar(50),
@value varchar(20)
)
AS
BEGIN
declare @TableNames varchar(max)='';
--declare @ColumnNames varchar(max)='';
declare @tblTmp Table
(
rowID int identity(1,1),
foreign_key_name varchar(100),
foreign_table varchar(100),
foreign_column varchar(100),
parent_table varchar(100),
parent_column varchar(100)
);
--declare @errorintables TABLE
--(
-- foreign_table varchar(50),
-- foreign_column varchar(50)
--);
INSERT INTO @tblTmp
select * from
(
select cast(f.name as varchar(255)) as foreign_key_name
, cast(c.name as varchar(255)) as foreign_table
, cast(fc.name as varchar(255)) as foreign_column
, cast(p.name as varchar(255)) as parent_table
, cast(rc.name as varchar(255)) as parent_column
from sysobjects f
inner join sysobjects c on f.parent_obj = c.id
inner join sysreferences r on f.id = r.constid
inner join sysobjects p on r.rkeyid = p.id
inner join syscolumns rc on r.rkeyid = rc.id and r.rkey1 = rc.colid
inner join syscolumns fc on r.fkeyid = fc.id and r.fkey1 = fc.colid
where f.type = 'F'
AND cast(p.name as varchar(255))=@tableName
) ss
declare @cnt int;
declare @loopcnt int=1;
declare @resultCount int;
declare @finalTableCount int = 0;
select @cnt = COUNT(*) from @tblTmp
WHILE(@loopcnt<=@cnt)
BEGIN
declare @sqltext nvarchar(max);
declare @ftable varchar(100);
declare @fcolumn varchar(100);
declare @pcolumn varchar(100);
select @ftable=foreign_table, @fcolumn=foreign_column,@pcolumn=parent_column from @tblTmp where rowID=@loopcnt;
set @sqltext = 'SELECT @resultCount=COUNT(*) FROM '+@tableName+' INNER JOIN '+@ftable+' ON '+@pcolumn+'='+@fcolumn;
if(@considerPrimaryKey = 1)
begin
SET @sqltext = @sqltext +' WHERE '+@tableName+'.'+@pcolumn+'='+@value;
end
else
begin
SET @sqltext = @sqltext +' WHERE '+@tableName+'.'+@consideringColumnName+'='+@value;
end
exec sp_executesql @sqltext,N'@resultCount int out',@resultCount out
if(@resultCount>0)
begin
SET @finalTableCount=@finalTableCount+1;
--insert into @errorintables values(@ftable,@fcolumn);
if(@finalTableCount>1)
begin
SET @TableNames = @TableNames + ',';
end
SET @TableNames = @TableNames + dbo.fn_SplitCamelCaseWord(@ftable);
--SET @ColumnNames = @ColumnNames + @fcolumn;
end
SET @loopcnt=@loopcnt+1;
end
--select * from @errorintables
select @TableNames
END
No comments:
Post a Comment