我有一个简单的问题,我相信解决方案已经浮出水面,但仍然无法解决。
我有一个存储过程,希望用户进行两次输入:
@userid int,
@subtotal int
我的存储过程还需要一个表中的值,该值可以从以下查询中获得:
SELECT discountid FROM historical WHERE userid = @userid
因此,我只需要将从查询中获得的值(即一个int值)保存在整个存储过程中将要使用的变量中,这样我就可以调用它并在以后使用它。
问题是我不知道如何声明一个变量来保存此查询中的值,以便以后使用。
我搜索后发现:
@userid int,
@subtotal int,
@discountid int = (SELECT discountid from historical where userid = @userid)
和...
@userid int,
@subtotal int,
SELECT @discountid = discountid from historical
where userid = @userid;
...但是它们似乎都不起作用。我当前使用的是SQL Server2008。希望有人能对我有所帮助,对不起我的英语,因为我不是母语人士,在此先感谢您!
首先,您必须DECLARE
为此变量,然后分配给它。另外,请确保您的查询仅返回一行,否则将无法将N行分配给一个INT变量,从而导致错误。为了防止这种情况,我将其添加TOP 1
到select语句中。
CREATE PROCEDURE YOUR NAME
@userid int,
@subtotal int
AS
Declare @discountid int;
SELECT TOP 1 @discountid = discountid from historicalwhere userid = @userid;
.....
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句