Solved

Combining 2 SQL Scripts into 1

Posted on 2013-01-31
7
281 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
rolling count by date, hour query 7 32
What does "Between" mean? 6 48
Report 8 27
SQL multiple joins 2 22
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.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

710 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