我有一个动态SQL,它返回未知的列数:
动态SQL的代码为:
DECLARE @RawTable TABLE
(
ID INT NOT NULL
, CarDealer VARCHAR(100)
, id_CarDealer INT
, CityTo VARCHAR(100) NULL
, CityFrom VARCHAR(100) NULL
, Price NUMERIC(18,3) NULL
, CarCount INT
)
INSERT INTO @RawTable
(
ID,
CarDealer,
id_CarDealer,
CityTo,
CityFrom,
Price,
CarCount
)
VALUES
( 1, 'TransLeader', 1, 'New York', 'Los Angeles', 5000, 10)
, ( 2, 'AutoTrader', 2, 'New York', 'Los Angeles', 6000, 11)
, ( 3, 'AO', 3, 'New York', 'Paris', 7000, 20)
, ( 4, 'CarSale', 4, 'Paris', 'London', 4000, 50)
和代码:
Declare @SQL varchar(max) = Stuff((Select Distinct ','+QuoteName(CarDealer+' Price')
+','+QuoteName(CarDealer+' Count')
From RawTable
Order By 1
For XML Path('')
),1,1,'')
Select @SQL = '
Select CityTo
,CityFrom
,[TotalCarDealerPrice]
,[TotalCarDealerCount]
,'+@SQL+'
From (
Select A.CityTo
,A.CityFrom
,B.*
From RawTable A
Cross Apply ( values (''TotalCarDealerPrice'',Price)
,(''TotalCarDealerCount'',CarCount)
,(CarDealer+'' Price'' ,Price)
,(CarDealer+'' Count'' ,CarCount)
) B (Item,Value)
) A
Pivot (sum([Value]) For [Item] in ([TotalCarDealerPrice],[TotalCarDealerCount],' + @SQL + ') ) p'
--Print @SQL
Exec(@SQL);
前四行将始终相同:
CityTo, CityFrom, TotalCarDealerPrice, TotalCarDealerCount
然后下一列将不同:
TransLeader Price, TransLeader Count, AutoTrader Price, AutoTrader Count, AO
Price, AO Count, CarSale Price, CarSale Count ...
动态SQL的最大列数为59:
是否可以在下表中插入数据:
DECLARE @TR TABLE
(
CityTo VARCHAR(255), CityFrom VARCHAR(255), TotalCarDealerPrice VARCHAR(255),
TotalCarDealerCount VARCHAR(255),
Dealer1 DECIMAL(18,3) NULL, DealerPrice1 INT, Dealer2 DECIMAL(18,3) NULL,
DealerPrice2 INT, Dealer3 DECIMAL(18,3) NULL, DealerPrice3 INT, Dealer4
DECIMAL(18,3) NULL, DealerPrice4 INT, Dealer5 DECIMAL(18,3) NULL,
DealerPrice5 INT,
Dealer6 DECIMAL(18,3) NULL, DealerPrice6 INT, Dealer7 DECIMAL(18,3) NULL,
DealerPrice7 INT, Dealer8 DECIMAL(18,3) NULL, DealerPrice8 INT, Dealer9
DECIMAL(18,3) NULL, DealerPrice9 INT, Dealer10 DECIMAL(18,3) NULL,
DealerPrice10 INT,
Dealer11 DECIMAL(18,3) NULL, DealerPrice11 INT, Dealer12 DECIMAL(18,3) NULL,
DealerPrice12 INT, Dealer13 DECIMAL(18,3) NULL, DealerPrice13 INT, Dealer14
DECIMAL(18,3) NULL, DealerPrice14 INT, Dealer15 DECIMAL(18,3) NULL,
DealerPrice15 INT,
Dealer16 DECIMAL(18,3) NULL, DealerPrice16 INT, Dealer17 DECIMAL(18,3) NULL,
DealerPrice17 INT, Dealer18 DECIMAL(18,3) NULL, DealerPrice18 INT, Dealer19
DECIMAL(18,3) NULL, DealerPrice19 INT, Dealer20 DECIMAL(18,3) NULL,
DealerPrice20 INT,
Dealer21 DECIMAL(18,3) NULL, DealerPrice21 INT, Dealer22 DECIMAL(18,3) NULL,
DealerPrice22 INT, Dealer23 DECIMAL(18,3) NULL, DealerPrice23 INT, Dealer24
DECIMAL(18,3) NULL, DealerPrice24 INT, Dealer25 DECIMAL(18,3) NULL,
DealerPrice25 INT,
Dealer26 DECIMAL(18,3) NULL, DealerPrice26 INT, Dealer27 DECIMAL(18,3) NULL,
DealerPrice27 INT, Dealer28 DECIMAL(18,3) NULL, DealerPrice28 INT, Dealer29
DECIMAL(18,3) NULL, DealerPrice29 INT, Dealer30 DECIMAL(18,3) NULL,
DealerPrice30 INT,
Dealer31 DECIMAL(18,3) NULL, DealerPrice31 INT, Dealer32 DECIMAL(18,3) NULL,
DealerPrice32 INT, Dealer33 DECIMAL(18,3) NULL, DealerPrice33 INT, Dealer34
DECIMAL(18,3) NULL, DealerPrice34 INT, Dealer35 DECIMAL(18,3) NULL,
DealerPrice35 INT,
Dealer36 DECIMAL(18,3) NULL, DealerPrice36 INT, Dealer37 DECIMAL(18,3) NULL,
DealerPrice37 INT, Dealer38 DECIMAL(18,3) NULL, DealerPrice38 INT, Dealer39
DECIMAL(18,3) NULL, DealerPrice39 INT, Dealer40 DECIMAL(18,3) NULL,
DealerPrice40 INT,
Dealer41 DECIMAL(18,3) NULL, DealerPrice41 INT, Dealer42 DECIMAL(18,3) NULL,
DealerPrice42 INT, Dealer43 DECIMAL(18,3) NULL, DealerPrice43 INT, Dealer44
DECIMAL(18,3) NULL, DealerPrice44 INT, Dealer45 DECIMAL(18,3) NULL,
DealerPrice45 INT,
Dealer46 DECIMAL(18,3) NULL, DealerPrice46 INT, Dealer47 DECIMAL(18,3) NULL,
DealerPrice47 INT, Dealer48 DECIMAL(18,3) NULL, DealerPrice48 INT, Dealer49
DECIMAL(18,3) NULL, DealerPrice49 INT, Dealer50 DECIMAL(18,3) NULL,
DealerPrice50 INT,
Dealer51 DECIMAL(18,3) NULL, DealerPrice51 INT, Dealer52 DECIMAL(18,3) NULL,
DealerPrice52 INT, Dealer53 DECIMAL(18,3) NULL, DealerPrice53 INT, Dealer54
DECIMAL(18,3) NULL, DealerPrice54 INT, Dealer55 DECIMAL(18,3) NULL,
DealerPrice55 INT
)
如果我将动态查询的数据插入@TR表,并且动态查询的列数不是63,而是30,那么我会看到错误:
列名或提供的值数与表不匹配 定义。
如何在不知道结果动态SQL中会有多少列的情况下编写有效的插入语句?