Solved

Combining 2 SQL Scripts into 1

Posted on 2013-01-31
7
266 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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
>> 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
Comment Utility
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:ScottPletcher
Comment Utility
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
Comment Utility
did you try the code i posted?
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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 setup several different housekeeping processes for a SQL Server.

771 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

12 Experts available now in Live!

Get 1:1 Help Now