Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Modify SQL Script to Delete Record

Posted on 2013-01-30
6
369 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

840 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