-1

I have two stored procedures that return result sets.

How can I use them to populate a string and update another column?

exec getEmailSignatureDetails 'Jane', 'Doe'

exec getFeaturedAccount 'June'

These both return columns that I would like to map to variables.

I would then like to put the variables into a string.

Then update a column in another table with that string.

Output from getEmailSignatureDetails:

addCity       | addLine     | addSt |  addZip | fName | lName
--------------+-------------+-------+---------+-------+------
San Francisco | 777 SV Lane | CA    |  94016  | Jane  | Doe

Output from getFeaturedAccount:

month | img      
------+----------
June  | base64...

I would like to turn this into a string like

Your package has been delivered to 
@fName @lName
@addLine
@addCity @addSt, @addZip
@img

And then update a column with this string matching on name.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
KVNA
  • 847
  • 1
  • 11
  • 24

1 Answers1

2

If I understand your question your are looking to dynamically fill-in a template via macro substitution

Example

-- Create some Sample Data
Declare @getEmailSignatureDetails Table ([addCity] varchar(50),[addLine] varchar(50),[addSt] varchar(50),[addZip] varchar(50),[fName] varchar(50),[lName] varchar(50))
Insert Into @getEmailSignatureDetails Values 
 ('San Francisco','777 SV Lane','CA',94016,'Jane','Doe')

Declare @getFeaturedAccount Table ([month] varchar(50),[img] varchar(50))
Insert Into @getFeaturedAccount Values 
 ('June','base64..')



-- Declare the Template
Declare @Template varchar(max) ='
Your package has been delivered to 
@fName @lName
@addLine
@addCity @addSt, @addZip
@img
'

-- Populate the Template
Select @Template = replace(@Template,'@'+Field,Value)
 From ( 
        Select C.*
         From (values (convert(XML,(Select * From  @getEmailSignatureDetails Join  @getFeaturedAccount on [month]='June' For XML Raw ) ) ) ) A(XMLData)
         Cross Apply (
                        Select Field = a.value('local-name(.)','varchar(100)')
                              ,Value = a.value('.','varchar(max)') 
                         From  A.XMLData.nodes('/row')  as C1(n)
                         Cross Apply C1.n.nodes('./@*') as C2(a)
                         Where a.value('local-name(.)','varchar(100)') not in ('Column1','Column2')
                     ) C
     ) A

Updated Template

Your package has been delivered to 
Jane Doe
777 SV Lane
San Francisco CA, 94016
base64.. 

If it helps with the visualization, the sub-query is a "dynamic" unpivot and generates the following:

Field     Value
addCity   San Francisco
addLine   777 SV Lane
addSt     CA
addZip    94016
fName     Jane
lName     Doe
month     June
img       base64..   -- (presumably would be the image)
John Cappelletti
  • 79,615
  • 7
  • 44
  • 66