2012-08-11 117 views
0

我有一个存储过程,它检索3列项目的多行。我正在以DataTable的格式检索。当我调试它时,它给我错误必须声明SQL Server存储过程中的标量变量

必须声明标量变量@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 [email protected]' 
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); 
} 
+0

你为什么使用动态SQL呢? 'NotifyOn'的数据类型是什么? – 2012-08-11 07:56:05

+0

NotifyOn数据类型是日期时间 – Shaggy 2012-08-11 08:05:15

回答

0

你为什么要使用动态SQL?

只是这样做

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

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

+0

没有只有一个表我有“VendorTickets”...很抱歉,它是一个错误 – Shaggy 2012-08-11 08:02:16

+0

我只有一个表第一个查询做选择操作,第二个做基于第一个选择查询中的值返回的更新 – Shaggy 2012-08-11 08:07:47

+0

那就更简单了。请参阅编辑。 – podiluska 2012-08-11 08:07:55

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

意味着你选择的是resul将写入变量@Ticket,所以@Ticket必须是表变量,但你申报@Ticket数字(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 
    )