Unix Technical Forum

Replacing columnn name programmatically

This is a discussion on Replacing columnn name programmatically within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, The following script does not return any resultset against a test db while I know for a fact ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 07:28 PM
Doug Baroter
 
Posts: n/a
Default Replacing columnn name programmatically

Hi,

The following script does not return any resultset against a test db
while I know for a fact tables with letter "aaa" has columns that
contains "ccc".
What's wrong? the the inner cursor?

Thanks.


-- get all tbls with letter aaa
declare @tbl varchar(8000)
declare tblCursor cursor for
SELECT name
FROM sysobjects
WHERE xtype = 'U'
AND name LIKE '%aaa%'

open tblCursor
fetch next from tblCursor
into @tbl

while (@@fetch_status = 0)
begin

-- get all columns with letter ccc and replace it with nothing /
remove it
declare @tbuffer varchar(4000)
declare @cbuffer varchar(8000)

declare abnormal_cols cursor for
SELECT o.name, c.name
FROM sysobjects o
JOIN syscolumns c ON o.id = c.id
WHERE o.xtype = 'U'
AND c.name LIKE '%ccc%'
and o.id = object_id('+@tbl')
-- ORDER BY c.name

open abnormal_cols
fetch next from abnormal_cols
into @tbuffer,@cbuffer

while (@@fetch_status = 0)
begin
-- EXEC sp_rename '+@tbuffer+'.['+@cbuffer+']','+Replace(+@cbuffer+','%ccc%','')',
'COLUMN';
-- test
print @tbuffer + ', ' + @cbuffer;
fetch next from abnormal_cols
into @tbuffer,@cbuffer
end

close abnormal_cols
deallocate abnormal_cols;

fetch next from tblCursor
into @tbl

end
close tblCursor
deallocate tblCursor;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 07:32 PM
John Bell
 
Posts: n/a
Default Re: Replacing columnn name programmatically

Hi

I can't see why there are two cursors here, try:

SELECT o.name, Replace(c.name,'ccc','') as NewName, c.name as OldName
FROM sysobjects o JOIN syscolumns c ON o.id = c.id
JOIN syscolumns a ON o.id = a.id
WHERE o.xtype = 'U'
AND c.name LIKE '%ccc%'
AND a.name LIKE '%aaa%'

John

"Doug Baroter" <qwert12345@boxfrog.com> wrote in message
news:fc254714.0310211451.2f59f9c4@posting.google.c om...
> Hi,
>
> The following script does not return any resultset against a test db
> while I know for a fact tables with letter "aaa" has columns that
> contains "ccc".
> What's wrong? the the inner cursor?
>
> Thanks.
>
>
> -- get all tbls with letter aaa
> declare @tbl varchar(8000)
> declare tblCursor cursor for
> SELECT name
> FROM sysobjects
> WHERE xtype = 'U'
> AND name LIKE '%aaa%'
>
> open tblCursor
> fetch next from tblCursor
> into @tbl
>
> while (@@fetch_status = 0)
> begin
>
> -- get all columns with letter ccc and replace it with nothing /
> remove it
> declare @tbuffer varchar(4000)
> declare @cbuffer varchar(8000)
>
> declare abnormal_cols cursor for
> SELECT o.name, c.name
> FROM sysobjects o
> JOIN syscolumns c ON o.id = c.id
> WHERE o.xtype = 'U'
> AND c.name LIKE '%ccc%'
> and o.id = object_id('+@tbl')
> -- ORDER BY c.name
>
> open abnormal_cols
> fetch next from abnormal_cols
> into @tbuffer,@cbuffer
>
> while (@@fetch_status = 0)
> begin
> -- EXEC sp_rename

'+@tbuffer+'.['+@cbuffer+']','+Replace(+@cbuffer+','%ccc%','')',
> 'COLUMN';
> -- test
> print @tbuffer + ', ' + @cbuffer;
> fetch next from abnormal_cols
> into @tbuffer,@cbuffer
> end
>
> close abnormal_cols
> deallocate abnormal_cols;
>
> fetch next from tblCursor
> into @tbl
>
> end
> close tblCursor
> deallocate tblCursor;



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:12 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com