更新时间:2023-12-01 14:21:16
您要搜索"t-SQL动态SQL".
You want to google "t-SQL dynamic SQL".
您的问题可以通过使用数据透视表解决sql
搜索SQL Server数据透视查询
Your problem can be solved using pivot table in sql
search for sql server pivot query
这是创建动态列的方法.
因为我没有您的表结构,所以我无法帮助您如何填充临时表.
This is how you create dynamic columns.
Because I don''t have your table structure I cannot help you how to fill the temp table.
IF OBJECT_ID( 'tempdb..#temp') IS NOT NULL
BEGIN
DROP TABLE #temp
END
CREATE TABLE #temp
(
[CVRelID] INT,
[CustomerID] INT,
[VendorID] INT,
[TransactionID] INT
)
DECLARE @script VARCHAR(8000)
SET @script = 'ALTER TABLE #temp ADD '
SELECT @script = @script + QUOTENAME(SDName) + ' VARCHAR(100),'
FROM tblSupportDocuments
WHERE SDIsActive = 1
SET @script = SUBSTRING( @script, 1, LEN(@script) - 1)-- remove last ","
PRINT @script
EXEC (@script)