create table #t2 (id int,name varchar(10))
-----------------------------------------------------------
alter PROC usp_commonvalues
AS
BEGIN
truncate table #t2
DECLARE @t table (id int,name varchar(10))
insert into @t values(1,'a'),(3,'c'),(2,'d'),(4,'e')
DECLARE @t1 table(id int,name varchar(10))
insert into @t1 values(1,'b'),(4,'e')
DECLARE @id INT
DECLARE @name VARCHAR(10)=''
DECLARE @i INT=1
DECLARE @count INT
SELECT @count=max(ID) FROM @T
WHILE(@i<=@count)
BEGIN
DECLARE @ID1 INT
DECLARE @NAME1 VARCHAR(10)=''
SELECT @NAME=NAME FROM @t WHERE id=@i
DECLARE @J INT=1
DECLARE @CNT INT
SELECT @CNT=max(ID) FROM @t1
WHILE(@J<=@CNT)
BEGIN
SELECT @NAME1=name,@ID1=id FROM @t1 WHERE id=@J
IF(@name=@NAME1)
BEGIN
INSERT INTO #t2(id,name) select id,name from @t1 where id=@J
END
SET @J=@J+1
END
set @J=1
SET @i=@I+1
END
select * from #t2
END
--EXEC usp_commonvalues
-----------------------------------------------------------
alter PROC usp_commonvalues
AS
BEGIN
truncate table #t2
DECLARE @t table (id int,name varchar(10))
insert into @t values(1,'a'),(3,'c'),(2,'d'),(4,'e')
DECLARE @t1 table(id int,name varchar(10))
insert into @t1 values(1,'b'),(4,'e')
DECLARE @id INT
DECLARE @name VARCHAR(10)=''
DECLARE @i INT=1
DECLARE @count INT
SELECT @count=max(ID) FROM @T
WHILE(@i<=@count)
BEGIN
DECLARE @ID1 INT
DECLARE @NAME1 VARCHAR(10)=''
SELECT @NAME=NAME FROM @t WHERE id=@i
DECLARE @J INT=1
DECLARE @CNT INT
SELECT @CNT=max(ID) FROM @t1
WHILE(@J<=@CNT)
BEGIN
SELECT @NAME1=name,@ID1=id FROM @t1 WHERE id=@J
IF(@name=@NAME1)
BEGIN
INSERT INTO #t2(id,name) select id,name from @t1 where id=@J
END
SET @J=@J+1
END
set @J=1
SET @i=@I+1
END
select * from #t2
END
--EXEC usp_commonvalues