0

有 2 个表,例如让我们称一个名字,第二个年龄

names table :                   age table :
ID      Names               ID      Names    Age
1       Bob                  1      Bob      18
2       Tommy                2      Tommy    21
3       Kate                 3      Kate     20
4       Adam                 4      Adam     23
5       Karl                 5      Karl     25

在存储过程中要检查名称表中的名称是否存在于年龄表中

create table #tbl
(
  id int identity(1,1),
  age int
)

insert into #tbl (age) values(and hear each age for each name)

我想我需要一些循环来做没有光标但如何?请帮我

编辑

我像这样解决它:

create table #tbl 
(
  id int identity(1,1),
  age int
);

INSERT INTO #tbl (age) 
SELECT a.age
  FROM age a
 WHERE NOT EXISTS (
   SELECT n.name
   FROM names n
   WHERE a.Name = n.Name)

   select * from #tbl 
4

1 回答 1

1

试试这个:

create table #tbl 
(
  id int identity(1,1),
  age int
);

INSERT INTO #tbl (age) 
SELECT a.age
FROM age a
LEFT JOIN names N ON N.Name = A.Name
WHERE N.Name IS NULL

select * from #tbl 
于 2013-10-28T18:12:35.837 回答