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
 General SQL Server Forums
 New to SQL Server Programming
 Build string in stored procedure

Author  Topic 

Pinto
Aged Yak Warrior

590 Posts

Posted - 2007-11-19 : 10:42:46
I have SQL table (tblUsers) and one of the fields holds the email address. I want to step through each record build a multiple email string to send to a lot of people. It would look like this

Str_email = Me@hotmail.com;Andy@Hotmail.com;Fred@Hotmail.com

I then want to pass Str_email back to an asp.web page

Can this be done in a stored procedure ?

jhocutt
Constraint Violating Yak Guru

385 Posts

Posted - 2007-11-19 : 10:53:30
Yes as long as the total length of the field is < 8000 chars.

"God does not play dice" -- Albert Einstein
"Not only does God play dice, but he sometimes throws them where they cannot be seen."
-- Stephen Hawking
Go to Top of Page

jhocutt
Constraint Violating Yak Guru

385 Posts

Posted - 2007-11-19 : 10:55:45
declare @str as varchar(8000)
set @str = ''
declare @a table (email varchar(100))
insert into @a
select 'me@you.com'
union all
select 'you@me.org'


select @str = @str + email + ';' from @a
select @str




"God does not play dice" -- Albert Einstein
"Not only does God play dice, but he sometimes throws them where they cannot be seen."
-- Stephen Hawking
Go to Top of Page

Pinto
Aged Yak Warrior

590 Posts

Posted - 2007-11-19 : 11:08:10
Thank you for your answer. Can I step through each record in the table and insert it instead of typing the email addresses in the sp as there are nearly 200 records on the table.
Go to Top of Page

jhocutt
Constraint Violating Yak Guru

385 Posts

Posted - 2007-11-19 : 11:15:54
That is what the example does.
declare @a table (email varchar(100))
insert into @a
select 'me@you.com'
union all
select 'you@me.org'

is just setting up a test table so you can see it work.



"God does not play dice" -- Albert Einstein
"Not only does God play dice, but he sometimes throws them where they cannot be seen."
-- Stephen Hawking
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2007-11-19 : 11:30:32
However, a better method would be pass all your data back to your ASP page, and let the page make the concatenation.

[Signature]For fast help, follow this link:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx
Learn SQL
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page
   

- Advertisement -