我的TSQL存储过程中有这段代码:
CREATE Procedure [dbo].[priceVarianceTest]
@Unit varchar(25),
@BegDate datetime,
@EndDate datetime
AS
DECLARE @Week1End datetime = DATEADD(Day, 6, @BegDate);
DECLARE @Week2Begin datetime = DATEADD(Day, 7, @BegDate);
DECLARE @MemberNo VarChar(25) = SELECT MEMBERNO FROM INVOICEDETAIL WHERE UNIT = @Unit;
DECLARE @MemberItemCode VarChar(50) = SELECT ITEMCODE FROM INVOICEDETAIL WHERE UNIT = @Unit;
......但它不会执行,说:
Msg 156, Level 15, State 1, Procedure priceVarianceTest, Line 9
Incorrect syntax near the keyword 'SELECT'.
Msg 156, Level 15, State 1, Procedure priceVarianceTest, Line 10
Incorrect syntax near the keyword 'SELECT'.
我想插入这些val,如下:
INSERT INTO #temp1 (Unit, MemberNo, MemberItemCode) VALUES (@Unit, MemberNo, MemberItemCode);
...所以我也尝试了这个:
INSERT INTO #temp1 (Unit, MemberNo, MemberItemCode) VALUES (@Unit, SELECT MEMBERNO FROM INVOICEDETAIL WHERE UNIT = @Unit, MemberItemCode);
......但这并不比我以前的尝试更受欢迎。
然后,我如何获得这些值。我的语法或方法有问题吗?
答案 0 :(得分:2)
尝试SELECT @variable = column
:
DECLARE @MemberNo VarChar(25);
SELECT @MemberNo = MEMBERNO FROM INVOICEDETAIL WHERE UNIT = @Unit;
DECLARE @MemberItemCode VarChar(50);
SELECT @MemberItemCode = ITEMCODE FROM INVOICEDETAIL WHERE UNIT = @Unit;
或更简单:
DECLARE @MemberNo VarChar(25);
DECLARE @MemberItemCode VarChar(50);
SELECT @MemberNo = MEMBERNO,
@MemberItemCode = ITEMCODE
FROM INVOICEDETAIL WHERE UNIT = @Unit;