Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sr_aneesh
Starting Member
17 Posts |
Posted - 2005-01-22 : 08:28:10
|
| i want to make a standard stored procedure to check the duplication of records in anytable.my code is as follows wich gives the error : Err 137 ; Must declare the variable @tblnameCREATE PROCEDURE sp_checkdup@tblname varchar(25),@fldname varchar(25),@fldval varchar(25),@spname varchar(25)ASif exists (select @fldname from @tblname where @fldname=@fldval)return 555elseexecute @spnameGOwhat could be the problem ..... thanx in advance |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2005-01-22 : 08:32:42
|
| In order for it to work with ANY table, it would have to be:a. extremely complicated and impractical, orb. written in dynamic sql, orc. bothA BETTER thing to do is: design your tables to PREVENT duplicates in the first place. Primary keys, unique constraints, even triggers can help you accomplish this, and it's part of a proper database design anyway. Then you don't even need a duplicate finder. |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-01-22 : 09:44:08
|
Everything rob says.If you have autonumbers,uniqueidentifiers and such as as pk,then You are now in the position of considering unique indexes on the "natural" keys of your tables.This will find dups, (and in some cases nondups, but that has low propbability)select * from tablenamewhere binary_checksum(*) in( select bcs from (select binary_checksum(*) as bcs from tablename) cs group by bcs having count(*) > 1) You could tweak the above to use columnlist iof *, and exclude uniqueidentifiers,timestamp,identity columns.rockmoose |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-01-24 : 14:22:06
|
| [code]I can't agree with robvolk more. However, I also can't resist these little challenges.The reason your original post generated errors, sr_aneesh, is that you can't say: Select * from @varYou have to say: (dynamic sql statement) exec('Select * from ' + @var)Here is an SP that will check for dupes in any table by whatever columns you want to pass in. As you can see the bulk of the work is validating the input parameters. The actual checking for dupes is a pretty simple dynamic statment.Here is a sample call for the compiled sp (below):declare @retCode intexec @retCode = MrsDupeCheck @tbName = '<anyTable>' ,@colList = '<comma seperated list of columns>' --NULL will use All columns in tableSElect @retCode--For those of you fortunate enough to not be poisoned by american television--disregard the name of this sp...If Object_ID('dbo.MrsDupeCheck') > 0 Drop Proc dbo.MrsDupeCheckGOcreate proc MrsDupeCheck @tbName varChar(50) ,@colList varchar(8000) = nullasdeclare @errMsg varchar(256) ,@col varchar(8000) ,@pos int ,@missingCols varChar(8000)declare @cols Table (col varChar(50))Create Table #dupes (n int)--======================================================--validate parameters--make sure the table provided is validif Object_ID(@tbName) is nullBegin set @errMsg = 'The table ''' + @tbName + ''' does not exist.' raiserror(@errMsg, 17, 1) return -1End--Make sure the column list provided is validif @colList is NOT NULLBegin --Parse the comma seperated List of columns and add them to @cols table --(this parsing process is a good candidate for a function) Select @pos = 1 ,@col = '' while @pos <= len(@ColList) Begin if subString(@ColList, @pos, 1) = ',' begin -- this is a seperator so add column and reset @col insert @cols values (@col) set @col = '' end else -- add current character to @col set @col = @col + subString(@ColList, @pos, 1) set @pos = @pos + 1 End --insert last or only column insert @Cols values (@col) --make sure all columns passed exist in @tbName Select @missingCols = coalesce(@missingCols + ', ' + c.col, c.col) From @cols c Left JOIN sysColumns sc ON sc.id = Object_ID(@tbName) AND sc.name = c.col where sc.name is NULL If len(@missingCols) > 0 Begin set @errMsg = 'The table ''' + @tbName + ''' does not contain the following column(s): (' + @missingCols + ')' raiserror(@errMsg, 17, 1) return -1 EndEnd--======================================================--if no column(s) provided, use all columns in table--This is a trick (well documented in these forums) for csv-ing records to stringif @colList is NULL Select @collist = coalesce(@collist + ',' + name, name) from sysColumns where id = Object_ID(@tbName)--======================================================--Just get the first dupe because we just need to know if any existset rowcount 1insert #dupes (n)exec('Select 1 from ' + @tbName + 'Group by ' + @collist + 'having count(*) > 1')--don't forget to reset the rowcountset rowcount 0if Exists(Select * from #dupes) Return 555return 0GO[/code] |
 |
|
|
|
|
|
|
|