Solved

Combining 2 SQL Scripts into 1

Posted on 2013-01-31
7
277 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
  • 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

777 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