想在一个表Table1中查找一条记录(比如查找字段 Name ='阿娇'),如果该记录不存在,就直接插入一条新记录,如果该记录已经存在,就更新它的Name 值.
一条SQL能否搞定?
20 个解决方案
#1
没有INsert语句,怎么插入新纪录?
#2
if exists (select 1 from tb where name = 'aa')
begin
update tb
set name = 'bb'
where name = 'aa'
end
else
begin
insert into tb(name) select 'aa'
end
#3
if exists(select * from Table1 where Name='阿娇')
update Table1
set Name = 'xxxx'
where ID=123
else
insert Table1(...)valuses(...)
update Table1
set Name = 'xxxx'
where ID=123
else
insert Table1(...)valuses(...)
#4
不知有些方法.参考如下:
if exists(select 1 from Table1 where [Name] = N'阿娇')
update Table1 set [Name] = N'新阿娇' where [Name] = N'阿娇'
else
insert Table1([Name]) values(N'阿娇')
#5
if exists (select 1 from tb where name = '阿娇'')
begin
update tb
set name = 'bb'
where name = '阿娇'
end
else
begin
insert into tb(name) select 'aa'
end
#6
if exists (select 1 from tb where name = '阿娇')
begin
update tb
set name = 'bb'
where name = '阿娇'
end
else
begin
insert into tb(name) select 'aa'
end
#7
if exists (select 1 from tb where name = '阿娇')
update tb set name = '冠希' where name = '阿娇'
else
insert into tb select '冠希'
#8
if exists(select name from table1 where name='阿娇')
insert into table1(字段1,字段2....) values (值1,值2,...)
else
update table1 set name='要更新的值' where name='阿娇'
#9
搞反了
if not exists(select name from table1 where name='阿娇')
insert into table1(字段1,字段2....) values (值1,值2,...)
else
update table1 set name='要更新的值' where name='阿娇'
#11
if exists(select * from tableA where Name ='阿娇')
update tableA set Name='NewName' where Name ='阿娇'
else
insert tableA values('value1','value2','NewName')
#12
介绍下用法吧
#13
if exists(select * from Table1 where Name='阿娇')
update Table1
set 更新的值
where Name='阿娇'
else
insert Table1(字段1,字段2.。。。。。)
valuses(字段1值,字段2值)
ps:执行这种语句,所过滤的字段(name)最好是主键,否则可能更新多个记录
update Table1
set 更新的值
where Name='阿娇'
else
insert Table1(字段1,字段2.。。。。。)
valuses(字段1值,字段2值)
ps:执行这种语句,所过滤的字段(name)最好是主键,否则可能更新多个记录
#14
if exists(select * from test where id = 1111)
update test set name ='test' where id = 1
else
insert into test(id,name) values(11,'test')
update test set name ='test' where id = 1
else
insert into test(id,name) values(11,'test')
#15
就用一句sql我还真搞不定...
#16
if not exists(select 1 from tb where name = '阿娇')
begin
insert into tb(name)
select '阿娇'
end
else
begin
update tb set name = 'fdf' where name = '阿娇'
end
#17
USE AdventureWorks;
GO
IF OBJECT_ID (N'dbo.Purchases', N'U') IS NOT NULL
DROP TABLE dbo.Purchases;
GO
CREATE TABLE dbo.Purchases (
ProductID int, CustomerID int, PurchaseDate datetime,
CONSTRAINT PK_PurchProdID PRIMARY KEY(ProductID,CustomerID));
GO
INSERT INTO dbo.Purchases VALUES(707, 11794, '20060821'),
(707, 15160, '20060825'),(708, 18529, '20060821'),
(711, 11794, '20060821'),(711, 19585, '20060822'),
(712, 14680, '20060825'),(712, 21524, '20060825'),
(712, 19072, '20060821'),(870, 15160, '20060823'),
(870, 11927, '20060824'),(870, 18749, '20060825');
GO
IF OBJECT_ID (N'dbo.FactBuyingHabits', N'U') IS NOT NULL
DROP TABLE dbo.FactBuyingHabits;
GO
CREATE TABLE dbo.FactBuyingHabits (
ProductID int, CustomerID int, LastPurchaseDate datetime,
CONSTRAINT PK_FactProdID PRIMARY KEY(ProductID,CustomerID));
GO
INSERT INTO dbo.FactBuyingHabits VALUES(707, 11794, '20060814'),
(707, 18178, '20060818'),(864, 14114, '20060818'),
(866, 13350, '20060818'),(866, 20201, '20060815'),
(867, 20201, '20060814'),(869, 19893, '20060815'),
(870, 17151, '20060818'),(870, 15160, '20060817'),
(871, 21717, '20060817'),(871, 21163, '20060815'),
(871, 13350, '20060815'),(873, 23381, '20060815');
GO
/*
dbo.Purchases
ProductID CustomerID PurchaseDate
----------- ----------- -----------------------
707 11794 2006-08-20 00:00:00.000
707 15160 2006-08-25 00:00:00.000
708 18529 2006-08-21 00:00:00.000
711 11794 2006-08-20 00:00:00.000
711 19585 2006-08-22 00:00:00.000
712 14680 2006-08-26 00:00:00.000
712 21524 2006-08-26 00:00:00.000
712 19072 2006-08-20 00:00:00.000
870 15160 2006-08-23 00:00:00.000
870 11927 2006-08-24 00:00:00.000
870 18749 2006-08-25 00:00:00.000
dbo.FactBuyingHabits
ProductID CustomerID LastPurchaseDate
----------- ----------- -----------------------
707 11794 2006-08-14 00:00:00.000
707 18178 2006-08-18 00:00:00.000
864 14114 2006-08-18 00:00:00.000
866 13350 2006-08-18 00:00:00.000
866 20201 2006-08-15 00:00:00.000
867 20201 2006-08-14 00:00:00.000
869 19893 2006-08-15 00:00:00.000
870 17151 2006-08-18 00:00:00.000
870 15160 2006-08-17 00:00:00.000
871 21717 2006-08-17 00:00:00.000
871 21163 2006-08-15 00:00:00.000
871 13350 2006-08-15 00:00:00.000
873 23381 2006-08-15 00:00:00.000
*/
#18
MERGE dbo.FactBuyingHabits AS Target
USING (SELECT CustomerID, ProductID, PurchaseDate FROM dbo.Purchases) AS Source
ON (Target.ProductID = Source.ProductID AND Target.CustomerID = Source.CustomerID)
WHEN MATCHED THEN
UPDATE SET Target.LastPurchaseDate = Source.PurchaseDate
WHEN NOT MATCHED BY TARGET THEN
INSERT (CustomerID, ProductID, LastPurchaseDate)
VALUES (Source.CustomerID, Source.ProductID, Source.PurchaseDate)
OUTPUT $action, Inserted.*, Deleted.*;
#20
#21
#1
没有INsert语句,怎么插入新纪录?
#2
if exists (select 1 from tb where name = 'aa')
begin
update tb
set name = 'bb'
where name = 'aa'
end
else
begin
insert into tb(name) select 'aa'
end
#3
if exists(select * from Table1 where Name='阿娇')
update Table1
set Name = 'xxxx'
where ID=123
else
insert Table1(...)valuses(...)
update Table1
set Name = 'xxxx'
where ID=123
else
insert Table1(...)valuses(...)
#4
不知有些方法.参考如下:
if exists(select 1 from Table1 where [Name] = N'阿娇')
update Table1 set [Name] = N'新阿娇' where [Name] = N'阿娇'
else
insert Table1([Name]) values(N'阿娇')
#5
if exists (select 1 from tb where name = '阿娇'')
begin
update tb
set name = 'bb'
where name = '阿娇'
end
else
begin
insert into tb(name) select 'aa'
end
#6
if exists (select 1 from tb where name = '阿娇')
begin
update tb
set name = 'bb'
where name = '阿娇'
end
else
begin
insert into tb(name) select 'aa'
end
#7
if exists (select 1 from tb where name = '阿娇')
update tb set name = '冠希' where name = '阿娇'
else
insert into tb select '冠希'
#8
if exists(select name from table1 where name='阿娇')
insert into table1(字段1,字段2....) values (值1,值2,...)
else
update table1 set name='要更新的值' where name='阿娇'
#9
搞反了
if not exists(select name from table1 where name='阿娇')
insert into table1(字段1,字段2....) values (值1,值2,...)
else
update table1 set name='要更新的值' where name='阿娇'
#10
#11
if exists(select * from tableA where Name ='阿娇')
update tableA set Name='NewName' where Name ='阿娇'
else
insert tableA values('value1','value2','NewName')
#12
介绍下用法吧
#13
if exists(select * from Table1 where Name='阿娇')
update Table1
set 更新的值
where Name='阿娇'
else
insert Table1(字段1,字段2.。。。。。)
valuses(字段1值,字段2值)
ps:执行这种语句,所过滤的字段(name)最好是主键,否则可能更新多个记录
update Table1
set 更新的值
where Name='阿娇'
else
insert Table1(字段1,字段2.。。。。。)
valuses(字段1值,字段2值)
ps:执行这种语句,所过滤的字段(name)最好是主键,否则可能更新多个记录
#14
if exists(select * from test where id = 1111)
update test set name ='test' where id = 1
else
insert into test(id,name) values(11,'test')
update test set name ='test' where id = 1
else
insert into test(id,name) values(11,'test')
#15
就用一句sql我还真搞不定...
#16
if not exists(select 1 from tb where name = '阿娇')
begin
insert into tb(name)
select '阿娇'
end
else
begin
update tb set name = 'fdf' where name = '阿娇'
end
#17
USE AdventureWorks;
GO
IF OBJECT_ID (N'dbo.Purchases', N'U') IS NOT NULL
DROP TABLE dbo.Purchases;
GO
CREATE TABLE dbo.Purchases (
ProductID int, CustomerID int, PurchaseDate datetime,
CONSTRAINT PK_PurchProdID PRIMARY KEY(ProductID,CustomerID));
GO
INSERT INTO dbo.Purchases VALUES(707, 11794, '20060821'),
(707, 15160, '20060825'),(708, 18529, '20060821'),
(711, 11794, '20060821'),(711, 19585, '20060822'),
(712, 14680, '20060825'),(712, 21524, '20060825'),
(712, 19072, '20060821'),(870, 15160, '20060823'),
(870, 11927, '20060824'),(870, 18749, '20060825');
GO
IF OBJECT_ID (N'dbo.FactBuyingHabits', N'U') IS NOT NULL
DROP TABLE dbo.FactBuyingHabits;
GO
CREATE TABLE dbo.FactBuyingHabits (
ProductID int, CustomerID int, LastPurchaseDate datetime,
CONSTRAINT PK_FactProdID PRIMARY KEY(ProductID,CustomerID));
GO
INSERT INTO dbo.FactBuyingHabits VALUES(707, 11794, '20060814'),
(707, 18178, '20060818'),(864, 14114, '20060818'),
(866, 13350, '20060818'),(866, 20201, '20060815'),
(867, 20201, '20060814'),(869, 19893, '20060815'),
(870, 17151, '20060818'),(870, 15160, '20060817'),
(871, 21717, '20060817'),(871, 21163, '20060815'),
(871, 13350, '20060815'),(873, 23381, '20060815');
GO
/*
dbo.Purchases
ProductID CustomerID PurchaseDate
----------- ----------- -----------------------
707 11794 2006-08-20 00:00:00.000
707 15160 2006-08-25 00:00:00.000
708 18529 2006-08-21 00:00:00.000
711 11794 2006-08-20 00:00:00.000
711 19585 2006-08-22 00:00:00.000
712 14680 2006-08-26 00:00:00.000
712 21524 2006-08-26 00:00:00.000
712 19072 2006-08-20 00:00:00.000
870 15160 2006-08-23 00:00:00.000
870 11927 2006-08-24 00:00:00.000
870 18749 2006-08-25 00:00:00.000
dbo.FactBuyingHabits
ProductID CustomerID LastPurchaseDate
----------- ----------- -----------------------
707 11794 2006-08-14 00:00:00.000
707 18178 2006-08-18 00:00:00.000
864 14114 2006-08-18 00:00:00.000
866 13350 2006-08-18 00:00:00.000
866 20201 2006-08-15 00:00:00.000
867 20201 2006-08-14 00:00:00.000
869 19893 2006-08-15 00:00:00.000
870 17151 2006-08-18 00:00:00.000
870 15160 2006-08-17 00:00:00.000
871 21717 2006-08-17 00:00:00.000
871 21163 2006-08-15 00:00:00.000
871 13350 2006-08-15 00:00:00.000
873 23381 2006-08-15 00:00:00.000
*/
#18
MERGE dbo.FactBuyingHabits AS Target
USING (SELECT CustomerID, ProductID, PurchaseDate FROM dbo.Purchases) AS Source
ON (Target.ProductID = Source.ProductID AND Target.CustomerID = Source.CustomerID)
WHEN MATCHED THEN
UPDATE SET Target.LastPurchaseDate = Source.PurchaseDate
WHEN NOT MATCHED BY TARGET THEN
INSERT (CustomerID, ProductID, LastPurchaseDate)
VALUES (Source.CustomerID, Source.ProductID, Source.PurchaseDate)
OUTPUT $action, Inserted.*, Deleted.*;