【问题标题】:Can I use a variable as a column in a select statement?我可以在 select 语句中使用变量作为列吗?
【发布时间】:2013-02-20 19:12:46
【问题描述】:

如果我在 ADO.net 中使用 ExecuteScalar,我想从我的存储过程中选择一个变量,以便我可以获取变量值。

我的存储过程是这样的

    CREATE PROCEDURE dbo.SPListGetID
    (
      @category varchar(100)
    )
    AS
      declare @maxListId int
      set @maxListId=(select max(MaterialId) from tblMaterialLists 
                      where category =@category and mode='1')
      set @maxListId=@maxListId+1;
      select @maxListId
      /* SET NOCOUNT ON */
      RETURN

此处select @maxListId 是不允许的。我该怎么做才能做到这一点?

【问题讨论】:

    标签: asp.net sql sql-server-2008 tsql stored-procedures


    【解决方案1】:

    尝试SET @maxListId=@maxListId+1; 而不是@maxListId=@maxListId+1;

    【讨论】:

      【解决方案2】:

      你需要稍微改变一下语法

      select @maxListId= max(MaterialId) where category =@category and mode='1'
      

      【讨论】:

        【解决方案3】:

        试试

        RETURN @maxListId 而不是

        select @maxListId
              /* SET NOCOUNT ON */
              RETURN
        

        【讨论】:

          【解决方案4】:
          CREATE PROCEDURE dbo.SPListGetID
              (
                @category varchar(100)
              )
              AS
          begin
                declare @maxListId int
                select  @maxListId= max(MaterialId) from tblMaterialLists 
                                where category =@category and mode='1'
                set @maxListId=@maxListId+1;
          
               end
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-05-24
            • 2010-11-05
            • 1970-01-01
            • 1970-01-01
            • 2014-12-27
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多