如何从表中分配日期变量值?

时间:2016-04-15 12:32:17

标签: sql tsql date variable-assignment

以下代码:

declare @tradeDate date = select tradeDate from tSystemStatus

生成错误:

Incorrect syntax near the keyword 'select'.

表格结构:

CREATE TABLE [dbo].[tSystemStatus](
    ....
    [TradeDate] [date] NOT NULL,
    ....

我错了什么?

2 个答案:

答案 0 :(得分:2)

试试这个

declare @tradeDate date = (select tradeDate from tSystemStatus)

但是如果select查询返回的值多一个,那么它会抛出错误(你可以在这里使用top 1)

declare @tradeDate date = (select top 1 tradeDate from tSystemStatus)

或者

declare @tradeDate date 
select @tradeDate = tradeDate from tSystemStatus

答案 1 :(得分:1)

试试这个

declare @tradeDate date;
select @tradeDate=tradeDate from tSystemStatus