这是我首先想到的。
示例
DECLARE @ID INT
SET @ID = 0
SELECT
@ID = ID,
TOTAL = @ID + 5
FROM
Bank
发生此错误:
为变量赋值的SELECT语句不能与数据检索操作结合使用。
还有其他方法吗?
EDIT2:
我试图实现这个目标:
CREATE TABLE #tempTab (id int)
INSERT INTO #tempTab (id) VALUES (5), (10)
DECLARE @SampleA INT
DECLARE @SampleB INT
DECLARE @SampleC INT
SELECT
@SampleA = 5*ID, --Row1 = 25, Row2 = 50
@SampleB = @SampleA, - 15 --Row1 = 10, Row2 = 35
@SampleC = @SampleA * @SampleB --Row1 = 250, Row2 = 1750
FROM
#tempTab
我需要的输出是
- 25 | 10 | 250 -row1
- 50 | 68 | 1750 -row2
答案 0 :(得分:0)
您需要使用Derived Tables:
SELECT ID, SampleA, SampleB, ( SampleA * SampleB ) AS SampleC
FROM(
SELECT ID, SampleA , ( SampleA - 15 ) AS SampleB
FROM(
SELECT ID, 5 * ID AS SampleA
FROM #tempTab
) AS A
) AS B
输出:
ID SampleA SampleB SampleC
----------- ----------- ----------- -----------
5 25 10 250
10 50 35 1750
答案 1 :(得分:0)
这是您问题的最简单答案。
CREATE TABLE #tempTab (id int)
INSERT INTO #tempTab (id) values (5)
INSERT INTO #tempTab (id) values (10)
SELECT (5*ID), --Row1 = 25, Row2 = 50
5*ID - 15, --Row1 = 10, Row2 = 35
(5*ID) * (5*ID - 15) --Row1 = 250, Row2 = 1750
FROM #tempTab
SELECT * FROM #tempTab
DROP TABLE #tempTab