我想这样做:
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
答案 0 :(得分:5)
select *
into #temptable
from someTable
where someTable.Column1='BLAH'
只需将“@”更改为“#”;)您也可以使用CTE。 Link
答案 1 :(得分:1)
WITH A
AS
(
select * from someTable where someTable.Column1='BLAH'
)
其中A
是“临时表”的别名。之后,您可以像其他普通表一样选择它。只知道你在<{1}}
Order By
答案 2 :(得分: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
谢谢大家的非凡帮助!
我带着一张永久的桌子,然后一直放下它!