0

我有一个存储过程,它检索多行 3 列项目。我正在以 DataTable 的格式检索。当我调试它时,它给了我错误

必须声明标量变量@Ticket。

但我已经宣布了。

存储过程:

BEGIN

Declare @Ticket Numeric(28,0)
Declare @SQL VarChar(Max)
Declare @SQLUpdate VarChar(Max)

 Set @SQL='Select @Ticket=Ticket,VendorTicket[Vendor Ticket],Comments From dbo.VendorTickets Where NotifyOn <= GetDate() And IsNull(NotifyOn,0)<>0 '
 Exec(@SQL)

 Set @SQLUpdate='Update dbo.VendorTicket Set NotifyOn=0 Where Ticket=@Ticket'
 Exec(@SQLUpdate)

END

调用存储过程的代码

SqlConnection oConn = null;
DataTable dtReturn = null;
try
{
getConnection(ref oConn, 1);
using (SqlStoredProcedure sspObj = new SqlStoredProcedure("dbo.usp_checkNotification", oConn, CommandType.StoredProcedure))
{
dtReturn = sspObj.ExecuteDataTable();
sspObj.Dispose();
}
closeConnection(ref oConn);
}
4

2 回答 2

0

为什么要使用动态 SQL?

就这样做

 Update dbo.VendorTickets
 Set NotifyOn=0 
 Where NotifyOn <= GetDate() 
 And NotifyOn IS NOT NULL

请注意,将日期时间 (NotifyOn) 设置为 0 会将其设置为 1900-01-01。

于 2012-08-11T07:57:03.257 回答
0
Select @Ticket=Ticket,VendorTicket[Vendor Ticket],Comments From dbo.VendorTickets Where NotifyOn <= GetDate() And IsNull(NotifyOn,0)<>0

意味着您选择的结果将写入变量@Ticket,因此@Ticket 必须是表变量,但您声明@Ticket Numeric(28,0)。你可以通过下一个 sql 脚本做你想做的事:

Update dbo.VendorTicket 
Set NotifyOn=0 
Where Ticket in (
      Select Ticket 
      from dbo.VendorTickets 
      Where NotifyOn <= GetDate() And IsNull(NotifyOn,0)<>0
      )
于 2012-08-11T08:17:51.257 回答