我在Report Builder 3.0中创建了一个包含3个参数(var1,var2和var3)的报表。该报告使用动态sql,它使用游标从多个位置选择值。有3个数据集," DataSet1"," DataSet2"和" DataSet3"。 " DataSet1"是生成报告的主要集合。 " DataSet2"和" DataSet3"用于获取@ var2和@ var3参数的值。
var1参数是一个允许空值的位,当null将使用" DataSet1"中的以下内容显示true和false值时:
CASE
WHEN @var1 IS NULL AND (t.var1Field = 1 OR t.var1Field = 0) THEN 1
WHEN @var1 = 1 AND t.var1Field = 1 THEN 1
WHEN @var1 = 0 AND t.var1Field = 0 THEN 1
END = 1
var2参数允许"全部"值,或要选择的单个var2。 @ var2的默认值为null,它在UNION ALL中用作" All"数据中的值,可用值来自" DataSet3"使用以下代码:
DECLARE @tmpSqlRefTable AS TABLE
(
sqlRef1 varchar(50),
sqlRef2 varchar(50),
SQLServer varchar(50),
DB varchar(50)
)
INSERT INTO @tmpSqlRefTable
SELECT l.sqlRef1,
l.sqlRef2,
l.SQLServer,
l.DB
FROM SqlRefTable l
WHERE l.sqlRef2 NOT IN ('Location1', 'Location2')
CREATE TABLE #tmpTable1
(
Field1 varchar(100)
)
DECLARE sqlCursor CURSOR FOR
SELECT l.sqlRef1,
l.sqlRef2,
l.SQLServer,
l.DB
FROM @tmpSqlRefTable l
OPEN sqlCursor
DECLARE @sqlRef1 varchar(50),
@sqlRef2 varchar(50),
@srv varchar(50),
@db varchar(50),
@sqlStr varchar(max)
FETCH NEXT FROM sqlCursor INTO @sqlRef1, @sqlRef2, @srv, @db
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sqlStr =
'
SELECT DISTINCT LTRIM(trf.Field1) Field1
FROM [' + @srv + '].[' + @db + '].dbo.Table1 trf
WHERE trf.Field1 IS NOT NULL
'
IF DB_NAME() <> @db
BEGIN
SET @sqlStr = 'EXEC(''' + Replace(@sqlStr, '''', '''''') + ''') at ' + @srv;
END
INSERT INTO #tmpTable1
EXEC(@sqlStr)
FETCH NEXT FROM sqlCursor INTO @sqlRef1, @sqlRef2, @srv, @db
END
CLOSE sqlCursor
DEALLOCATE sqlCursor
SELECT NULL AS Field1, 'All' As Field1Descrip
UNION ALL
SELECT DISTINCT tt.Field1, tt.Field1 AS Field1Descrip
FROM #tmpTable1 tt
DROP TABLE #tmpTable1
var3参数是一个多值参数,允许使用&#34; Select All&#34;选择所有值,也可以通过选择var2进行过滤。可用值和默认值来自&#34; DataSet2&#34;如下所示:
DECLARE @tmpSqlRefTable AS TABLE
(
sqlRef1 varchar(50),
sqlRef2 varchar(50),
SQLServer varchar(50),
DB varchar(50)
)
INSERT INTO @tmpSqlRefTable
SELECT l.sqlRef1,
l.sqlRef2,
l.SQLServer,
l.DB
FROM SqlRefTable l
WHERE l.sqlRef2 NOT IN ('Location1', 'Location2')
CREATE TABLE #tmpTable2
(
Field1 varchar(50)
)
DECLARE sqlCursor CURSOR FOR
SELECT l.sqlRef1,
l.sqlRef2,
l.SQLServer,
l.DB
FROM @tmpSqlRefTable l
OPEN sqlCursor
DECLARE @sqlRef1 varchar(50),
@sqlRef2 varchar(50),
@srv varchar(50),
@db varchar(50),
@sqlStr varchar(max)
FETCH NEXT FROM sqlCursor INTO @sqlRef1, @sqlRef2, @srv, @db
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sqlStr =
'
DECLARE @var1 AS bit = ' + ISNULL(CAST(@var1 AS varchar(10)), 'NULL') + '
DECLARE @var2 AS varchar(100) = ' + ISNULL('''' + @var2 + '''', 'NULL') + '
SELECT DISTINCT t.Field1
FROM [' + @srv + '].[' + @db + '].dbo.Table1 t
INNER JOIN [' + @srv + '].[' + @db + '].dbo.Table2 trf ON t.Field1 = trf.Field1
WHERE
CASE
WHEN @var1 IS NULL AND (t.var1Field = 1 OR t.var1Field = 0) THEN 1
WHEN @var1 = 1 AND t.var1Field = 1 THEN 1
WHEN @var1 = 0 AND t.var1Field = 0 THEN 1
END = 1
AND (@var2 IS NULL OR trf.Table2 = @var2)
'
IF DB_NAME() <> @db
BEGIN
SET @sqlStr = 'EXEC(''' + Replace(@sqlStr, '''', '''''') + ''') at ' + @srv;
END
INSERT INTO #tmpTable2
EXEC(@sqlStr)
FETCH NEXT FROM sqlCursor INTO @sqlRef1, @sqlRef2, @srv, @db
END
CLOSE sqlCursor
DEALLOCATE sqlCursor
SELECT DISTINCT ttc.Field1
FROM #tmpTable2 ttc
ORDER BY Field1
DROP TABLE #tmpTable2
&#34; DataSet1&#34;使用上述参数生成报告如下:
DECLARE @tmpSqlRefTable AS TABLE
(
sqlRef1 varchar(50),
sqlRef2 varchar(50),
SQLServer varchar(50),
DB varchar(50)
)
INSERT INTO @tmpSqlRefTable
SELECT l.sqlRef1,
l.sqlRef2,
l.SQLServer,
l.DB
FROM SqlRefTable l
WHERE l.sqlRef2 NOT IN ('Location1', 'Location2')
CREATE TABLE #tmpResults
(
sqlRef1 varchar(50),
sqlRef2 varchar(50),
Field1 varchar(50),
Isvar1Field varchar(50),
Field2 varchar(100),
Field3 varchar(50),
Field4 int,
Field5 varchar(50),
Field6 float,
Field7 varchar(50)
)
DECLARE sqlCursor CURSOR FOR
SELECT l.sqlRef1,
l.sqlRef2,
l.SQLServer,
l.DB
FROM @tmpSqlRefTable l
OPEN sqlCursor
DECLARE @sqlRef1 varchar(50),
@sqlRef2 varchar(50),
@srv varchar(50),
@db varchar(50),
@sqlStr varchar(max)
FETCH NEXT FROM sqlCursor INTO @sqlRef1, @sqlRef2, @srv, @db
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sqlStr =
'
DECLARE @var2 AS varchar(100) = ' + ISNULL('''' + @var2 + '''', 'NULL') + '
DECLARE @var1 AS bit = ' + ISNULL(CAST(@var1 AS varchar(10)), 'NULL') + '
SELECT DISTINCT
''' + @sqlRef1 + ''' AS sqlRef1,
''' + @sqlRef2 + ''' AS sqlRef2,
t.Field1,
CASE
WHEN t.var1Field != 1 THEN ''No''
ELSE ''Yes''
END Isvar1Field,
trf.Field2,
tcl.Field3,
tcl.Field4,
c.Field5,
c.Field6,
t.Field7
FROM [' + @srv + '].[' + @db + '].dbo.Table1 t
INNER JOIN [' + @srv + '].[' + @db + '].dbo.Table2 tcl ON t.Field1 = tcl.Field1
INNER JOIN [' + @srv + '].[' + @db + '].dbo.Table3 trf ON t.Field1 = trf.Field1
LEFT JOIN [' + @srv + '].[' + @db + '].dbo.Table4 c ON tcl.Field3 = c.Field3
WHERE (@var2 IS NULL OR trf.var2Field = @var2)
AND
CASE
WHEN @var1 IS NULL AND (t.var1Field = 1 OR t.var1Field = 0) THEN 1
WHEN @var1 = 1 AND t.var1Field = 1 THEN 1
WHEN @var1 = 0 AND t.var1Field = 0 THEN 1
END = 1
'
IF DB_NAME() <> @db
BEGIN
SET @sqlStr = 'EXEC(''' + Replace(@sqlStr, '''', '''''') + ''') at ' + @srv;
END
INSERT INTO #tmpResults
EXEC(@sqlStr)
FETCH NEXT FROM sqlCursor INTO @sqlRef1, @sqlRef2, @srv, @db
END
CLOSE sqlCursor
DEALLOCATE sqlCursor
SELECT tmp.sqlRef2,
tmp.Field1,
tmp.Isvar1Field,
tmp.Field2,
tmp.Field3,
tmp.Field4,
tmp.Field5,
tmp.Field6,
tmp.Field7
FROM #tmpResults tmp
WHERE tmp.Field1 IN (@var3)
DROP TABLE #tmpResults
使用&#34;运行&#34;在报表生成器3.o中查看数据时命令,所有数据都按预期显示。
当我尝试通过网络上的报表服务查看数据时出现问题。相同的参数显示为默认值,但不显示任何报告(甚至不显示标题)。我希望有人可能遇到类似的问题,并知道解决这个问题的方法。