我是SSRS的初学者,也许这听起来很容易,但我想知道如何根据我的数据集中返回的行数动态创建表(选择查询)。
如果我有5行,则应该在5个不同的表中显示。
实施例: 选择查询:
ID | Name | Age
01 | Mike | 10
02 | Mary | 12
然后在我的ssrs中:
ID | 01
Name | Mike
Age | 10
ID | 02
Name | Mary
Age | 12
答案 0 :(得分:1)
您可以使用SQL
CURSOR
DECLARE @table_name as nvarchar(100)
DECLARE @count int = 0
DECLARE @id, @name as nvarchar(255)
DECLARE @age as int
-- declare cursor
DECLARE tbl_cursor CURSOR FOR
SELECT [id], [name], [age] FROM [SourceTable]
OPEN tbl_cursor
FETCH NEXT FROM tbl_cursor INTO @id, @name, @age
WHILE @@FETCH_STATUS = 0 -- ok
BEGIN
-- create table with temp name
CREATE TABLE temp_table (
[id] nvarchar(255),
[name] nvarchar(255),
[age] int
);
INSERT INTO temp_table ([id], [name], [age]) VALUES (@id, @name, @age)
-- rename temp table
SET @count = @count + 1
SET @table_name = 'tblTable' + CAST(@count as varchar)
exec sp_rename temp_table, @table_name
FETCH NEXT FROM tbl_cursor INTO @id, @name, @age
END
-- close cursor
CLOSE tbl_cursor
DEALLOCATE tbl_cursor
如果SELECT [id], [name], [age] FROM [SourceTable]
返回五行,代码将创建五个表[tblTable1..5]
答案 1 :(得分:0)