我有一个选择存储过程,我正在努力使它所带来的结果也会更新一个名为Downloaded
的列,并将这些行标记为下载。
例如,我将那10行中的10行下拉,我还希望在同一存储过程中将Downloaded
列更新为true。这可能吗?
到目前为止,这是我的sp,它会下载数据。
ALTER PROCEDURE [dbo].[GetLeads]
@DateTo datetime = null,
@DateFrom datetime = null
AS
SELECT name
, lastname
, title
, company
, address
, address2
, city
, [state]
, zip
, country
, stamptime
FROM
lead
where
((@DateTo is null AND @DateFrom IS null) or (stamptime BETWEEN @DateTo AND @DateFrom))
谢谢!
答案 0 :(得分:12)
您只需OUTPUT
更新的行;
UPDATE lead
SET Downloaded = 1
OUTPUT INSERTED.*
WHERE ((@DateTo is null AND @DateFrom IS null) or (stamptime BETWEEN @DateTo AND @DateFrom))
此更新,然后在单个语句中返回更新的行。
答案 1 :(得分:4)
继续关注vulkanino的评论答案,如下:
ALTER PROCEDURE [dbo].[GetLeads]
@DateTo datetime = null,
@DateFrom datetime = null
AS
UPDATE
lead
SET
Downloaded = 1
WHERE
((@DateTo is null AND @DateFrom IS null) or (stamptime BETWEEN @DateTo AND @DateFrom))
SELECT name
, lastname
, title
, company
, address
, address2
, city
, [state]
, zip
, country
, stamptime
FROM
lead
where
((@DateTo is null AND @DateFrom IS null) or (stamptime BETWEEN @DateTo AND @DateFrom))
答案 2 :(得分:2)
您最好的选择可能是使用带UPDATE的OUTPUT语句。
DECLARE @TEMPTABLE
(
name <type>
, lastname <type>
, title <type>
, company <type>
, address <type>
, address2 <type>
, city <type>
, state <type>
, zip <type>
, country <type>
, stamptime <type>
)
UPDATE a
SET a.Downloaded = 1
OUTPUT Inserted.name, Inserted.lastname, Inserted.title, etc. INTO @TEMPTABLE
FROM lead a
WHERE ((@DateTo IS NULL AND @DateFrom IS NULL) OR (a.stamptime BETWEEN @DateTo AND @DateFrom))
SELECT * FROM @TEMPTABLE