如果基于MemberID
子句存在WHERE
,我正在检查SQL Server数据库
IF EXISTS (SELECT MemberID FROM ADMIN_Biosketch_Members
WHERE emailAddress = @emailaddress)
如果它存在,我需要将该值插入另一个表中:
SELECT @MemberID = MemberID
FROM ADMIN_Biosketch_Members
WHERE emailAddress = @emailaddress
有没有办法可以一步完成?我试过了
IF EXISTS (SELECT @MemberID = MemberID
FROM ADMIN_Biosketch_Members
WHERE emailAddress = @emailaddress)
但它不喜欢那样。
由于
答案 0 :(得分:1)
嗯,您可以在获取值后检查NULL
:
SELECT @MemberID = MemberID
FROM ADMIN_Biosketch_Members
WHERE emailAddress = @emailaddress
IF @MemberID IS NOT NULL
-- value exists --> do something with it
如果您的表格中没有@emailaddress
值,则@MemberID
会变为NULL
。
答案 1 :(得分:0)
然后你可以像下面这样做,而不是使用EXISTS
SELECT @MemberID = MemberID FROM ADMIN_Biosketch_Members WHERE emailAddress = @emailaddress
if @MemberID is not null
begin
--your sql
end
答案 2 :(得分:0)
我觉得你在搜索类似的东西
-- this is your current table
create table #tmp (ID int, Name nvarchar(100))
insert into #tmp select 1, 'John'
insert into #tmp select 2, 'Andrey'
insert into #tmp select 3, 'Daniel'
-- this new table where you should insert your value
create table #tmp2 (ID int, Name nvarchar(100))
-- trick with inserting in case if it exists
insert into #tmp2
select *
from #tmp t
where t.ID in (select t2.ID from #tmp t2 where t2.ID = 3)
select *
from #tmp2
在你的情况下它看起来像
insert into ADMIN_Biosketch_Members_NEW(MemberID)
select MemberID
from ADMIN_Biosketch_Members t
where t.MemberID in (select t2.MemberID from ADMIN_Biosketch_Members t2 WHERE t2.emailAddress = @emailaddress)