Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Change 1 word in 100 store procs with another

Author  Topic 

overthetop
Starting Member

18 Posts

Posted - 2006-11-20 : 11:43:37
Hi :)

I know you won't let me down again :). Here is the problem: I have 100 sp with simular stucture

create store proc sp_name
(
....
)
as

declare @Error int

declare @ret int

INSERT tablename
(
....
)
values
(
......
)

set @Error = @@Error
set @ret = @@Identity

.....

return @ret


So I have to change for all 100 sp the @@Identity word with SCOPE_IDENTITY(). I know the table names but how can I change this?! Is there any way to do that? If only I could get the sp text and replace it...Please help :)


10x

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-11-20 : 11:46:15
Generate scripts for all the SPs and then do a careful Find & Replace over them.

Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-11-20 : 12:30:45
In my case (in case its food for thought):

We have all our Stored Procedure code in a Version Control repository (using SubVersion).

I can therefore checkout all the SQL Source Code into folders on my local drive, I can then use my programmers editor to do a find & replace across all files (or, more probably, a Find and Optional Replace! so that I can vet the changes)

And then all the changes I make will go back into the version control, and if in a couple of weeks I find something has broken because of the change I can review what was actually changed and make necessary improvements based on that knowledge

Kristen
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2006-11-20 : 12:40:24
if you don't have your code is source control, this little app I wrote may help you fix that:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=73884




SqlSpec - a fast, cheap, and comprehensive data dictionary generator for
SQL Server 2000/2005 and Analysis Server 2005 - http://www.elsasoft.org
Go to Top of Page

overthetop
Starting Member

18 Posts

Posted - 2006-11-20 : 16:02:06
10x again this forum rocks...by the way the tool looks nice I'll give it a test tomorrow :)
Go to Top of Page

overthetop
Starting Member

18 Posts

Posted - 2006-11-21 : 02:55:01
I have one more question. Can I generate sql script for sp from t-sql, because I have over 600 sp in the database and I wan't to do the change for sp with simular name like XXXInsert. I can get the names for this sp-s like this

select * from dbo.sysobjects where [name] like N'%Insert'

But can I generate scripts only for this sp, wihtout doing it manualy from Enterprise Mng?
Go to Top of Page

overthetop
Starting Member

18 Posts

Posted - 2006-11-21 : 04:15:30
I figured out one solution using sp_helptext for every sp and it will do. If you have other sugestions... :)
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-21 : 04:19:08
How about
UPDATE	syscomments
SET [text] = REPLACE([text], '@@identity', 'SCOPE_IDENTITY()')


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-21 : 04:21:25
Remember to backup first.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-21 : 04:37:22
This is the code for sp_helptext. Maybe it is useful to you and maybe you can change the code to include all SP with names and run this for every SP?
create procedure sp_helptext
@objname nvarchar(776)
,@columnname sysname = NULL
as

set nocount on

declare @dbname sysname
,@BlankSpaceAdded int
,@BasePos int
,@CurrentPos int
,@TextLength int
,@LineId int
,@AddOnLen int
,@LFCR int --lengths of line feed carriage return
,@DefinedLength int

/* NOTE: Length of @SyscomText is 4000 to replace the length of
** text column in syscomments.
** lengths on @Line, #CommentText Text column and
** value for @DefinedLength are all 255. These need to all have
** the same values. 255 was selected in order for the max length
** display using down level clients
*/
,@SyscomText nvarchar(4000)
,@Line nvarchar(255)

Select @DefinedLength = 255
SELECT @BlankSpaceAdded = 0 /*Keeps track of blank spaces at end of lines. Note Len function ignores
trailing blank spaces*/
CREATE TABLE #CommentText
(LineId int
,Text nvarchar(255) collate database_default)

/*
** Make sure the @objname is local to the current database.
*/
select @dbname = parsename(@objname,3)

if @dbname is not null and @dbname <> db_name()
begin
raiserror(15250,-1,-1)
return (1)
end

/*
** See if @objname exists.
*/
if (object_id(@objname) is null)
begin
select @dbname = db_name()
raiserror(15009,-1,-1,@objname,@dbname)
return (1)
end

-- If second parameter was given.
if ( @columnname is not null)
begin
-- Check if it is a table
if (select count(*) from sysobjects where id = object_id(@objname) and xtype in ('S ','U ','TF'))=0
begin
raiserror(15218,-1,-1,@objname)
return(1)
end
-- check if it is a correct column name
if ((select 'count'=count(*) from syscolumns where name = @columnname and id = object_id(@objname) and number = 0) =0)
begin
raiserror(15645,-1,-1,@columnname)
return(1)
end
if ((select iscomputed from syscolumns where name = @columnname and id = object_id(@objname) and number = 0) = 0)
begin
raiserror(15646,-1,-1,@columnname)
return(1)
end

DECLARE ms_crs_syscom CURSOR LOCAL
FOR SELECT text FROM syscomments WHERE id = object_id(@objname) and encrypted = 0 and number =
(select colid from syscolumns where name = @columnname and id = object_id(@objname) and number = 0)
order by number,colid
FOR READ ONLY

end
else
begin
/*
** Find out how many lines of text are coming back,
** and return if there are none.
*/
if (select count(*) from syscomments c, sysobjects o where o.xtype not in ('S', 'U')
and o.id = c.id and o.id = object_id(@objname)) = 0
begin
raiserror(15197,-1,-1,@objname)
return (1)
end

if (select count(*) from syscomments where id = object_id(@objname)
and encrypted = 0) = 0
begin
raiserror(15471,-1,-1)
return (0)
end

DECLARE ms_crs_syscom CURSOR LOCAL
FOR SELECT text FROM syscomments WHERE id = OBJECT_ID(@objname) and encrypted = 0
ORDER BY number, colid
FOR READ ONLY
end

/*
** Else get the text.
*/
SELECT @LFCR = 2
SELECT @LineId = 1


OPEN ms_crs_syscom

FETCH NEXT FROM ms_crs_syscom into @SyscomText

WHILE @@fetch_status >= 0
BEGIN

SELECT @BasePos = 1
SELECT @CurrentPos = 1
SELECT @TextLength = LEN(@SyscomText)

WHILE @CurrentPos != 0
BEGIN
--Looking for end of line followed by carriage return
SELECT @CurrentPos = CHARINDEX(char(13)+char(10), @SyscomText, @BasePos)

--If carriage return found
IF @CurrentPos != 0
BEGIN
/*If new value for @Lines length will be > then the
**set length then insert current contents of @line
**and proceed.
*/
While (isnull(LEN(@Line),0) + @BlankSpaceAdded + @CurrentPos-@BasePos + @LFCR) > @DefinedLength
BEGIN
SELECT @AddOnLen = @DefinedLength-(isnull(LEN(@Line),0) + @BlankSpaceAdded)
INSERT #CommentText VALUES
( @LineId,
isnull(@Line, N'') + isnull(SUBSTRING(@SyscomText, @BasePos, @AddOnLen), N''))
SELECT @Line = NULL, @LineId = @LineId + 1,
@BasePos = @BasePos + @AddOnLen, @BlankSpaceAdded = 0
END
SELECT @Line = isnull(@Line, N'') + isnull(SUBSTRING(@SyscomText, @BasePos, @CurrentPos-@BasePos + @LFCR), N'')
SELECT @BasePos = @CurrentPos+2
INSERT #CommentText VALUES( @LineId, @Line )
SELECT @LineId = @LineId + 1
SELECT @Line = NULL
END
ELSE
--else carriage return not found
BEGIN
IF @BasePos <= @TextLength
BEGIN
/*If new value for @Lines length will be > then the
**defined length
*/
While (isnull(LEN(@Line),0) + @BlankSpaceAdded + @TextLength-@BasePos+1 ) > @DefinedLength
BEGIN
SELECT @AddOnLen = @DefinedLength - (isnull(LEN(@Line),0) + @BlankSpaceAdded)
INSERT #CommentText VALUES
( @LineId,
isnull(@Line, N'') + isnull(SUBSTRING(@SyscomText, @BasePos, @AddOnLen), N''))
SELECT @Line = NULL, @LineId = @LineId + 1,
@BasePos = @BasePos + @AddOnLen, @BlankSpaceAdded = 0
END
SELECT @Line = isnull(@Line, N'') + isnull(SUBSTRING(@SyscomText, @BasePos, @TextLength-@BasePos+1 ), N'')
if LEN(@Line) < @DefinedLength and charindex(' ', @SyscomText, @TextLength+1 ) > 0
BEGIN
SELECT @Line = @Line + ' ', @BlankSpaceAdded = 1
END
END
END
END

FETCH NEXT FROM ms_crs_syscom into @SyscomText
END

IF @Line is NOT NULL
INSERT #CommentText VALUES( @LineId, @Line )

select Text from #CommentText order by LineId

CLOSE ms_crs_syscom
DEALLOCATE ms_crs_syscom

DROP TABLE #CommentText

return (0) -- sp_helptext


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-11-21 : 12:36:41
"I figured out one solution using sp_helptext for every sp and it will do. If you have other suggestions... :)"

sp_HelpText is inclined to insert line-breaks in long lines - something to watch out for!

"UPDATE syscomments
SET [text] = REPLACE([text], '@@identity', 'SCOPE_IDENTITY()')
"

Watch out if an Sproc might be encrypted (but then it shouldn't match the parameter, of course, but won't get "fixed" either!)

Does this cause the new source to be used immediately, or is a cache flush required?

syscomments is stored in "chunks" of text, so '@@identity' might be split over a boundary, and the replacement 'SCOPE_IDENTITY()' might not fit within the current "chunk" ?

Kristen
Go to Top of Page

overthetop
Starting Member

18 Posts

Posted - 2006-11-21 : 12:41:32
yeah :) exec sp_helptext 'sp_helptext' That's nice I'll have it in mind...
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-11-21 : 13:20:32
"exec sp_helptext 'sp_helptext'"

and posted by "OverTheTop" ... Priceless!!

Made my day!

Kristen
Go to Top of Page
   

- Advertisement -