1. 程式人生 > >SQL從DB中取出多個欄位賦值給多個變數/一個欄位給單個變數賦值

SQL從DB中取出多個欄位賦值給多個變數/一個欄位給單個變數賦值

 

DECLARE @FIRST_BEGIN    DATETIME
DECLARE @FIRST_END        DATETIME
DECLARE @SECOND_BEGIN    DATETIME
DECLARE @SECOND_END        DATETIME

SELECT    @FIRST_BEGIN = startDate_no1, @FIRST_END = endDate_no1,
        @SECOND_BEGIN = startDate_no2, @SECOND_END = endDate_no2
FROM tempTbl WHERE year= 2008 AND season= 3

PRINT @FIRST_BEGIN
PRINT @FIRST_END
PRINT @SECOND_BEGIN
PRINT @SECOND_END

----------------------------------------------------------------------------------------

給單個變數賦值

方法一:

DECLARE @FIRST_BEGIN    DATETIME

SELECT    @FIRST_BEGIN = startDate_no1
FROM tempTbl WHERE year= 2008 AND season= 3

方法二:

DECLARE @FIRST_BEGIN    DATETIME

SET @FIRST_BEGIN = (SELECT startDate_no1 FROM tempTbl WHERE year= 2008 AND season= 3)

注意,別忘了上面語句中的括號