0

我正在使用 ms visual studio 2012 和 ms sql 2012。

在我的 asp.net VB 页面上,我有 4 个文本框,用户可以输入值并单击插入。然后这些值与日期、用户名和一个整数一起通过子例程传递给 sql,该子例程执行第一个文本框值,然后是第二个,依此类推。

我的问题是存储过程。我使用 IF NOT exist 来查看表中的当前数据,如果没有与日期和整数匹配的数据,那么它将插入记录。然后,VB 子程序将传递第二组数据,它会再次查看日期是否与整数一起存在,依此类推。存储过程如下:

@price money,
@datesubmitted datetime,
@commodityID int,
@submitted_By nvarchar(10)

As
    begin
    if not exists ((select * from dailyPricing where (convert(date, datesubmitted, 103) = convert(date, @datesubmitted, 103) and commodityID = 1)
    or
    (convert(date, datesubmitted, 103) = convert(date, @datesubmitted, 103) and commodityID = 2)
    or
    (convert(date, datesubmitted, 103) = convert(date, @datesubmitted, 103) and commodityID = 3)
    or
    (convert(date, datesubmitted, 103) = convert(date, @datesubmitted, 103) and commodityID = 4)))
    Begin
    INSERT INTO dailyPricing (price, datesubmitted, commodityID, submitted_By)
    values(@price, @datesubmitted, @commodityID, @submitted_By)

end
end

上面的结果是它只输入了第一组值,而不是第二、第三或第四组。我已经调试了我的 VB 代码并且它工作正常我只是认为我没有正确地形成 SQL。

4

1 回答 1

1

I think you need to rewrite that "not exists" for every check try something like this, and i think you need to update the brackets they might be wrong:

if not exists ((select * from dailyPricing where 
  (convert(date, datesubmitted, 103)
     = convert(date, @datesubmitted, 103) and commodityID = 1)

or

if not exists ((select * from dailyPricing where 
   (convert(date, datesubmitted, 103) 
     = convert(date, @datesubmitted, 103) and commodityID = 2)

or ....

于 2013-10-25T11:06:07.733 回答