使用存储过程中的select语句为参数赋值

时间:2014-06-16 19:32:42

标签: sql stored-procedures parameters

Go
Alter procedure usp_ReportUpdates

AS
 Declare  @currentweek as varchar(255), @previousweek varchar(255)

 set @currentweek SELECT distinct fiscalweekqtr from fiscaltime where fiscalweekago=0;
  set @previousweek SELECT distinct fiscalweekqtr   from fiscaltime where fiscalweekago=1;

此代码在选择工作时出现语法错误。我如何将选择查询的单数结果分配给@currentweek和@preweweek?

2 个答案:

答案 0 :(得分:0)

试试这个,

SELECT @currentweek = fiscalweekqtr from fiscaltime where fiscalweekago=0; 
SELECT @previousweek = fiscalweekqtr from fiscaltime where fiscalweekago=1;

答案 1 :(得分:0)

这看起来像SQL Server语法。如果是这样,我认为这是你可能正在寻找的:

Alter procedure usp_ReportUpdates
AS
BEGIN
    Declare @currentweek as varchar(255),
            @previousweek varchar(255);

    SELECT @currentweek = fiscalweekqtr from fiscaltime where fiscalweekago = 0;
    SELECT @previousweek = fiscalweekqtr from fiscaltime where fiscalweekago = 1;
END;

您似乎错过了@previousweek查询的条件。应该酌情填写。

您的特定语法错误是因为select语句是子查询,因此应该在括号中。但是,您不需要此语句的子查询,如上所示。