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
 SQL Server Development (2000)
 xp_sprintf 255 char limitation

Author  Topic 

datagod
Starting Member

37 Posts

Posted - 2006-05-04 : 10:13:08
Does anyone have a replacement script/function for xp_sprintf?

xp_sprintf is great for replacing tokens in a string with parameter values, but there is a 255 char limit, which totally blows for what I need (formatting Body text of an email based on multiple parameters).

Thaks.

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-05-04 : 10:51:06
will this do?

create proc xpSprintF
@body varchar(8000) = '',
@params varchar(1000) = '',
@paramSeparator varchar(10) = ','
as
begin
set @params = @params + @paramSeparator -- so we don't have to specially handle the last item
declare @param varchar(200),
@leftBodyOld varchar(8000),
@leftBodyNew varchar(8000),
@i int
set @i = 0
while @params <> '' and @i < 8000 -- max 8000 iterations
begin
select @param = replace(left(@params, charindex('"' + @paramSeparator, @params)), '"', ''),
@leftBodyOld = left(@body, charindex('%s', @body)+1),
@leftBodyNew = replace(@leftBodyOld, '%s', @param),
@body = replace(@body, @leftBodyOld, @leftBodyNew),
@params = replace(@params, '"' + @param + '"' + @paramSeparator, '')
set @i = @i + 1 -- max iterations... so we don't get an infinite loop by accident
end
select @body
end
go
declare @body varchar(8000), @params varchar(1000)
select @body = 'INSERT INTO %s VALUES (%s, %s)',
@params = '"table1","''1''","2"' -- no spaces between parameters!!!
--Parameters must be in format '"parameterValue","parameterValue2","parameterValue3"'

exec xpSprintF @body, @params
go
drop proc xpSprintF


Go with the flow & have fun! Else fight the flow
Blog thingie: [URL="http://weblogs.sqlteam.com/mladenp"]
Go to Top of Page

datagod
Starting Member

37 Posts

Posted - 2006-05-04 : 10:59:00
Fantastic! Thank you very much!!
Go to Top of Page

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2006-05-04 : 11:19:51
Hi all,

I see spirit1 beat me too it , but since I've written it now, here's an alternative.

It's a function but is similar in style to spirit's.


select dbo.fnSprintf('INSERT INTO %s VALUES (%s, %s)', 'table1,1,2', default)

create function dbo.fnSprintf (@s varchar(8000), @params varchar(8000), @separator char(1) = ',')
returns varchar(8000)
as
begin
declare @p varchar(8000)

set @params = @params + @separator
while not @params = ''
begin
set @p = left(@params+@separator, charindex(@separator, @params)-1)
set @s = STUFF(@s, charindex('%s', @s), 2, @p)
set @params = substring(@params, len(@p)+2, 8000)
end
return @s
end


Ryan Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-05-04 : 11:29:31
nice Ryan.

i always forget the stuff function exists

Go with the flow & have fun! Else fight the flow
Blog thingie: [URL="http://weblogs.sqlteam.com/mladenp"]
Go to Top of Page

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2006-05-04 : 11:39:24
I guess it has a bit of a forgettable name . I just need to learn to use it a bit more quickly...

Ryan Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page
   

- Advertisement -