用 sql 带参数的查询过程 报表字段名不来
我用带参数查询的存储过程,如果参数为空就查全部记录:
create proc [dbo].[scanstore04](@工程号 varchar (20))
as
begin
declare @sql varchar (4000)
set @sql=N'SELECT barcode1,cast(sl as int)as sl, COUNT(stationName) AS zysl, stationName, zc, bzl, cl, ys, cc, gch, tdh, bh
FROM dbo.ScanData
WHERE 1=1
GROUP BY barcode1,stationName, sl, zc, bzl, cl, ys, cc, gch, tdh, bh '
if @工程号 <>''
set @sql=@sql+' and gch='''+@工程号+''''
exec(@sql)
end
用Report Builder 2.0 做报表时,所选字段不出来,如果,把COUNT(stationNAME) GROUP BY ……去掉后,就能出字段。
------解决方案--------------------try this,
create proc [dbo].[scanstore04]
(@工程号 varchar (20))
as
begin
declare @sql varchar(4000)
set @sql=N'SELECT barcode1,cast(sl as int)as sl, COUNT(stationName) AS zysl, stationName, zc, bzl, cl, ys, cc, gch, tdh, bh
FROM dbo.ScanData
WHERE 1=1 '
if @工程号<>''
set @sql=@sql+' and gch='''+@工程号+''' '
set @sql=@sql+' GROUP BY barcode1,stationName, sl, zc, bzl, cl, ys, cc, gch, tdh, bh '
exec(@sql)
end