Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Combining 2 SQL Scripts into 1

Posted on 2013-01-31
7
Medium Priority
?
285 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 1500 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 70

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 70

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

886 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