Web Hosting Talk







View Full Version : MSSQL - Setting a SQL Select command as a variable


Goldfiles
05-21-2007, 04:43 PM
I want to run a 'SELECT' command on my database that will output a small table of data. About 3 rows and 3 columns. Can I set that 'SELECT' command to a @variable for later usage?

Something Like This? (doesn't work)

DECLARE @StatusTable varchar(100)

SET @variable = SELECT id, name, phone
FROM user_table

This is in MSSQL, btw. I'm new to MSSQL and I'm trying to figure out how I can set this table of data to a @variable so I can use it in a message later on in my code.

plumsauce
05-22-2007, 04:20 AM
Much better to do it with a stored procedure. Look in books online.

Goldfiles
05-22-2007, 08:41 AM
I want to include it in an email message later, like this:


SELECT @msg = Char(10) + Char(13)
SELECT @msg = @msg + 'Message Goes Here' + Char(10) + Char(13)
EXEC spSendEmail @msg, 'email@domain.com', 'Subject Line'


Any thoughts on how I can include a small table of data in an email message?

DatabaseMart
05-22-2007, 11:07 AM
You can use SQL Server XML technology. Please see http://www.15seconds.com/issue/001102.htm.