如何将前1列值选择为变量?

时间:2016-08-08 18:29:22

标签: sql sql-server stored-procedures

这是我的表定义,其中存储了安装信息:

CREATE TABLE [dbo].[InstallInfo](
  [ID] [uniqueidentifier] NOT NULL,
  [Module] [int] NOT NULL,
  [Version] [nvarchar](50) NOT NULL,
  [InstallDate] [datetime] NOT NULL,
  CONSTRAINT [PK_InstallInfo] PRIMARY KEY CLUSTERED 
  ([ID] ASC)
  WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

我想要做的是根据最新日期获取模块的前1行(安装版本):

ALTER PROCEDURE [dbo].[InstallInfo_GetLatest] 
-- Add the parameters for the stored procedure here
@Module int, 
@Version nvarchar(50) out
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

set @Version = ''

-- Insert statements for procedure here
SELECT TOP 1 @Version=[Version]
from InstallInfo
where [Module] = @Module
order by InstallDate

END

但是,当我运行存储过程时,我收到此错误:

Msg 8114, Level 16, State 5, Procedure InstallInfo_GetLatest, Line 0
Error converting data type nvarchar to int.

以下是我执行存储过程的方法:

declare @Module int
set @Module = 1
declare @versionOut nvarchar(50)
exec InstallInfo_GetLatest Module, @versionOut output
select @versionOut as 'v OUT'

发现它:忘记@符号

exec InstallInfo_GetLatest @Module, @versionOut output

谢谢大家!

1 个答案:

答案 0 :(得分:0)

以下是经过多次评论和反馈的答案:

  exec InstallInfo_GetLatest @Module, @versionOut output