hi friends,
my sp is used to eliminate duplicate data from the table
dynamic query inside the sp is
select @.sql = 'SELECT * into ' + @.i_errorDb + '.dbo.' + @.i_tableName + '
from '
+ @.i_oldDb + '..'+ @.i_tableName + ' as T where ' + @.i_PrimaryKey + '=
(select min('+@.i_PrimaryKey + ') from ' +
@.i_oldDb + '..'+ @.i_tableName + ' where ' + @.i_PrimaryKey + ' = T.' +
@.i_PrimaryKey + ' having count(*) > 1) order by ' + @.i_PrimaryKey
exec sp_executesql @.sql
it works fine if i have a single pk in the table. but in case of composite
pk, it fails.
pls help me to solve this
thanks
vanithait gives me the solution.
is there any alternative method?
select @.sql = 'delete FROM ' + @.i_oldDb + '..'+ @.i_tableName + ' WHERE ' +
@.i_PrimaryKey1 + '=(SELECT MIN('+
@.i_PrimaryKey1 + ') FROM ' + @.i_oldDb + '..'+ @.i_tableName + ' as T WHERE '
+ @.i_oldDb + '..'+ @.i_tableName + '.' + @.i_PrimaryKey1 + '= T.' +
@.i_PrimaryKey1
if @.i_PrimaryKey2 is not null
begin
select @.sql = @.sql + ' and ' + @.i_oldDb + '..'+ @.i_tableName + '.' +
@.i_PrimaryKey2 + ' = T.' + @.i_PrimaryKey2
end
if @.i_PrimaryKey3 is not null
begin
select @.sql = @.sql + ' and ' + @.i_oldDb + '..'+ @.i_tableName + '.' +
@.i_PrimaryKey3 + ' = T.' + @.i_PrimaryKey3
end
select @.sql = @.sql + ' having count(*) > 1)'
exec sp_executesql @.sql
"vanitha" wrote:
> hi friends,
> my sp is used to eliminate duplicate data from the table
> dynamic query inside the sp is
> select @.sql = 'SELECT * into ' + @.i_errorDb + '.dbo.' + @.i_tableName + '
> from '
> + @.i_oldDb + '..'+ @.i_tableName + ' as T where ' + @.i_PrimaryKey + '=
> (select min('+@.i_PrimaryKey + ') from ' +
> @.i_oldDb + '..'+ @.i_tableName + ' where ' + @.i_PrimaryKey + ' = T.' +
> @.i_PrimaryKey + ' having count(*) > 1) order by ' + @.i_PrimaryKey
> exec sp_executesql @.sql
> it works fine if i have a single pk in the table. but in case of composite
> pk, it fails.
> pls help me to solve this
> thanks
> vanitha
No comments:
Post a Comment