开发者

Placing column values in variables using single SQL query

开发者 https://www.devze.com 2022-12-25 07:11 出处:网络
In SQL server, how can I place the value of more than one column in variables using one query? Ex: my query is:开发者_开发问答

In SQL server, how can I place the value of more than one column in variables using one query?

Ex: my query is:

开发者_开发问答
SELECT ET.ID,ET.Description,ET.DefaultTemplateText 
FROM TBL_EMAILTEMPLATE ET 
WHERE ET.NAME='OneWeekReminder'

I want to place the column values in variables.


You can use the following syntax:

Declare @id INT
Declare @desc VarChar(100)
Declare @template VarChar(100)

SELECT @id = ET.ID, @desc = ET.Description, @template = ET.DefaultTemplateText 
FROM TBL_EMAILTEMPLATE ET 
WHERE ET.NAME='OneWeekReminder'


declare the variables first then set them in the select clause.

declare
    @ID int,
    @Description varchar(10),
    @DefaultTemplateText varchar(10)

select
    @ID = ET.ID,
    @Description = ET.Description,
    @DefaultTemplateText = ET.DefaultTemplateText
from
    TBL_EMAILTEMPLATE ET
where
    ET.NAME = 'OneWeekReminder'


You can separate multiple assignments with a comma. For example:

declare @a varchar(50)
declare @b varchar(50)

select 
    @a = et.Description
,   @b = et.DefaultTemplateText
from YourTable


Assuming only one row,

SELECT @id = ET.ID, @Description = ET.Description, @DefaultTemplateText = ET.DefaultTemplateText
FROM TBL_EMAILTEMPLATE ET 
WHERE ET.NAME='OneWeekReminder'
0

精彩评论

暂无评论...
验证码 换一张
取 消