Solved

Modify SQL Script to Delete Record

Posted on 2013-01-30
6
366 Views
Last Modified: 2013-01-31
Hello Experts,

I currently have this SQL script that goes through all my tables in the database and deletes the record when it finds the item I specify. It is working well for me so far.
It searches for the field name "ITEMNMBR". But I just found out that the item is also located as a different field name "ITEMNO".
I would like to modify the script to be able to look at both fields for the item.

Thanks
*****************************************************************

CREATE TABLE zzResults (
      TableName VARCHAR(100)                                            
)      

EXEC sp_msforeachtable '
IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = PARSENAME(''?'', 1) AND COLUMN_NAME = ''ITEMNMBR'')
            exec (''IF EXISTS(SELECT 1 FROM ? WHERE ITEMNMBR = ''''01AAABBBCCC-XX'''')
                        INSERT zzResults(TableName) VALUES (''''?'''') '')
           
'
declare @tabname nvarchar(100)
while exists (select top 1 1 from zzResults)
begin
select @tabname = min(TableName) from zzResults
exec ('delete from ' + @tabname + ' where ITEMNMBR = ''01AAABBBCCC-XX''')
delete from zzResults where TableName = @tabname
end
SELECT TableName
FROM zzResults

DROP TABLE zzResults
0
Comment
Question by:jann3891
  • 3
  • 3
6 Comments
 
LVL 39

Expert Comment

by:appari
ID: 38837723
just run the same code with changed columnname one more time:
CREATE TABLE zzResults (
      TableName VARCHAR(100)                                            
)      

EXEC sp_msforeachtable '
IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = PARSENAME(''?'', 1) AND COLUMN_NAME = ''ITEMNMBR'')
            exec (''IF EXISTS(SELECT 1 FROM ? WHERE ITEMNMBR = ''''01AAABBBCCC-XX'''')
                        INSERT zzResults(TableName) VALUES (''''?'''') '') 
            
'
declare @tabname nvarchar(100)
while exists (select top 1 1 from zzResults)
begin
select @tabname = min(TableName) from zzResults
exec ('delete from ' + @tabname + ' where ITEMNMBR = ''01AAABBBCCC-XX''')
delete from zzResults where TableName = @tabname
end
SELECT TableName
FROM zzResults


EXEC sp_msforeachtable '
IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = PARSENAME(''?'', 1) AND COLUMN_NAME = ''ITEMNO'')
            exec (''IF EXISTS(SELECT 1 FROM ? WHERE ITEMNO = ''''01AAABBBCCC-XX'''')
                        INSERT zzResults(TableName) VALUES (''''?'''') '') 
            
'
declare @tabname nvarchar(100)
while exists (select top 1 1 from zzResults)
begin
select @tabname = min(TableName) from zzResults
exec ('delete from ' + @tabname + ' where ITEMNO = ''01AAABBBCCC-XX''')
delete from zzResults where TableName = @tabname
end

SELECT TableName
FROM zzResults

DROP TABLE zzResults 

Open in new window

0
 

Author Comment

by:jann3891
ID: 38837753
Yes, that's what I have been doing, just hoping there is a way to combine this that way I won't have to change the item 4 times.
If you don't mind, I will wait for other experts to put in their comment.

Thanks
0
 
LVL 39

Accepted Solution

by:
appari earned 500 total points
ID: 38837796
then try like this
CREATE TABLE zzResults (
      TableName VARCHAR(100) ,
      ColumnName varchar(100)
)      

EXEC sp_msforeachtable ' INSERT into zzResults(TableName,ColumnName)  SELECT TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = PARSENAME(''?'', 1) AND COLUMN_NAME in ( ''ITEMNMBR'', ''ITEMNO'') '
SELECT * 
FROM zzResults

declare @tabname nvarchar(100)
declare @colname nvarchar(100)

while exists (select top 1 1 from zzResults)
begin
   Select @tabname = TableName, @colname = ColumnName from ( Select TableName, ColumnName, row_number() over(order by TableName, ColumnName) RowID from 
            zzResults) A where RowID = 1

	exec ('delete from ' + @tabname + ' where ' + @colname + ' = ''01AAABBBCCC-XX''')

	Delete from zzResults where  @tabname = TableName and @colname = ColumnName

end

SELECT * 
FROM zzResults

drop table zzResults

Open in new window

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 39

Expert Comment

by:appari
ID: 38837810
you can replace, this works even if your sql version is SQL 2000.

   Select @tabname = TableName, @colname = ColumnName from ( Select TableName, ColumnName, row_number() over(order by TableName, ColumnName) RowID from
            zzResults) A where RowID = 1

with

   Select @tabname = TableName, @colname = ColumnName from ( Select TOP 1 TableName, ColumnName from
            zzResults) A
0
 

Author Comment

by:jann3891
ID: 38840015
getting this error:
Server: Msg 195, Level 15, State 10, Line 16
'row_number' is not a recognized function name.
0
 

Author Comment

by:jann3891
ID: 38840848
Got it. Had to make a few changes, but got it to work.
Thank you very much for your help
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now