我正在使用sql存储过程编写基于参数的报告。问题是有几个“可选”参数,用户可以选择一个值,或者完全忽略该参数。我需要在asp.net报告中使用存储过程作为数据集。我想到的唯一方法就是从结果中创建一个表。但我很无能。
如何将其转换为'create table'存储过程? 非常感谢任何帮助。
辛迪
这是我到目前为止 -
ALTER PROCEDURE [dbo].[rspSCL]
-- Add the parameters for the stored procedure here
(@year as int,
@RRID as int,
@State as nchar(2),
@Sub as varchar(75),
@app as int,
@sprayed as int,
@subcontracted as int,
@thirdapp as bit)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
DECLARE @SQL as varchar(4000)
DECLARE @SUBQ AS varchar(400)
--what fields need to be seen
SET @SQL = 'SELECT C.CRID, C.DOT, C.RR, C.Pref, C.MP, C.Division, C.Subdivision,C.City, C.Street,
C.State, C.County, C.RestrictedCounty, C.Remarks, C.SpecialInstructions, C.Route, C.ThirdAppRequired,
C.MainTrks, C.OtherTrks, C.OnSpur, C.MaxSpeed, C.SubContracted, C.FenceEncroachment, C.Lat, C.Long,
C.PropertyType, C.WarningDevice,C.Surface, C.ROWNE, C.ROWNW, C.ROWSE, C.ROWSW, C.ROWWidth, C.ExtNE,
C.ExtNW, C.ExtSE, C.ExtSW, C.TempActive, A.Spray, A.ContractYear'
SET @SUBQ =
CASE WHEN @app = 1 THEN
'FROM Crossings LEFT OUTER JOIN
(select A.CRID, A.spray FROM ApplicationData A
WHERE A.ContractYear = ' + CAST( @year AS varchar(4)) + ') as APPS
ON Crossings.CRID = APPS.CRID'
WHEN @app = 2 THEN
'FROM Crossings inner join
(SELECT crid, C,OUNT(APPID)as count
from ApplicationData
where ContractYear = ' + CAST( @year AS varchar(4)) + '
group by CRID) as APPS
ON Crossings.CRID = APPS.CRID
WHERE Crossings.RR = '+ CAST( @RRID AS varchar(4)) + '
and crossings.state = '+ @state + '
and count = 1'
when @app = 3 THEN
'FROM Crossings inner join
(SELECT crid, COUNT(APPID)as count
from ApplicationData
where ContractYear = ' + CAST( @year AS varchar(4)) + '
group by CRID) as APPS
ON Crossings.CRID = APPS.CRID
WHERE Crossings.RR = '+ CAST( @RRID AS varchar(4)) + '
and crossings.state = '+ @state + '
and count = 2'
ELSE ''
END
--add the FROM to the query
SET @SQL = @SQL + @SUBQ
--add the subdivision to the where statement
If @Sub != 'ALL'
SET @SQL = @SQL + 'AND (C.Subdivision = ' + @Sub + ')'
ELSE
SET @SQL = @SQL
--add sprayed to where
DECLARE @S as varchar(100)
SET @S =
CASE WHEN @sprayed = 1 THEN
'AND (A.sprayed = 1)'
WHEN @sprayed = 0 THEN
'AND (A.sprayed = 0)'
ELSE
''
END
SET @SQL = @SQL + @S
--add subcontracted to where
DECLARE @C as varchar (100)
SET @C =
CASE WHEN @subcontracted = 0 THEN
'AND (C.subcontracted = 0)'
WHEN @subcontracted = 1 THEN
'AND (C.subcontracted = 1)'
ELSE
''
END
SET @SQL = @SQL + @C
IF @thirdapp = 1
SET @SQL = @SQL + 'AND (C.ThirdAppRequired = 1)'
ELSE
SET @SQL = @SQL
END