执行从“OUTPUT DELETED。[ColumnName]”语句返回的varchar的值

时间:2012-11-06 06:18:08

标签: sql sql-server-2008 stored-procedures

我正在尝试将表格用作heap queue。但是,我在弄清楚如何获取在dequeue函数中生成的文本并对其执行执行时遇到了一些麻烦。

这是存储队列和出列方法的表。

CREATE TABLE [dbo].[Merge_Queued_Work](Sql_Text varchar(2000) NOT NULL)
GO

create procedure usp_dequeueWork
as
  set nocount on;
  delete top(1) from [Merge_Queued_Work] with (rowlock, readpast)
      output deleted.[Sql_Text];      
go

如何将deleted.[Sql_Text]的varchar转换为execute语句?

1 个答案:

答案 0 :(得分:2)

捕获表变量中的输出并从表变量中填充varchar变量。

create procedure usp_dequeueWork
as
  set nocount on;

  declare @T table(Sql_Text varchar(2000))
  declare @S varchar(2000)

  delete top(1) from [Merge_Queued_Work] with (rowlock, readpast)
      output deleted.[Sql_Text] into @T;      

  select @S = Sql_Text from @T

  exec (@S)