Solved

Combining 2 SQL Scripts into 1

Posted on 2013-01-31
7
282 Views
Last Modified: 2013-02-07
Hello Experts,

I have 2 SQL scripts, one modifies an item from one to another, and the other one deletes the record if the item exists. Both scripts have been working well for me so far.
But I had to run both scripts each time, I am wondering if there is a way to combine the 2 scripts into one.

You might ask why I need to run the delete script if the modify script already changed the item. The reason to this is when I run the modifier script, changing the item from AAA to BBB, both items already exist in the database. I am merely moving all the transactions of  item AAA to merge with BBB, and on some tables, the ITEMNMBR is a primary field, so it won't modify, which is why I had to run the delete script to get rid of the records with AAA items.
Hope this makes sense. Anyway, just wondering if this is possible.

Thanks

*********************** Modifier Script ******************************
DECLARE @TableName nvarchar(100),@Query nvarchar(100),@ColumnName nvarchar(100),@i int
CREATE TABLE zzTableNames (i int identity, TableName nvarchar(100), columnName nvarchar(100) )
INSERT INTO zzTableNames
SELECT TABLE_NAME, column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME IN ( 'ITEMNMBR', 'ITEMNO')
SELECT @i =@@ROWCOUNT
SET @TableName = ''
WHILE @i > 0
BEGIN
SELECT @TableName = TableName , @ColumnName = columnName
FROM zzTableNames
WHERE i = @i
SET @i =@i-1
IF @TableName IS NOT NULL
BEGIN
SET @Query = 'UPDATE ' + @TableName + ' SET '+@ColumnName+' = ''02BBBBBBBBB-YYY'' WHERE ' +@ColumnName+' = ''01AAAAAAAAA-XXX'''
EXEC(@Query)  
END
END

DROP TABLE zzTableNames

*****************************************************************

******************** Delete Script ***********************************
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 TOP 1 TableName, ColumnName from
            zzResults) A

      exec ('delete from ' + @tabname + ' where ' + @colname + ' = ''01AAAAAAAAA-XXX''')

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

end

SELECT *
FROM zzResults

drop table zzResults
************************************************************************
0
Comment
Question by:jann3891
[X]
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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 38841277
They can just be jammed together to make one script without any changes.  Or are you looking to consolidate the logic into less statements?
0
 

Author Comment

by:jann3891
ID: 38841305
Yes, I am looking for a consolidated one where I will only change the 2 fields, the 01AAAAAAAAA-XXX and the 02BBBBBBBBB-YYY

Thanks
0
 
LVL 39

Accepted Solution

by:
appari earned 500 total points
ID: 38842015
>>and on some tables, the ITEMNMBR is a primary field, so it won't modify, which is why I had to run the delete script to get rid of the records with AAA items.
in that case do you want to insert a new record with BBB item or only delete is enough?

if only delete is enough you can try something 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 TOP 1 TableName, ColumnName from 
            zzResults) A

    SET @Query = 'UPDATE ' + @tabname  + ' SET '+ @colname  +' = ''02BBBBBBBBB-YYY'' WHERE ' + @colname  + ' = ''01AAAAAAAAA-XXX'''
    EXEC(@Query) 

      exec ('delete from ' + @tabname + ' where ' + @colname + ' = ''01AAAAAAAAA-XXX''')

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

end

SELECT * 
FROM zzResults

drop table zzResults

Open in new window

0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 38844360
>> on some tables, the ITEMNMBR is a primary field, so it won't modify <<

That (IMO dopey) restriction used to exist in SQL Server, but it doesn't now.

You can UPDATE a primary key column just like any other column, as long as it doesn't create a duplicate PK value.
0
 

Author Comment

by:jann3891
ID: 38844383
appari, I need it to do both modify first, and then delete.

ScottPletcher, it does create a duplicate PK value..
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 38844736
If key value ''02BBBBBBBBB-YYY'' is already there, and it's a PK, then you can't change other rows to that value since it already exists.

Not sure what you're trying to do then.
0
 
LVL 39

Expert Comment

by:appari
ID: 38850078
did you try the code i posted?
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

626 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