?
Solved

Modify SQL Script for modifying record

Posted on 2013-01-30
4
Medium Priority
?
374 Views
Last Modified: 2013-01-30
Hello Experts,

I currently have this SQL script that goes through all my tables in the database and modifies the item I specify from one item to another. It is working well for me so far.
It searches for the field name "ITEMNMBR". But I just found out that the item I need modified is also located as a different field name "ITEMNO".
I would like to modify the script to be able to look at both fields for the item I want to modify.

Thanks
*****************************************************************

USE BTF
CREATE TABLE zzTableNames (TableName nvarchar(100))
INSERT INTO zzTableNames
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'ITEMNMBR'
DECLARE @TableName nvarchar(100),@Query nvarchar(100)
SET @TableName = ''
WHILE @TableName IS NOT NULL
BEGIN
SELECT @TableName = (SELECT MIN(TableName) FROM zzTableNames WHERE TableName > @TableName)
IF @TableName IS NOT NULL
BEGIN
SET @Query = 'UPDATE ' + @TableName + ' SET ITEMNMBR = ''01AAABBBCCC-XX'' WHERE ITEMNMBR = ''02DDDEEEFF-YY'''
EXEC(@Query)  
END
END

DROP TABLE zzTableNames
0
Comment
Question by:jann3891
  • 2
  • 2
4 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 38837442
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 L
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 ITEMNMBR = ''01AAABBBCCC-XX'' WHERE ' +@ColumnName+' = ''02DDDEEEFF-YY'''
EXEC(@Query)  
END
END

DROP TABLE zzTableNames
0
 

Author Comment

by:jann3891
ID: 38837521
Thank you for the quick response.
i have a couple of questions.

WHILE @i > 0 L
is that "L" suppose to be there?  I am getting an error.
  Server: Msg 170, Level 15, State 1, Line 7
  Line 7: Incorrect syntax near 'L'.

so, I removed the L, query batch completed with errors.
  Server: Msg 207, Level 16, State 1, Line 1
  Invalid column name 'ITEMNMBR'.
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 2000 total points
ID: 38837555
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+' = ''01AAABBBCCC-XX'' WHERE ' +@ColumnName+' = ''02DDDEEEFF-YY'''
EXEC(@Query)  
END
END

DROP TABLE zzTableNames
0
 

Author Closing Comment

by:jann3891
ID: 38837593
works great. Thank you.
please watch for my second post, it is almost the same thing, but the script is for a delete record.

Thanks
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Suggested Courses

840 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