0

如何在 select 中使用变量,如下所示:

 SELECT @V_BALANCE_PRIN=E.REC_PRINCIPAL_OS AS BALANCE_PRINCIPAL 
 FROM CR_TERMINATION_DTL E
4

2 回答 2

1

You do need 2 queries to do this I believe, you can declare and assign the variable at the same time, but not select it. If you do a select @variable = (query) you don't get the result displayed, you just assign the value.

So I think the best you can do (to use the fewest lines) is:

DECLARE @V_BALANCE_PRIN NVARCHAR(10) = (SELECT E.REC_PRINCIPAL_OS FROM CR_TERMINATION_DTL E)
SELECT @V_BALANCE_PRIN AS BALANCE_PRINCIPAL

Obviously your data type will have to match, and you will also have to make sure that the subquery only returns a single value as pointed out by Justin, otherwise it will error.

于 2012-12-15T18:43:48.567 回答
0

两个查询怎么样:

SELECT @V_BALANCE_PRIN=E.REC_PRINCIPAL_OS FROM CR_TERMINATION_DTL E
SELECT @V_BALANCE_PRIN AS BALANCE_PRINCIPAL
于 2012-12-15T07:32:21.453 回答