This is just a procedure and I specified the code field and all the values are returned without where
The input must include the name of the table and the schema, separated by a comma
I cannot use EXEC in functions, I had to use stored procedures
alter proc TestProc1-- '[dbo].[T1],[dbo].[T2]'
@ListTAble nvarchar(100)
as
drop table if exists #listvalues;drop table if exists #tables
create table #listvalues (datas uniqueidentifier)
select
'['+a.TABLE_SCHEMA+ '].' + '['+a.TABLE_NAME+ ']' as TAbleNAme
,ROW_NUMBER() over(order by TABLE_NAME) as rw
into #tables
from INFORMATION_SCHEMA.TABLES a
where '['+a.TABLE_SCHEMA+ '].' + '['+a.TABLE_NAME+ ']' in (select * from string_split(@ListTAble,',') )
declare @query varchar(max)=''
declare @Count int=(select count(*) from #tables)
declare @I int=1
while(@Count>=@I)
begin
set @query=' insert into #listvalues (datas) select Id from ' + (select TAbleNAme from #tables where rw=@I)
exec(@query)
set @I=@I+1
end
select STRING_AGG(cast( datas as nvarchar(max)),',') from #listvalues
create table T1(id uniqueidentifier,Name nvarchar(100))
create table T2(id uniqueidentifier,Name1 nvarchar(100))
create table T3(id uniqueidentifier,Name2 nvarchar(100))
insert into T1 (id,Name) values(NEWID(),'ff')
insert into T1 (id,Name) values(NEWID(),'ff')
insert into T1 (id,Name) values(NEWID(),'ff')
insert into T2 (id,Name1) values(NEWID(),'ff')
insert into T2 (id,Name1) values(NEWID(),'ff')
insert into T2 (id,Name1) values(NEWID(),'ff')
insert into T3 (id,Name2) values(NEWID(),'ff')
insert into T3 (id,Name2) values(NEWID(),'ff')
insert into T3 (id,Name2) values(NEWID(),'ff')
insert into T3 (id,Name2) values(NEWID(),'ff')