我试图在另一个SQL查询中将SQL查询检索到的行用作列和表名
例如:
declare @var1 varchar(max)
declare @var2 varchar(max)
declare @result varchar(max)
set @var1 = 'cctl_country'
set @var2 = 'typecode'
set @result = 'select '+@var2+' from '+@var1
print @result
在上面的示例中,输出为select typecode from cctl_country
。
它也应该像select cctl_country,typecode from cctl_country
一样。 table_name应该跟着typecode。
但是,扭曲的是@ var1是动态的并且不断变化,这需要从信息模式中检索。 因此,对于@ var1值的每次更改,我都需要执行select查询并将结果集插入另一个表中。 我在下面的代码中尝试了同样的事情但是我被卡住了。一点帮助将不胜感激
CREATE PROCEDURE usp_try
AS
BEGIN
DECLARE @StartLoop INT
DECLARE @EndLoop INT
DECLARE @esal INT
DECLARE @sqlquery varchar(max)
DECLARE @Result TABLE (table_name nvarchar(100),typecodes nvarchar(100))
DECLARE @InitResult TABLE (id INT IDENTITY(1, 1),
table_name NVARCHAR(50),
typecode NVARCHAR(50))
INSERT INTO @InitResult
select col.TABLE_NAME,'typecodes' as typecodes from [NFUM_Studio].INFORMATION_SCHEMA.COLUMNS col
inner join [NFUM_Studio].INFORMATION_SCHEMA.TABLES tab
on col.TABLE_NAME = tab.TABLE_NAME
where col.COLUMN_NAME = 'typecode'
SELECT @StartLoop = MIN(ID),
@EndLoop = MAX(ID)
FROM @InitResult
WHILE @StartLoop <= @EndLoop
BEGIN
SET @sqlquery = 'insert into @Result
select table_name,typecode from @InitResult'
SET @StartLoop = @StartLoop + 1
END
SELECT *
FROM @Result
END
答案 0 :(得分:1)
尝试使用Temp Table而不是Table变量,并且不需要将Information_Schema.Columns
表与Information_Schema.Tables
连接起来。您也错过了执行@strQuery
中存储的查询,只需使用EXEC(@strQuery)
CREATE PROCEDURE usp_try
AS
BEGIN
DECLARE @StartLoop INT
DECLARE @EndLoop INT
DECLARE @esal INT
DECLARE @sqlquery varchar(max)
CREATE TABLE #Result(table_name nvarchar(100),typecodes nvarchar(100))
CREATE TABLE #InitResult(id INT IDENTITY(1, 1),
table_name NVARCHAR(50),
typecode NVARCHAR(50))
INSERT INTO #InitResult
select col.TABLE_NAME,'typecodes' as typecodes from [NFUM_Studio].INFORMATION_SCHEMA.COLUMNS col
where col.COLUMN_NAME = 'typecode'
SELECT @StartLoop = MIN(ID),
@EndLoop = MAX(ID)
FROM @InitResult
WHILE @StartLoop <= @EndLoop
BEGIN
SET @sqlquery = 'insert into #Result SELECT * FROM ('
SELECT @sqlquery = @sqlquery + 'SELECT ''' + Table_name + ''' as [Table_name], [' + type_code + '] FROM ' + Table_name ' UNION ALL '
FROM #InitResult
SET @sqlquery = SUBSTRING(@strquery,1,LEN(@strquery) - 11)
SET @sqlquery = @sqlquery + ' ) AS T1'
EXEC(@strquery)
SET @StartLoop = @StartLoop + 1
END
SELECT *
FROM #Result
END
答案 1 :(得分:1)
最后我能够获得输出,但是在光标的帮助下
DECLARE @@CC_TABLENAME VARCHAR(255)
declare @cc_exec varchar(255)
declare @cc_table varchar(255)
create table #temp(table_name varchar(max),typecode varchar(max))
DECLARE @CSR_TL AS CURSOR;
SET @CSR_TL = CURSOR FOR
select col.TABLE_NAME from [NFUM_Studio].INFORMATION_SCHEMA.COLUMNS col
where col.COLUMN_NAME = 'typecode'
OPEN @CSR_TL
FETCH NEXT FROM @CSR_TL INTO @@CC_TABLENAME
WHILE @@FETCH_STATUS = 0
BEGIN
set @cc_exec = 'insert into #temp(typecode) select typecode from '+@@CC_TABLENAME
exec(@cc_exec)
set @cc_table = 'update #temp set table_name='''+@@CC_TABLENAME+''' where table_name is null'
exec(@cc_table)
FETCH NEXT FROM @CSR_TL INTO @@CC_TABLENAME
END
--drop table #temp
CLOSE @CSR_TL
DEALLOCATE @CSR_TL