SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 xp_sprintf 255 char limitation
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

datagod
Starting Member

37 Posts

Posted - 05/04/2006 :  10:13:08  Show Profile  Reply with Quote
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

Slovenia
11751 Posts

Posted - 05/04/2006 :  10:51:06  Show Profile  Visit spirit1's Homepage  Reply with Quote
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: http://weblogs.sqlteam.com/mladenp

Edited by - spirit1 on 05/04/2006 10:56:02
Go to Top of Page

datagod
Starting Member

37 Posts

Posted - 05/04/2006 :  10:59:00  Show Profile  Reply with Quote
Fantastic! Thank you very much!!
Go to Top of Page

RyanRandall
Flowing Fount of Yak Knowledge

United Kingdom
1074 Posts

Posted - 05/04/2006 :  11:19:51  Show Profile  Reply with Quote
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

Slovenia
11751 Posts

Posted - 05/04/2006 :  11:29:31  Show Profile  Visit spirit1's Homepage  Reply with Quote
nice Ryan.

i always forget the stuff function exists

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

RyanRandall
Flowing Fount of Yak Knowledge

United Kingdom
1074 Posts

Posted - 05/04/2006 :  11:39:24  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000