如果情况有问题 [英] if condition problem
本文介绍了如果情况有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果出现某些情况,我想更新我的桌子
如果不是我想插入我的桌子
但我的错误在我的代码中由于某种原因可以有人告诉我什么是我的错误
I want to UPdate my table if some condition Takes place
if not i want to insert to my table
but i have errore in my code for some reason can anybody tell me what is my errore
ALTER PROC [dbo].[SqldataTable]
@id real,
@prod nvarchar(50),
@category varchar(50),
@price real,
@instock bit,
@count real,
@DateUpdate datetime,
@description nvarchar(max)
as
--somthing wrong here!
if (ID like @id)
begin
UPDATE [DBT]
set
ID=@id,
ProductName=@prod,
Category=@category,
Price=@price,
Instock=@instock,
[Count]=@count,
DateUpdate=@DateUpdate,
[Description]=@description
where ID=@id
end
else
begin
INSERT INTO [DBT] (ID,ProductName,Category,Price,Instock,[Count],DateUpdate,[Description])
VALUES (@id,@prod,@category,@price,@instock,@count,@DateUpdate,@description)
end
推荐答案
您需要声明ID(虽然需要将其命名为其他内容)并将该行更改为:
You need to declare ID(it will need to be named something else though) and change that line to this:
DECLARE @secondID varchar(max)
Set @secondID = Select ID FROM table WHERE ID = @id
IF @id = @secondID
BEGIN
--your code
END
ID将是你桌子和桌子中的id列,当然是你要查询的表。
"ID" will be the id column in your table and "table", of course is the table you are querying.
我不知道你为什么要比较ID(未声明或实例化),但我认为如果记录你想要INSERT ID = @ID不存在,如果确实存在则更新。
以下是我的方法。从Microsoft SQL Server 2008开始,有一个MERGE语句可用于产生相同的结果。
I do not know why you are trying to compare ID (which is not declared or instantiated) but I assume that you want to do an INSERT if the record with ID = @ID does not exist and an UPDATE if it does exist.
Below is how I would do it. Starting with Microsoft SQL Server 2008, there is a MERGE statement that could be used to cause the same result.
ALTER PROC [dbo].[SqldataTable]
@id real,
@prod nvarchar(50),
@category varchar(50),
@price real,
@instock bit,
@count real,
@DateUpdate datetime,
@description nvarchar(max)
as
if (select COUNT(*) from [DBT] where ID=@ID) > 0
begin
UPDATE [DBT]
set
ProductName=@prod,
Category=@category,
Price=@price,
Instock=@instock,
[Count]=@count,
DateUpdate=@DateUpdate,
[Description]=@description
where ID=@id
end
else
begin
INSERT INTO [DBT] (ID,ProductName,Category,Price,Instock,[Count],DateUpdate,[Description])
VALUES (@id,@prod,@category,@price,@instock,@count,@DateUpdate,@description)
end
这篇关于如果情况有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文