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.
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) = ','asbeginset @params = @params + @paramSeparator -- so we don't have to specially handle the last itemdeclare @param varchar(200), @leftBodyOld varchar(8000), @leftBodyNew varchar(8000), @i intset @i = 0while @params <> '' and @i < 8000 -- max 8000 iterationsbegin 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 accidentendselect @bodyendgodeclare @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, @paramsgodrop proc xpSprintF Go with the flow & have fun! Else fight the flow Blog thingie: [URL="http://weblogs.sqlteam.com/mladenp"] |
|
|
datagod
Starting Member
37 Posts |
Posted - 2006-05-04 : 10:59:00
|
Fantastic! Thank you very much!! |
|
|
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)asbegindeclare @p varchar(8000)set @params = @params + @separatorwhile 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)endreturn @send Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
|
|
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"] |
|
|
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 Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
|
|
|
|
|
|
|