日期:2014-05-17  浏览次数:20664 次

存储过程执行
我的存储过程如下 :
-- =============================================
ALTER PROCEDURE [dbo].[proc_tbBDdataAll_Print_tj03]
-- Add the parameters for the stored procedure here
@Begindate datetime =null,
@Enddate datetime =null,
@station nvarchar(25) =null,
@Chuanming nvarchar(25) =null,
@Hangci nvarchar(25)=null

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;


declare @strWhere nvarchar(4000)
declare @strSql nvarchar (4000)

set @strWhere = ' where ( dbo.tbBDData_All.KSRQI between '+'''' + convert(varchar(20), @Begindate ) +'''' + ' and ' + '''' + convert(varchar(20), @Enddate ) +'''' 
if (@station <> '')
set @strWhere = @strWhere + ' and dbo.tbJLD.Name = '+'''' + @station +''')'
if (@Chuanming <>'')
set @strWhere = @strWhere + ' and dbo.tbCHGJ.Name ='+'''' + @Chuanming +''''
if (@Hangci <>'')
set @strWhere = @strWhere + ' and dbo.tbReWu.HangCi ='+'''' + @Hangci +''')'
print @strWhere

select @strSql ='SELECT dbo.tbCHGJ.Name + dbo.tbReWu.HangCi AS 船号, dbo.tbJLD.Name AS 计量点, dbo.tbCheHao.Name AS 车号, COUNT(dbo.tbCheHao.Name) AS 次数, SUM(dbo.tbBDData_All .NET) AS 总重
FROM dbo.tbReWu INNER JOIN dbo.tbCHGJ ON dbo.tbReWu.CHGJ = dbo.tbCHGJ.CCID INNER JOIN
dbo.tbBDData_All ON dbo.tbReWu.RWHao = dbo.tbBDData_All.RWHao INNER JOIN
dbo.tbJLD ON dbo.tbBDData_All.JLID = dbo.tbJLD.CCID INNER JOIN
dbo.tbCheHao ON dbo.tbBDData_All.CHID = dbo.tbCheHao.CCID
GROUP BY dbo.tbCheHao.Name, dbo.tbCHGJ.Name, dbo.tbJLD.Name, dbo.tbReWu.HangCi '
  
   
  print @strSql
  exec @strSql 

但是执行时却提示
消息 203,级别 16,状态 2,过程 proc_tbBDdataAll_Print_tj03,第 43 行
名称 'SELECT dbo.tbCHGJ.Name + dbo.tbReWu.HangCi AS 船号, dbo.tbJLD.Name AS 计量点, dbo.tbCheHao.Name AS 车号, COUNT(dbo.tbCheHao.Name) AS 次数, SUM(dbo.tbBDData_All .NET) AS 总重
FROM dbo.tbReWu INNER JOIN dbo.tbCHGJ ON dbo.tbReWu.CHGJ = dbo.tbCHGJ.CCID INNER JOIN
dbo.tbBDData_All ON dbo.tbReWu.RWHao = dbo.tbBDData_All.RWHao INNER JOIN
dbo.tbJLD ON dbo.tbBDData_All.JLID = dbo.tbJLD.CCID INNER JOIN
dbo.tbCheHao ON dbo.tbBDData_All.CHID = dbo.tbCheHao.CCID
GROUP BY dbo.tbCheHao.Name, dbo.tbCHGJ.Name, dbo.tbJLD.Name, dbo.tbReWu.HangCi ' 不是有效的标识符。


------解决方案--------------------
SQL code
ALTER PROCEDURE [dbo].[proc_tbBDdataAll_Print_tj03]
-- Add the parameters for the stored procedure here
@Begindate datetime =null,
@Enddate datetime =null,
@station nvarchar(25) =null,
@Chuanming nvarchar(25) =null,
@Hangci nvarchar(25)=null

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;


declare @strWhere nvarchar(4000)
declare @strSql nvarchar (4000)

set @strWhere = ' where ( dbo.tbBDData_All.KSRQI between '+'''' + convert(varchar(20), @Begindate ) +'''' + ' and ' + '''' + convert(varchar(20), @Enddate ) +''''  
if (@station <> '')
set @strWhere = @strWhere + ' and dbo.tbJLD.Name = '+'''' + @station +''')'
if (@Chuanming <>'')
set @strWhere = @strWhere + ' and dbo.tbCHGJ.Name ='+'''' + @Chuanming +''''
if (@Hangci <>'')
set @strWhere = @strWhere + ' and dbo.tbReWu.HangCi ='+'''' + @Hangci +''')'
print @strWhere

select @strSql ='SELECT dbo.tbCHGJ.Name + dbo.tbReWu.HangCi AS 船号, dbo.tbJLD.Name AS 计量点, dbo.tbCheHao.Name AS 车号, COUNT(dbo.tbCheHao.Name) AS 次数, SUM(dbo.tbBDData_All .NET) AS 总重
FROM dbo.tbReWu INNER JOIN dbo.tbCHGJ ON dbo.tbReWu.CHGJ = dbo.tbCHGJ.CCID INNER JOIN
dbo.tbBDData_All ON dbo.tbReWu.RWHao = dbo.tbBDData_All.RWHao INNER JOIN
dbo.tbJLD ON dbo.tbBDData_All.JLID = dbo.tbJLD.CCID INNE