日期:2014-05-18  浏览次数:20544 次

一大清早,问个存储过程参数无效的问题?
我打算将查询出来的值赋给变量@oldtime,可是一直都得不到值,用单步调试也也找到原因.
大家帮忙看一下原因啊,存储过程如下:
CREATE PROCEDURE Sp_Uptotalday
-- Add the parameters for the stored procedure here
@sssj varchar(8),
@time varchar(8),
@rq varchar(10),
@kqkh varchar(10)
AS
BEGIN
  DECLARE @sql varchar(100)
  DECLARE @oldtime varchar(50)
  set @sql='select '+@oldtime+'='+@sssj+' from totalday where rq='''+@rq+''' and kqkh='''+@kqkh+''''
  exec (@sql)
END

------解决方案--------------------
不知道你的oldtime是干啥用的, 下面这个存储过程很顺利地取出了表中的值.

SQL code
create table totalday
(swsb varchar(50), swxb varchar(50), xwsb varchar(50), xwxb varchar(50), wssb varchar(50), 
 wsxb varchar(50), kqkh varchar(50), rq varchar(50))

insert totalday
select '1', '2', '3', '4', '5', '6', '001', '2007-10-20'
GO

create Procedure Sp_Uptotalday
@sssj varchar(8),
@time varchar(50) output,
@rq varchar(10),
@kqkh varchar(10)
AS
Begin
    Declare @sql nvarchar(100)
    declare @oldtime varchar(50)
    set @sql = 'SELECT @oldtime = ' + @sssj + ' from totalday where rq = ''' + @rq + ''' and kqkh=''' + @kqkh + ''''
    exec sp_executesql @sql, N'@oldtime varchar(50) OUTPUT', @oldtime OUTPUT
    set @time = @oldtime
END

GO

declare @time varchar(50)
exec Sp_Uptotalday 'swsb', @time OUTPUT, '2007-10-20', '001'
select @time

GO

drop table totalday
drop procedure Sp_Uptotalday