我试图以编程方式在我的表中输入值.
我不能直接选择@variables.我必须使用关键字Values.
如何在insert中使用Values时创建where子句.
我试图避免重复
DECLARE @MyID INT DECLARE @Phone varchar(10) DECLARE @MyDATE DateTime DECLARE @Agent as varchar(50) DECLARE @Charge as varchar(50) DECLARE @Vendor as varchar(50) SET @MyID = 215199999 SET @Phone = '9999999999' SET @MyDATE = '2010-12-04 11:56:12.000' SET @Agent = 'fbrown' SET @Charge = 'NO' SET @Vendor = 'NO' INSERT INTO [MyDB].[dbo].[Accounts] (MyID,Phone,MyDate,Agent,Charge,Vendor) VALUES ( @MyID,@Phone,@MyDATE,@Agent,@Charge,@Vendor ) WHERE MyID NOT IN (@MyID)
解决方法
IF NOT EXISTS(SELECT 1 FROM [MyDB].[dbo].[Accounts] WHERE MyID = @MyID) INSERT INTO [MyDB].[dbo].[Accounts] (MyID,Vendor) VALUES (@MyID,@Vendor)