April 29, 2008 at 3:26 am
Hi,
I have a table with around 60 columns. 20 of them have a name that begin with "aa_". I want to set the same value to all of them. Is there any quick and easy way to do that?
Thanks!
April 29, 2008 at 5:24 am
Hi,
You could use Dynamic SQL to do it. Something like the following
DECLARE @tableName VARCHAR(30)
DECLARE @newValue VARCHAR(30)
DECLARE @sql VARCHAR(2000)
/* Set Values */
SET @tableName = 'myTable'
SET @newValue = 'changeToThis'
/* Build Update statement */
SELECT @sql = 'UPDATE ' + @tableName + ' SET '
SELECT @sql = @sql + ' sc.name + ' = ' + @newValue + ', '
FROM sysobjects so
INNER JOIN syscolumns sc
ON so.id = sc.id
WHERE so.name = @tableName
AND sc.name LIKE 'aa[_]%'
/* Trim final comma */
SELECT @sql = LEFT(@sql, LEN(@sql) -1)
/* Review SQL Statement first */
PRINT @sql
/* Uncomment to execute */
-- EXEC (@sql)
HTH,
April 29, 2008 at 6:48 am
Hi and thanks for your answer! It helps a lot.
However.. : ) I understand this gives me all the column names I need :
SELECT sc.name
FROM sysobjects so
INNER JOIN syscolumns sc
ON so.id = sc.id
WHERE so.name = 'MyTable'
AND sc.name LIKE 'aa_%'
But I can't manage to get them into a big update statement.. For now my best idea would consist in puting the result in a temp table and go through each record with a cursor... Can anyone help with that?
Thanks!
April 29, 2008 at 7:07 am
Adrian's code does create and execute "one big update" statement as you've asked... have you tried it on a test table?
--Jeff Moden
Change is inevitable... Change for the better is not.
April 29, 2008 at 7:27 am
Hi,
actually the code does not work since there is a small quote problem. Look here :
SELECT @sql = @sql + ' sc.name + ' = ' + @newValue + ', '
And I can't figure it out since I can't get an accurate idea of how the final query should exactly look like. I keep looking..
April 29, 2008 at 8:00 am
Allright, got it!
Here's the correct code :
DECLARE @tableName VARCHAR(30)
DECLARE @newValue VARCHAR(30)
DECLARE @sql VARCHAR(2000)
/* Set Values */
SET @tableName = 'myTable'
SET @newValue = 'changeToThis'
/* Build Update statement */
SELECT @sql = 'UPDATE ' + @tableName + ' SET '
SELECT @sql = @sql + sc.name + ' = ' + @newValue + ', '
FROM sysobjects so
INNER JOIN syscolumns sc
ON so.id = sc.id
WHERE so.name = @tableName
AND sc.name LIKE 'aa[_]%'
/* Trim final comma */
SELECT @sql = LEFT(@sql, LEN(@sql) -1)
/* Review SQL Statement first */
PRINT @sql
So there was just one quote too many just before sc.name
Thanks again to you Adrian for the answer and to you Jeff for keeping me on the right track.
Cheers
April 29, 2008 at 6:35 pm
Perfect. Thanks for the feedback.
Adrian... nice job!
--Jeff Moden
Change is inevitable... Change for the better is not.
April 30, 2008 at 2:02 am
April 30, 2008 at 6:35 pm
Actually, Adrian's script is not quite perfect.
🙂
If any table or column name is happened to have spece or other "unnormal" character it will fail.
When you build any kind of generic scxripts you need to take care of it.
Also, if @newValue is not any kind of number Adrian's UPDATE will fail because it will interprete @newValue as a column identifier.
This must be better:
/* Build Update statement */
SELECT @sql = 'UPDATE ' + QUOTENAME(@tableName) + ' SET '
SELECT @sql = @sql + QUOTENAME(sc.name) + ' = ' + QUOTENAME(@newValue, '''') + ', '
FROM sysobjects so
INNER JOIN syscolumns sc
ON so.id = sc.id
WHERE so.name = @tableName
AND sc.name LIKE 'aa[_]%'
And you can avoid trimming last comma and do everything in one statement:
/* Build Update statement */
SELECT @sql = ISNULL(@sql + ',', 'UPDATE ' + QUOTENAME(@tableName) + ' SET ')
+ QUOTENAME(sc.name) + ' = ' + QUOTENAME(@newValue, '''')
FROM sysobjects so
INNER JOIN syscolumns sc
ON so.id = sc.id
WHERE so.name = @tableName
AND sc.name LIKE 'aa[_]%'
_____________
Code for TallyGenerator
April 30, 2008 at 6:38 pm
Heh... yeah... You're right and I agree... except I don't allow garbage names into the server. :hehe:
--Jeff Moden
Change is inevitable... Change for the better is not.
April 30, 2008 at 6:46 pm
In "report specific" tables typically it's not up to you or me to define names.
😉
_____________
Code for TallyGenerator
April 30, 2008 at 6:47 pm
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply