2

我想这样做:

DECLARE @TmpTable TABLE = select * from someTable where someTable.Column1='BLAH'

我需要@TmpTable成为的一个子集,someTable并且我不想隐式声明一个带有字段的结构@TmpTable,而是我希望它是从 someTable 动态创建的。

这可能吗?

非常感谢您的帮助和指导!

我已经接受了尝试临时表的建议。但是,我收到错误消息:

  The table #SubSet does not exist in the database

这是代码:

DECLARE @StartDT DATE
DECLARE @MinDOS DATE
SELECT @MinDOS = MIN(dos) FROM accn_demographics
SELECT @StartDT = 
    CAST(CAST(datepart(YYYY,@MinDOS) AS varchar) + '-' + CAST(datepart(mm,@MinDOS) AS varchar) + '-' + CAST('01' AS varchar) AS DATETIME)
DECLARE @FileLocation VARCHAR(50)
DROP TABLE #SubSet
WHILE @StartDT < '20110901'
BEGIN
    SELECT * 
        INTO #SubSet
    FROM ViewAccountDetail
    WHERE datepart(yyyy,ViewAccountDetail.DOS) = datepart(yyyy,@StartDT)
        AND datepart(mm,ViewAccountDetail.DOS) = datepart(mm,@StartDT)
    SET @FileLocation='C:\test\'+'ViewAccountDetail'+cast(@StartDT as varchar)+'.csv'
    EXEC BCP_Text_File #SubSet, @FileLocation       
    SET @StartDT = DATEADD(MONTH,1,@StartDT)

    DROP TABLE #SubSet
END
4

3 回答 3

5
select * 
into #temptable
from someTable 
where someTable.Column1='BLAH'

只需将“@”更改为“#”;)您也可以使用 CTE。关联

于 2012-06-14T19:12:21.487 回答
1
WITH A
AS
(
    select * from someTable where someTable.Column1='BLAH'
)

A您的“临时表”的别名在哪里。之后,您可以像选择另一个普通表一样选择它。只知道你不能Order Byselect * from someTable where someTable.Column1='BLAH'

于 2012-06-14T19:14:54.420 回答
0

这就是我所做的!

DECLARE @StartDT DATE 
DECLARE @MinDOS DATE 
SELECT @MinDOS = MIN(dos) FROM accn_demographics 
SELECT @StartDT = 
CAST(CAST(datepart(YYYY,@MinDOS) AS varchar) + '-' + CAST(datepart(mm,@MinDOS) AS varchar) + '-' + CAST('01' AS varchar) AS DATETIME) 
DECLARE @FileLocation VARCHAR(50) 
DROP TABLE #SubSet 
WHILE @StartDT < '20110901' 
BEGIN 
SELECT * 
INTO SubSet 
FROM ViewTransactionDetails 
WHERE datepart(yyyy,ViewTransactionDetails.DOS) = datepart(yyyy,@StartDT) 
AND datepart(mm,ViewTransactionDetails.DOS) = datepart(mm,@StartDT) 
SET @FileLocation='C:\test\'+'ViewTransactionDetails'+cast(@StartDT as varchar)+'.csv' 
EXEC BCP_Text_File SubSet, @FileLocation    
SET @StartDT = DATEADD(MONTH,1,@StartDT) 

DROP TABLE SubSet 
END

感谢大家的非凡帮助!

我选择了一张永久性的桌子,然后继续放下它!

于 2012-06-14T19:50:06.980 回答