MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 23rd, 2013, 04:09 AM
IncaKola IncaKola is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 1 IncaKola User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 29 sec
Reputation Power: 0
Formatting NVARCHAR with xp_sprintf and dynamic list of parameters

Hi all I need your help, how can you format a string with placeholders with the number of placeholders not known in advance (dynamic number of placeholders) ?

I need to do that in T-SQL

"xxxxx {0} yyyy {1} zzzz {2} "

with {} replaced by parameters.

I did the code:

Code:
DECLARE @PARAMETERS nvarchar(500)

DECLARE @TEST nvarchar(500)

DECLARE @QUERY  nvarchar(500)

SET @TEST = '%s %s %s'

SET @PARAMETERS='o,p,i'

SET @QUERY = '

PRINT @TEST;

EXEC xp_sprintf @TEST out, @TEST,' + @PARAMETERS + '

PRINT @TEST'


EXECUTE sp_executesql @QUERY,N'@TEST nvarchar(500), @PARAMETERS nvarchar(500)', @TEST,@PARAMETERS


That's working but... I had to put in concatenation @PARAMETERS (not a good practice)

I get the result:

%s %s %s
o p i

I tried to do this, so as to avoid SQL injection risk by injecting the parameters directly in the dynamic query.

Code:
DECLARE @PARAMETERS nvarchar(500)

DECLARE @TEST nvarchar(500)

DECLARE @QUERY  nvarchar(500)

SET @TEST = '%s %s %s'

SET @PARAMETERS='o,p,i'

SET @QUERY = '

PRINT @TEST;

EXEC xp_sprintf @TEST out, @TEST,@PARAMETERS

PRINT @TEST'


EXECUTE sp_executesql @QUERY,N'@TEST nvarchar(500), @PARAMETERS nvarchar(500)', @TEST,@PARAMETERS


I got the message:

%s %s %s
Error executing extended stored procedure: Invalid Parameter
Msg 50003, Level 1, State 0
%s %s %s

Please help Do you know a better way of doing things too?

Thanks

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Formatting NVARCHAR with xp_sprintf and dynamic list of parameters

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap