如何动态的插入数据select * into 表名from-CSDN论坛

2024-12-16 03:11:34
推荐回答(1个)
回答1:

DECLARE @Airline NVARCHAR(10)
DECLARE @sql NVARCHAR(1000)

DECLARE Split_Airline CURSOR FOR
SELECT count(Airline),Airline FROM FilterTable GROUP BY Airline
ORDER BY Airline

OPEN Split_Airline

FETCH NEXT FROM Split_Airline
INTO @Airline

WHILE @@FETCH_STATUS = 0
BEGIN
--
IF OBJECTPROPERTY (object_id('@Airline'),'ISTABLE') = 1

BEGIN

--SET sql = 'SELECT field INTO V_A FROM' || tablename;
SET @sql ='SELECT * INTO '+ @Airline + ' FROM FilterTable where Airline=@Airline'
select @sql

--PREPARE s1 FROM @sql
--EXECUTE sql(@sql)

--exec sql (SELECT * INTO ''+ @Airline + '' FROM FilterTable where Airline=@Airline)

exec s1
END

FETCH NEXT FROM Split_Airline
INTO @Airline

END
CLOSE Split_Airline
DEALLOCATE Split_Airline

1,如何动态的判断已经建立的表存在
IF OBJECTPROPERTY (object_id('@Airline'),'ISTABLE') = 1
@Airline是我要动态建立的表名,这样写对吗?
2,如何select * INTO @Airline from FilterTable where Airline=@Airline
@Airline是从游标中动态获得的,要作为表名插入。